RadialShooter 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. //osgParticle - Copyright (C) 2002 Marco Jez
  14. #ifndef OSGPARTICLE_RADIAL_SHOOTER
  15. #define OSGPARTICLE_RADIAL_SHOOTER 1
  16. #include <osgParticle/Shooter>
  17. #include <osgParticle/Particle>
  18. #include <osgParticle/range>
  19. #include <osg/CopyOp>
  20. #include <osg/Object>
  21. #include <osg/Math>
  22. namespace osgParticle
  23. {
  24. /** A shooter class that shoots particles radially.
  25. This shooter computes the velocity vector of incoming particles by choosing a
  26. random direction and a random speed. Both direction and speed are chosen within
  27. specified ranges. The direction is defined by two angles: <B>theta</B>, which
  28. is the angle between the velocity vector and the Z axis, and <B>phi</B>, which is
  29. the angle between the X axis and the velocity vector projected onto the X-Y plane.
  30. */
  31. class RadialShooter: public Shooter {
  32. public:
  33. inline RadialShooter();
  34. inline RadialShooter(const RadialShooter& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  35. META_Object(osgParticle, RadialShooter);
  36. /// Get the range of possible values for <B>theta</B> angle.
  37. inline const rangef& getThetaRange() const;
  38. /// Set the range of possible values for <B>theta</B> angle.
  39. inline void setThetaRange(const rangef& r);
  40. /// Set the range of possible values for <B>theta</B> angle.
  41. inline void setThetaRange(float r1, float r2);
  42. /// Get the range of possible values for <B>phi</B> angle.
  43. inline const rangef& getPhiRange() const;
  44. /// Set the range of possible values for <B>phi</B> angle.
  45. inline void setPhiRange(const rangef& r);
  46. /// Set the range of possible values for <B>phi</B> angle.
  47. inline void setPhiRange(float r1, float r2);
  48. /// Get the range of possible values for initial speed of particles.
  49. inline const rangef& getInitialSpeedRange() const;
  50. /// Set the range of possible values for initial speed of particles.
  51. inline void setInitialSpeedRange(const rangef& r);
  52. /// Set the range of possible values for initial speed of particles.
  53. inline void setInitialSpeedRange(float r1, float r2);
  54. /// Get the range of possible values for initial rotational speed of particles.
  55. inline const rangev3& getInitialRotationalSpeedRange() const;
  56. /// Set the range of possible values for initial rotational speed of particles.
  57. inline void setInitialRotationalSpeedRange(const rangev3& r);
  58. /// Set the range of possible values for initial rotational speed of particles.
  59. inline void setInitialRotationalSpeedRange(const osg::Vec3& r1, const osg::Vec3& r2);
  60. /// Shoot a particle. Do not call this method manually.
  61. inline void shoot(Particle* P) const;
  62. protected:
  63. virtual ~RadialShooter() {}
  64. RadialShooter& operator=(const RadialShooter&) { return *this; }
  65. private:
  66. rangef _theta_range;
  67. rangef _phi_range;
  68. rangef _speed_range;
  69. rangev3 _rot_speed_range;
  70. };
  71. // INLINE FUNCTIONS
  72. inline RadialShooter::RadialShooter()
  73. : Shooter(),
  74. _theta_range(0, 0.5f*osg::PI_4),
  75. _phi_range(0, 2*osg::PI),
  76. _speed_range(10, 10),
  77. _rot_speed_range(osg::Vec3(0,0,0), osg::Vec3(0,0,0))
  78. {
  79. }
  80. inline RadialShooter::RadialShooter(const RadialShooter& copy, const osg::CopyOp& copyop)
  81. : Shooter(copy, copyop),
  82. _theta_range(copy._theta_range),
  83. _phi_range(copy._phi_range),
  84. _speed_range(copy._speed_range),
  85. _rot_speed_range(copy._rot_speed_range)
  86. {
  87. }
  88. inline const rangef& RadialShooter::getThetaRange() const
  89. {
  90. return _theta_range;
  91. }
  92. inline const rangef& RadialShooter::getPhiRange() const
  93. {
  94. return _phi_range;
  95. }
  96. inline const rangef& RadialShooter::getInitialSpeedRange() const
  97. {
  98. return _speed_range;
  99. }
  100. inline const rangev3& RadialShooter::getInitialRotationalSpeedRange() const
  101. {
  102. return _rot_speed_range;
  103. }
  104. inline void RadialShooter::setThetaRange(const rangef& r)
  105. {
  106. _theta_range = r;
  107. }
  108. inline void RadialShooter::setThetaRange(float r1, float r2)
  109. {
  110. _theta_range.minimum = r1;
  111. _theta_range.maximum = r2;
  112. }
  113. inline void RadialShooter::setPhiRange(const rangef& r)
  114. {
  115. _phi_range = r;
  116. }
  117. inline void RadialShooter::setPhiRange(float r1, float r2)
  118. {
  119. _phi_range.minimum = r1;
  120. _phi_range.maximum = r2;
  121. }
  122. inline void RadialShooter::setInitialSpeedRange(const rangef& r)
  123. {
  124. _speed_range = r;
  125. }
  126. inline void RadialShooter::setInitialSpeedRange(float r1, float r2)
  127. {
  128. _speed_range.minimum = r1;
  129. _speed_range.maximum = r2;
  130. }
  131. inline void RadialShooter::setInitialRotationalSpeedRange(const rangev3& r)
  132. {
  133. _rot_speed_range = r;
  134. }
  135. inline void RadialShooter::setInitialRotationalSpeedRange(const osg::Vec3& r1, const osg::Vec3& r2)
  136. {
  137. _rot_speed_range.minimum = r1;
  138. _rot_speed_range.maximum = r2;
  139. }
  140. inline void RadialShooter::shoot(Particle* P) const
  141. {
  142. float theta = _theta_range.get_random();
  143. float phi = _phi_range.get_random();
  144. float speed = _speed_range.get_random();
  145. osg::Vec3 rot_speed = _rot_speed_range.get_random();
  146. P->setVelocity(osg::Vec3(
  147. speed * sinf(theta) * cosf(phi),
  148. speed * sinf(theta) * sinf(phi),
  149. speed * cosf(theta)
  150. ));
  151. P->setAngularVelocity(rot_speed);
  152. }
  153. }
  154. #endif