OrbitOperator 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_ORBITOPERATOR
  15. #define OSGPARTICLE_ORBITOPERATOR
  16. #include <osgParticle/ModularProgram>
  17. #include <osgParticle/Operator>
  18. #include <osgParticle/Particle>
  19. namespace osgParticle
  20. {
  21. /** An orbit operator forces particles in the orbit around a point.
  22. Refer to David McAllister's Particle System API (http://www.particlesystems.org)
  23. */
  24. class OrbitOperator : public Operator
  25. {
  26. public:
  27. OrbitOperator()
  28. : Operator(), _magnitude(1.0f), _epsilon(1e-3), _maxRadius(FLT_MAX)
  29. {}
  30. OrbitOperator( const OrbitOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
  31. : Operator(copy, copyop), _center(copy._center), _magnitude(copy._magnitude),
  32. _epsilon(copy._epsilon), _maxRadius(copy._maxRadius)
  33. {}
  34. META_Object( osgParticle, OrbitOperator );
  35. /// Set the center of orbit
  36. void setCenter( const osg::Vec3& c ) { _center = c; }
  37. /// Get the center of orbit
  38. const osg::Vec3& getCenter() const { return _center; }
  39. /// Set the acceleration scale
  40. void setMagnitude( float mag ) { _magnitude = mag; }
  41. /// Get the acceleration scale
  42. float getMagnitude() const { return _magnitude; }
  43. /// Set the acceleration epsilon
  44. void setEpsilon( float eps ) { _epsilon = eps; }
  45. /// Get the acceleration epsilon
  46. float getEpsilon() const { return _epsilon; }
  47. /// Set max radius between the center and the particle
  48. void setMaxRadius( float max ) { _maxRadius = max; }
  49. /// Get max radius between the center and the particle
  50. float getMaxRadius() const { return _maxRadius; }
  51. /// Apply the acceleration to a particle. Do not call this method manually.
  52. inline void operate( Particle* P, double dt );
  53. /// Perform some initializations. Do not call this method manually.
  54. inline void beginOperate( Program* prg );
  55. protected:
  56. virtual ~OrbitOperator() {}
  57. OrbitOperator& operator=( const OrbitOperator& ) { return *this; }
  58. osg::Vec3 _center;
  59. osg::Vec3 _xf_center;
  60. float _magnitude;
  61. float _epsilon;
  62. float _maxRadius;
  63. };
  64. // INLINE METHODS
  65. inline void OrbitOperator::operate( Particle* P, double dt )
  66. {
  67. osg::Vec3 dir = _xf_center - P->getPosition();
  68. float length = dir.length();
  69. if ( length<_maxRadius )
  70. {
  71. P->addVelocity( dir * ((_magnitude * dt) /
  72. (length * (_epsilon+length*length))) );
  73. }
  74. }
  75. inline void OrbitOperator::beginOperate( Program* prg )
  76. {
  77. if ( prg->getReferenceFrame()==ModularProgram::RELATIVE_RF )
  78. {
  79. _xf_center = prg->transformLocalToWorld(_center);
  80. }
  81. else
  82. {
  83. _xf_center = _center;
  84. }
  85. }
  86. }
  87. #endif