1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
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.
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.
13// Written by Wang Rui, (C) 2010
15#ifndef OSGPARTICLE_EXPLOSIONOPERATOR
16#define OSGPARTICLE_EXPLOSIONOPERATOR
18#include <osgParticle/ModularProgram>
19#include <osgParticle/Operator>
20#include <osgParticle/Particle>
26/** An explosion operator exerts force on each particle away from the explosion center.
27 Refer to David McAllister's Particle System API (http://www.particlesystems.org)
29class ExplosionOperator : public Operator
33 : Operator(), _radius(1.0f),
34 _magnitude(1.0f), _epsilon(1e-3), _sigma(1.0f),
35 _inexp(0.0f), _outexp(0.0f)
38 ExplosionOperator( const ExplosionOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
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)
44 META_Object( osgParticle, ExplosionOperator );
46 /// Set the center of shock wave
47 void setCenter( const osg::Vec3& c ) { _center = c; }
49 /// Get the center of shock wave
50 const osg::Vec3& getCenter() const { return _center; }
52 /// Set the radius of wave peak
53 void setRadius( float r ) { _radius = r; }
55 /// Get the radius of wave peak
56 float getRadius() const { return _radius; }
58 /// Set the acceleration scale
59 void setMagnitude( float mag ) { _magnitude = mag; }
61 /// Get the acceleration scale
62 float getMagnitude() const { return _magnitude; }
64 /// Set the acceleration epsilon
65 void setEpsilon( float eps ) { _epsilon = eps; }
67 /// Get the acceleration epsilon
68 float getEpsilon() const { return _epsilon; }
70 /// Set broadness of the strength of the wave
71 void setSigma( float s ) { _sigma = s; }
73 /// Get broadness of the strength of the wave
74 float getSigma() const { return _sigma; }
76 /// Apply the acceleration to a particle. Do not call this method manually.
77 inline void operate( Particle* P, double dt );
79 /// Perform some initializations. Do not call this method manually.
80 inline void beginOperate( Program* prg );
83 virtual ~ExplosionOperator() {}
84 ExplosionOperator& operator=( const ExplosionOperator& ) { return *this; }
98inline void ExplosionOperator::operate( Particle* P, double dt )
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) );
108inline void ExplosionOperator::beginOperate( Program* prg )
110 if ( prg->getReferenceFrame()==ModularProgram::RELATIVE_RF )
112 _xf_center = prg->transformLocalToWorld(_center);
116 _xf_center = _center;
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);