| 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 | |
|---|
| 15 | #ifndef OSGPARTICLE_RADIAL_SHOOTER |
|---|
| 16 | #define OSGPARTICLE_RADIAL_SHOOTER 1 |
|---|
| 17 | |
|---|
| 18 | #include <osgParticle/Shooter> |
|---|
| 19 | #include <osgParticle/Particle> |
|---|
| 20 | #include <osgParticle/range> |
|---|
| 21 | |
|---|
| 22 | #include <osg/CopyOp> |
|---|
| 23 | #include <osg/Object> |
|---|
| 24 | #include <osg/Math> |
|---|
| 25 | |
|---|
| 26 | namespace osgParticle |
|---|
| 27 | { |
|---|
| 28 | |
|---|
| 29 | /** A shooter class that shoots particles radially. |
|---|
| 30 | This shooter computes the velocity vector of incoming particles by choosing a |
|---|
| 31 | random direction and a random speed. Both direction and speed are chosen within |
|---|
| 32 | specified ranges. The direction is defined by two angles: <B>theta</B>, which |
|---|
| 33 | is the angle between the velocity vector and the Z axis, and <B>phi</B>, which is |
|---|
| 34 | the angle between the X axis and the velocity vector projected onto the X-Y plane. |
|---|
| 35 | */ |
|---|
| 36 | class RadialShooter: public Shooter { |
|---|
| 37 | public: |
|---|
| 38 | inline RadialShooter(); |
|---|
| 39 | inline RadialShooter(const RadialShooter& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY); |
|---|
| 40 | |
|---|
| 41 | META_Object(osgParticle, RadialShooter); |
|---|
| 42 | |
|---|
| 43 | /// Get the range of possible values for <B>theta</B> angle. |
|---|
| 44 | inline const rangef& getThetaRange() const; |
|---|
| 45 | |
|---|
| 46 | /// Set the range of possible values for <B>theta</B> angle. |
|---|
| 47 | inline void setThetaRange(const rangef& r); |
|---|
| 48 | |
|---|
| 49 | /// Set the range of possible values for <B>theta</B> angle. |
|---|
| 50 | inline void setThetaRange(float r1, float r2); |
|---|
| 51 | |
|---|
| 52 | /// Get the range of possible values for <B>phi</B> angle. |
|---|
| 53 | inline const rangef& getPhiRange() const; |
|---|
| 54 | |
|---|
| 55 | /// Set the range of possible values for <B>phi</B> angle. |
|---|
| 56 | inline void setPhiRange(const rangef& r); |
|---|
| 57 | |
|---|
| 58 | /// Set the range of possible values for <B>phi</B> angle. |
|---|
| 59 | inline void setPhiRange(float r1, float r2); |
|---|
| 60 | |
|---|
| 61 | /// Get the range of possible values for initial speed of particles. |
|---|
| 62 | inline const rangef& getInitialSpeedRange() const; |
|---|
| 63 | |
|---|
| 64 | /// Set the range of possible values for initial speed of particles. |
|---|
| 65 | inline void setInitialSpeedRange(const rangef& r); |
|---|
| 66 | |
|---|
| 67 | /// Set the range of possible values for initial speed of particles. |
|---|
| 68 | inline void setInitialSpeedRange(float r1, float r2); |
|---|
| 69 | |
|---|
| 70 | /// Get the range of possible values for initial rotational speed of particles. |
|---|
| 71 | inline const rangev3& getInitialRotationalSpeedRange() const; |
|---|
| 72 | |
|---|
| 73 | /// Set the range of possible values for initial rotational speed of particles. |
|---|
| 74 | inline void setInitialRotationalSpeedRange(const rangev3& r); |
|---|
| 75 | |
|---|
| 76 | /// Set the range of possible values for initial rotational speed of particles. |
|---|
| 77 | inline void setInitialRotationalSpeedRange(const osg::Vec3& r1, const osg::Vec3& r2); |
|---|
| 78 | |
|---|
| 79 | /// Shoot a particle. Do not call this method manually. |
|---|
| 80 | inline void shoot(Particle* P) const; |
|---|
| 81 | |
|---|
| 82 | protected: |
|---|
| 83 | virtual ~RadialShooter() {} |
|---|
| 84 | RadialShooter& operator=(const RadialShooter&) { return *this; } |
|---|
| 85 | |
|---|
| 86 | private: |
|---|
| 87 | rangef _theta_range; |
|---|
| 88 | rangef _phi_range; |
|---|
| 89 | rangef _speed_range; |
|---|
| 90 | rangev3 _rot_speed_range; |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | // INLINE FUNCTIONS |
|---|
| 94 | |
|---|
| 95 | inline RadialShooter::RadialShooter() |
|---|
| 96 | : Shooter(), |
|---|
| 97 | _theta_range(0, 0.5f*osg::PI_4), |
|---|
| 98 | _phi_range(0, 2*osg::PI), |
|---|
| 99 | _speed_range(10, 10), |
|---|
| 100 | _rot_speed_range(osg::Vec3(0,0,0), osg::Vec3(0,0,0)) |
|---|
| 101 | { |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | inline RadialShooter::RadialShooter(const RadialShooter& copy, const osg::CopyOp& copyop) |
|---|
| 105 | : Shooter(copy, copyop), |
|---|
| 106 | _theta_range(copy._theta_range), |
|---|
| 107 | _phi_range(copy._phi_range), |
|---|
| 108 | _speed_range(copy._speed_range), |
|---|
| 109 | _rot_speed_range(copy._rot_speed_range) |
|---|
| 110 | { |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | inline const rangef& RadialShooter::getThetaRange() const |
|---|
| 114 | { |
|---|
| 115 | return _theta_range; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | inline const rangef& RadialShooter::getPhiRange() const |
|---|
| 119 | { |
|---|
| 120 | return _phi_range; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | inline const rangef& RadialShooter::getInitialSpeedRange() const |
|---|
| 124 | { |
|---|
| 125 | return _speed_range; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | inline const rangev3& RadialShooter::getInitialRotationalSpeedRange() const |
|---|
| 129 | { |
|---|
| 130 | return _rot_speed_range; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | inline void RadialShooter::setThetaRange(const rangef& r) |
|---|
| 134 | { |
|---|
| 135 | _theta_range = r; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | inline void RadialShooter::setThetaRange(float r1, float r2) |
|---|
| 139 | { |
|---|
| 140 | _theta_range.minimum = r1; |
|---|
| 141 | _theta_range.maximum = r2; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | inline void RadialShooter::setPhiRange(const rangef& r) |
|---|
| 145 | { |
|---|
| 146 | _phi_range = r; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | inline void RadialShooter::setPhiRange(float r1, float r2) |
|---|
| 150 | { |
|---|
| 151 | _phi_range.minimum = r1; |
|---|
| 152 | _phi_range.maximum = r2; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | inline void RadialShooter::setInitialSpeedRange(const rangef& r) |
|---|
| 156 | { |
|---|
| 157 | _speed_range = r; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | inline void RadialShooter::setInitialSpeedRange(float r1, float r2) |
|---|
| 161 | { |
|---|
| 162 | _speed_range.minimum = r1; |
|---|
| 163 | _speed_range.maximum = r2; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | inline void RadialShooter::setInitialRotationalSpeedRange(const rangev3& r) |
|---|
| 167 | { |
|---|
| 168 | _rot_speed_range = r; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | inline void RadialShooter::setInitialRotationalSpeedRange(const osg::Vec3& r1, const osg::Vec3& r2) |
|---|
| 172 | { |
|---|
| 173 | _rot_speed_range.minimum = r1; |
|---|
| 174 | _rot_speed_range.maximum = r2; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | inline void RadialShooter::shoot(Particle* P) const |
|---|
| 178 | { |
|---|
| 179 | float theta = _theta_range.get_random(); |
|---|
| 180 | float phi = _phi_range.get_random(); |
|---|
| 181 | float speed = _speed_range.get_random(); |
|---|
| 182 | osg::Vec3 rot_speed = _rot_speed_range.get_random(); |
|---|
| 183 | |
|---|
| 184 | P->setVelocity(osg::Vec3( |
|---|
| 185 | speed * sinf(theta) * cosf(phi), |
|---|
| 186 | speed * sinf(theta) * sinf(phi), |
|---|
| 187 | speed * cosf(theta) |
|---|
| 188 | )); |
|---|
| 189 | |
|---|
| 190 | P->setAngularVelocity(rot_speed); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | #endif |
|---|