| 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 | |
|---|
| 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) |
|---|
| 28 | */ |
|---|
| 29 | class ExplosionOperator : public Operator |
|---|
| 30 | { |
|---|
| 31 | public: |
|---|
| 32 | ExplosionOperator() |
|---|
| 33 | : Operator(), _radius(1.0f), |
|---|
| 34 | _magnitude(1.0f), _epsilon(1e-3), _sigma(1.0f), |
|---|
| 35 | _inexp(0.0f), _outexp(0.0f) |
|---|
| 36 | {} |
|---|
| 37 | |
|---|
| 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) |
|---|
| 42 | {} |
|---|
| 43 | |
|---|
| 44 | META_Object( osgParticle, ExplosionOperator ); |
|---|
| 45 | |
|---|
| 46 | /// Set the center of shock wave |
|---|
| 47 | void setCenter( const osg::Vec3& c ) { _center = c; } |
|---|
| 48 | |
|---|
| 49 | /// Get the center of shock wave |
|---|
| 50 | const osg::Vec3& getCenter() const { return _center; } |
|---|
| 51 | |
|---|
| 52 | /// Set the radius of wave peak |
|---|
| 53 | void setRadius( float r ) { _radius = r; } |
|---|
| 54 | |
|---|
| 55 | /// Get the radius of wave peak |
|---|
| 56 | float getRadius() const { return _radius; } |
|---|
| 57 | |
|---|
| 58 | /// Set the acceleration scale |
|---|
| 59 | void setMagnitude( float mag ) { _magnitude = mag; } |
|---|
| 60 | |
|---|
| 61 | /// Get the acceleration scale |
|---|
| 62 | float getMagnitude() const { return _magnitude; } |
|---|
| 63 | |
|---|
| 64 | /// Set the acceleration epsilon |
|---|
| 65 | void setEpsilon( float eps ) { _epsilon = eps; } |
|---|
| 66 | |
|---|
| 67 | /// Get the acceleration epsilon |
|---|
| 68 | float getEpsilon() const { return _epsilon; } |
|---|
| 69 | |
|---|
| 70 | /// Set broadness of the strength of the wave |
|---|
| 71 | void setSigma( float s ) { _sigma = s; } |
|---|
| 72 | |
|---|
| 73 | /// Get broadness of the strength of the wave |
|---|
| 74 | float getSigma() const { return _sigma; } |
|---|
| 75 | |
|---|
| 76 | /// Apply the acceleration to a particle. Do not call this method manually. |
|---|
| 77 | inline void operate( Particle* P, double dt ); |
|---|
| 78 | |
|---|
| 79 | /// Perform some initializations. Do not call this method manually. |
|---|
| 80 | inline void beginOperate( Program* prg ); |
|---|
| 81 | |
|---|
| 82 | protected: |
|---|
| 83 | virtual ~ExplosionOperator() {} |
|---|
| 84 | ExplosionOperator& operator=( const ExplosionOperator& ) { return *this; } |
|---|
| 85 | |
|---|
| 86 | osg::Vec3 _center; |
|---|
| 87 | osg::Vec3 _xf_center; |
|---|
| 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 | |
|---|
| 108 | inline void ExplosionOperator::beginOperate( Program* prg ) |
|---|
| 109 | { |
|---|
| 110 | if ( prg->getReferenceFrame()==ModularProgram::RELATIVE_RF ) |
|---|
| 111 | { |
|---|
| 112 | _xf_center = prg->transformLocalToWorld(_center); |
|---|
| 113 | } |
|---|
| 114 | else |
|---|
| 115 | { |
|---|
| 116 | _xf_center = _center; |
|---|
| 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 |
|---|