ExplosionOperator 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifndef OSGPARTICLE_EXPLOSIONOPERATOR
  15. #define OSGPARTICLE_EXPLOSIONOPERATOR
  16. #include <osgParticle/ModularProgram>
  17. #include <osgParticle/Operator>
  18. #include <osgParticle/Particle>
  19. namespace osgParticle
  20. {
  21. /** An explosion operator exerts force on each particle away from the explosion center.
  22. Refer to David McAllister's Particle System API (http://www.particlesystems.org)
  23. */
  24. class ExplosionOperator : public Operator
  25. {
  26. public:
  27. ExplosionOperator()
  28. : Operator(), _radius(1.0f),
  29. _magnitude(1.0f), _epsilon(1e-3), _sigma(1.0f),
  30. _inexp(0.0f), _outexp(0.0f)
  31. {}
  32. ExplosionOperator( const ExplosionOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
  33. : Operator(copy, copyop), _center(copy._center), _radius(copy._radius),
  34. _magnitude(copy._magnitude), _epsilon(copy._epsilon), _sigma(copy._sigma),
  35. _inexp(copy._inexp), _outexp(copy._outexp)
  36. {}
  37. META_Object( osgParticle, ExplosionOperator );
  38. /// Set the center of shock wave
  39. void setCenter( const osg::Vec3& c ) { _center = c; }
  40. /// Get the center of shock wave
  41. const osg::Vec3& getCenter() const { return _center; }
  42. /// Set the radius of wave peak
  43. void setRadius( float r ) { _radius = r; }
  44. /// Get the radius of wave peak
  45. float getRadius() const { return _radius; }
  46. /// Set the acceleration scale
  47. void setMagnitude( float mag ) { _magnitude = mag; }
  48. /// Get the acceleration scale
  49. float getMagnitude() const { return _magnitude; }
  50. /// Set the acceleration epsilon
  51. void setEpsilon( float eps ) { _epsilon = eps; }
  52. /// Get the acceleration epsilon
  53. float getEpsilon() const { return _epsilon; }
  54. /// Set broadness of the strength of the wave
  55. void setSigma( float s ) { _sigma = s; }
  56. /// Get broadness of the strength of the wave
  57. float getSigma() const { return _sigma; }
  58. /// Apply the acceleration to a particle. Do not call this method manually.
  59. inline void operate( Particle* P, double dt );
  60. /// Perform some initializations. Do not call this method manually.
  61. inline void beginOperate( Program* prg );
  62. protected:
  63. virtual ~ExplosionOperator() {}
  64. ExplosionOperator& operator=( const ExplosionOperator& ) { return *this; }
  65. osg::Vec3 _center;
  66. osg::Vec3 _xf_center;
  67. float _radius;
  68. float _magnitude;
  69. float _epsilon;
  70. float _sigma;
  71. float _inexp;
  72. float _outexp;
  73. };
  74. // INLINE METHODS
  75. inline void ExplosionOperator::operate( Particle* P, double dt )
  76. {
  77. osg::Vec3 dir = P->getPosition() - _xf_center;
  78. float length = dir.length();
  79. float distanceFromWave2 = (_radius - length) * (_radius - length);
  80. float Gd = exp(distanceFromWave2 * _inexp) * _outexp;
  81. float factor = (_magnitude * dt) / (length * (_epsilon+length*length));
  82. P->addVelocity( dir * (Gd * factor) );
  83. }
  84. inline void ExplosionOperator::beginOperate( Program* prg )
  85. {
  86. if ( prg->getReferenceFrame()==ModularProgram::RELATIVE_RF )
  87. {
  88. _xf_center = prg->transformLocalToWorld(_center);
  89. }
  90. else
  91. {
  92. _xf_center = _center;
  93. }
  94. float oneOverSigma = (_sigma!=0.0f ? (1.0f / _sigma) : 1.0f);
  95. _inexp = -0.5f * oneOverSigma * oneOverSigma;
  96. _outexp = oneOverSigma / sqrt(osg::PI * 2.0f);
  97. }
  98. }
  99. #endif