AngularDampingOperator 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_ANGULARDAMPINGOPERATOR
  15. #define OSGPARTICLE_ANGULARDAMPINGOPERATOR
  16. #include <osgParticle/Operator>
  17. #include <osgParticle/Particle>
  18. namespace osgParticle
  19. {
  20. /** A angular damping operator applies damping constant to particle's angular velocity.
  21. Refer to David McAllister's Particle System API (http://www.particlesystems.org)
  22. */
  23. class AngularDampingOperator : public Operator
  24. {
  25. public:
  26. AngularDampingOperator() : Operator(), _cutoffLow(0.0f), _cutoffHigh(FLT_MAX)
  27. {}
  28. AngularDampingOperator( const AngularDampingOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
  29. : Operator(copy, copyop), _damping(copy._damping),
  30. _cutoffLow(copy._cutoffLow), _cutoffHigh(copy._cutoffHigh)
  31. {}
  32. META_Object( osgParticle, AngularDampingOperator );
  33. /// Set the damping factors
  34. void setDamping( float x, float y, float z ) { _damping.set(x, y, z); }
  35. void setDamping( const osg::Vec3& damping ) { _damping = damping; }
  36. /// Set the damping factors to one value
  37. void setDamping( float x ) { _damping.set(x, x, x); }
  38. /// Get the damping factors
  39. void getDamping( float& x, float& y, float& z ) const
  40. { x = _damping.x(); y = _damping.y(); z = _damping.z(); }
  41. const osg::Vec3& getDamping() const { return _damping; }
  42. /// Set the velocity cutoff factors
  43. void setCutoff( float low, float high ) { _cutoffLow = low; _cutoffHigh = high; }
  44. void setCutoffLow( float low ) { _cutoffLow = low; }
  45. void setCutoffHigh( float low ) { _cutoffHigh = low; }
  46. /// Get the velocity cutoff factors
  47. void getCutoff( float& low, float& high ) const { low = _cutoffLow; high = _cutoffHigh; }
  48. float getCutoffLow() const { return _cutoffLow; }
  49. float getCutoffHigh() const { return _cutoffHigh; }
  50. /// Apply the acceleration to a particle. Do not call this method manually.
  51. inline void operate( Particle* P, double dt );
  52. protected:
  53. virtual ~AngularDampingOperator() {}
  54. AngularDampingOperator& operator=( const AngularDampingOperator& ) { return *this; }
  55. osg::Vec3 _damping;
  56. float _cutoffLow;
  57. float _cutoffHigh;
  58. };
  59. // INLINE METHODS
  60. inline void AngularDampingOperator::operate( Particle* P, double dt )
  61. {
  62. const osg::Vec3& vel = P->getAngularVelocity();
  63. float length2 = vel.length2();
  64. if ( length2>=_cutoffLow && length2<=_cutoffHigh )
  65. {
  66. osg::Vec3 newvel( vel.x() * (1.0f - (1.0f - _damping.x()) * dt),
  67. vel.y() * (1.0f - (1.0f - _damping.y()) * dt),
  68. vel.z() * (1.0f - (1.0f - _damping.z()) * dt) );
  69. P->setAngularVelocity( newvel );
  70. }
  71. }
  72. }
  73. #endif