OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExplosionOperator.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 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 // Written by Wang Rui, (C) 2010
14 
15 #ifndef OSGPARTICLE_EXPLOSIONOPERATOR
16 #define OSGPARTICLE_EXPLOSIONOPERATOR
17 
18 #include <osgParticle/ModularProgram>
19 #include <osgParticle/Operator>
20 #include <osgParticle/Particle>
21 
22 namespace osgParticle
23 {
24 
25 
30 {
31 public:
33  : Operator(), _radius(1.0f),
34  _magnitude(1.0f), _epsilon(1e-3), _sigma(1.0f),
35  _inexp(0.0f), _outexp(0.0f)
36  {}
37 
39  : Operator(copy, copyop), _center(copy._center), _radius(copy._radius),
40  _magnitude(copy._magnitude), _epsilon(copy._epsilon), _sigma(copy._sigma),
41  _inexp(copy._inexp), _outexp(copy._outexp)
42  {}
43 
45 
47  void setCenter( const osg::Vec3& c ) { _center = c; }
48 
50  const osg::Vec3& getCenter() const { return _center; }
51 
53  void setRadius( float r ) { _radius = r; }
54 
56  float getRadius() const { return _radius; }
57 
59  void setMagnitude( float mag ) { _magnitude = mag; }
60 
62  float getMagnitude() const { return _magnitude; }
63 
65  void setEpsilon( float eps ) { _epsilon = eps; }
66 
68  float getEpsilon() const { return _epsilon; }
69 
71  void setSigma( float s ) { _sigma = s; }
72 
74  float getSigma() const { return _sigma; }
75 
77  inline void operate( Particle* P, double dt );
78 
80  inline void beginOperate( Program* prg );
81 
82 protected:
83  virtual ~ExplosionOperator() {}
84  ExplosionOperator& operator=( const ExplosionOperator& ) { return *this; }
85 
88  float _radius;
89  float _magnitude;
90  float _epsilon;
91  float _sigma;
92  float _inexp;
93  float _outexp;
94 };
95 
96 // INLINE METHODS
97 
98 inline void ExplosionOperator::operate( Particle* P, double dt )
99 {
100  osg::Vec3 dir = P->getPosition() - _xf_center;
101  float length = dir.length();
102  float distanceFromWave2 = (_radius - length) * (_radius - length);
103  float Gd = exp(distanceFromWave2 * _inexp) * _outexp;
104  float factor = (_magnitude * dt) / (length * (_epsilon+length*length));
105  P->addVelocity( dir * (Gd * factor) );
106 }
107 
109 {
111  {
113  }
114  else
115  {
117  }
118 
119  float oneOverSigma = (_sigma!=0.0f ? (1.0f / _sigma) : 1.0f);
120  _inexp = -0.5f * oneOverSigma * oneOverSigma;
121  _outexp = oneOverSigma / sqrt(osg::PI * 2.0f);
122 }
123 
124 
125 }
126 
127 #endif
float getRadius() const
Get the radius of wave peak.
ExplosionOperator & operator=(const ExplosionOperator &)
ExplosionOperator(const ExplosionOperator &copy, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
void setEpsilon(float eps)
Set the acceleration epsilon.
float getSigma() const
Get broadness of the strength of the wave.
void setCenter(const osg::Vec3 &c)
Set the center of shock wave.
void setSigma(float s)
Set broadness of the strength of the wave.
const osg::Vec3 & getPosition() const
Get the position vector.
Definition: Particle.h:417
void setRadius(float r)
Set the radius of wave peak.
ReferenceFrame getReferenceFrame() const
Get the reference frame.
float getEpsilon() const
Get the acceleration epsilon.
float getMagnitude() const
Get the acceleration scale.
const double PI
Definition: Math.h:30
void setMagnitude(float mag)
Set the acceleration scale.
void operate(Particle *P, double dt)
Apply the acceleration to a particle. Do not call this method manually.
META_Object(osgParticle, ExplosionOperator)
void addVelocity(const osg::Vec3 &dv)
Add a vector to the velocity vector.
Definition: Particle.h:507
value_type length() const
Definition: Vec3f.h:176
const osg::Vec3 & getCenter() const
Get the center of shock wave.
osg::Vec3 transformLocalToWorld(const osg::Vec3 &P)
Transform a point from local to world coordinates (valid only during cull traversal).
void beginOperate(Program *prg)
Perform some initializations. Do not call this method manually.