ExplosionOperator
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
// Written by Wang Rui, (C) 2010
#ifndef OSGPARTICLE_EXPLOSIONOPERATOR
#define OSGPARTICLE_EXPLOSIONOPERATOR
#include <osgParticle/ModularProgram>
#include <osgParticle/Operator>
#include <osgParticle/Particle>
namespace osgParticle
{
class ExplosionOperator : public Operator
{
public:
ExplosionOperator()
: Operator(), _radius(1.0f),
_magnitude(1.0f), _epsilon(1e-3), _sigma(1.0f),
_inexp(0.0f), _outexp(0.0f)
{}
ExplosionOperator( const ExplosionOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
: Operator(copy, copyop), _center(copy._center), _radius(copy._radius),
_magnitude(copy._magnitude), _epsilon(copy._epsilon), _sigma(copy._sigma),
_inexp(copy._inexp), _outexp(copy._outexp)
{}
META_Object( osgParticle, ExplosionOperator );
void setCenter( const osg::Vec3& c ) { _center = c; }
const osg::Vec3& getCenter() const { return _center; }
void setRadius( float r ) { _radius = r; }
float getRadius() const { return _radius; }
void setMagnitude( float mag ) { _magnitude = mag; }
float getMagnitude() const { return _magnitude; }
void setEpsilon( float eps ) { _epsilon = eps; }
float getEpsilon() const { return _epsilon; }
void setSigma( float s ) { _sigma = s; }
float getSigma() const { return _sigma; }
inline void operate( Particle* P, double dt );
inline void beginOperate( Program* prg );
protected:
virtual ~ExplosionOperator() {}
ExplosionOperator& operator=( const ExplosionOperator& ) { return *this; }
osg::Vec3 _center;
osg::Vec3 _xf_center;
float _radius;
float _magnitude;
float _epsilon;
float _sigma;
float _inexp;
float _outexp;
};
// INLINE METHODS
inline void ExplosionOperator::operate( Particle* P, double dt )
{
osg::Vec3 dir = P->getPosition() - _xf_center;
float length = dir.length();
float distanceFromWave2 = (_radius - length) * (_radius - length);
float Gd = exp(distanceFromWave2 * _inexp) * _outexp;
float factor = (_magnitude * dt) / (length * (_epsilon+length*length));
P->addVelocity( dir * (Gd * factor) );
}
inline void ExplosionOperator::beginOperate( Program* prg )
{
if ( prg->getReferenceFrame()==ModularProgram::RELATIVE_RF )
{
_xf_center = prg->transformLocalToWorld(_center);
}
else
{
_xf_center = _center;
}
float oneOverSigma = (_sigma!=0.0f ? (1.0f / _sigma) : 1.0f);
_inexp = -0.5f * oneOverSigma * oneOverSigma;
_outexp = oneOverSigma / sqrt(osg::PI * 2.0f);
}
}
#endif
ModularProgram
Operator
Particle
osgParticle::ExplosionOperator::setEpsilon
void setEpsilon(float eps)
Set the acceleration epsilon.
Definition ExplosionOperator:65
osgParticle::ExplosionOperator::_xf_center
osg::Vec3 _xf_center
Definition ExplosionOperator:87
osgParticle::ExplosionOperator::_center
osg::Vec3 _center
Definition ExplosionOperator:86
osgParticle::ExplosionOperator::beginOperate
void beginOperate(Program *prg)
Perform some initializations. Do not call this method manually.
Definition ExplosionOperator:108
osgParticle::ExplosionOperator::_radius
float _radius
Definition ExplosionOperator:88
osgParticle::ExplosionOperator::setCenter
void setCenter(const osg::Vec3 &c)
Set the center of shock wave.
Definition ExplosionOperator:47
osgParticle::ExplosionOperator::setMagnitude
void setMagnitude(float mag)
Set the acceleration scale.
Definition ExplosionOperator:59
osgParticle::ExplosionOperator::_inexp
float _inexp
Definition ExplosionOperator:92
osgParticle::ExplosionOperator::_magnitude
float _magnitude
Definition ExplosionOperator:89
osgParticle::ExplosionOperator::getCenter
const osg::Vec3 & getCenter() const
Get the center of shock wave.
Definition ExplosionOperator:50
osgParticle::ExplosionOperator::~ExplosionOperator
virtual ~ExplosionOperator()
Definition ExplosionOperator:83
osgParticle::ExplosionOperator::setRadius
void setRadius(float r)
Set the radius of wave peak.
Definition ExplosionOperator:53
osgParticle::ExplosionOperator::ExplosionOperator
ExplosionOperator()
Definition ExplosionOperator:32
osgParticle::ExplosionOperator::META_Object
META_Object(osgParticle, ExplosionOperator)
osgParticle::ExplosionOperator::setSigma
void setSigma(float s)
Set broadness of the strength of the wave.
Definition ExplosionOperator:71
osgParticle::ExplosionOperator::_outexp
float _outexp
Definition ExplosionOperator:93
osgParticle::ExplosionOperator::getSigma
float getSigma() const
Get broadness of the strength of the wave.
Definition ExplosionOperator:74
osgParticle::ExplosionOperator::getRadius
float getRadius() const
Get the radius of wave peak.
Definition ExplosionOperator:56
osgParticle::ExplosionOperator::_sigma
float _sigma
Definition ExplosionOperator:91
osgParticle::ExplosionOperator::operator=
ExplosionOperator & operator=(const ExplosionOperator &)
Definition ExplosionOperator:84
osgParticle::ExplosionOperator::ExplosionOperator
ExplosionOperator(const ExplosionOperator &copy, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
Definition ExplosionOperator:38
osgParticle::ExplosionOperator::getEpsilon
float getEpsilon() const
Get the acceleration epsilon.
Definition ExplosionOperator:68
osgParticle::ExplosionOperator::operate
void operate(Particle *P, double dt)
Apply the acceleration to a particle. Do not call this method manually.
Definition ExplosionOperator:98
osgParticle::ExplosionOperator::getMagnitude
float getMagnitude() const
Get the acceleration scale.
Definition ExplosionOperator:62
osgParticle::ExplosionOperator::_epsilon
float _epsilon
Definition ExplosionOperator:90
osgParticle::Operator::Operator
Operator()
Definition Operator:92
osgParticle::ParticleProcessor::RELATIVE_RF
@ RELATIVE_RF
Definition ParticleProcessor:42
osgParticle::ParticleProcessor::getReferenceFrame
ReferenceFrame getReferenceFrame() const
Get the reference frame.
Definition ParticleProcessor:179
osgParticle::ParticleProcessor::transformLocalToWorld
osg::Vec3 transformLocalToWorld(const osg::Vec3 &P)
Transform a point from local to world coordinates (valid only during cull traversal).
Definition ParticleProcessor:305
osgParticle::Particle
Definition Particle:47
osgParticle::Particle::addVelocity
void addVelocity(const osg::Vec3 &dv)
Add a vector to the velocity vector.
Definition Particle:493
osgParticle::Particle::getPosition
const osg::Vec3 & getPosition() const
Get the position vector.
Definition Particle:403
osgParticle::Program
Definition osgParticle/Program:36
osg::CopyOp
Definition CopyOp:41
osg::CopyOp::SHALLOW_COPY
@ SHALLOW_COPY
Definition CopyOp:47
osg::Vec3f::length
value_type length() const
Definition Vec3f:176
osgParticle
Definition AccelOperator:27
osg::Vec3
Vec3f Vec3
Definition Vec3:21
osg::PI
const double PI
Definition Math:30