ForceOperator 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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. //osgParticle - Copyright (C) 2002 Marco Jez
  14. #ifndef OSGPARTICLE_FORCEOPERATOR
  15. #define OSGPARTICLE_FORCEOPERATOR 1
  16. #include <osgParticle/ModularProgram>
  17. #include <osgParticle/Operator>
  18. #include <osgParticle/Particle>
  19. #include <osg/CopyOp>
  20. #include <osg/Object>
  21. #include <osg/Vec3>
  22. namespace osgParticle
  23. {
  24. /** An operator that applies a constant force to the particles.
  25. * Remember that if the mass of particles is expressed in kg and the lengths are
  26. * expressed in meters, then the force should be expressed in Newtons.
  27. */
  28. class ForceOperator: public Operator {
  29. public:
  30. inline ForceOperator();
  31. inline ForceOperator(const ForceOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  32. META_Object(osgParticle, ForceOperator);
  33. /// Get the force vector.
  34. inline const osg::Vec3& getForce() const;
  35. /// Set the force vector.
  36. inline void setForce(const osg::Vec3& f);
  37. /// Apply the force to a particle. Do not call this method manually.
  38. inline void operate(Particle* P, double dt);
  39. /// Perform some initialization. Do not call this method manually.
  40. inline void beginOperate(Program *prg);
  41. protected:
  42. virtual ~ForceOperator() {};
  43. ForceOperator& operator=(const ForceOperator&) { return *this; }
  44. private:
  45. osg::Vec3 _force;
  46. osg::Vec3 _xf_force;
  47. };
  48. // INLINE FUNCTIONS
  49. inline ForceOperator::ForceOperator()
  50. : Operator(), _force(0, 0, 0)
  51. {
  52. }
  53. inline ForceOperator::ForceOperator(const ForceOperator& copy, const osg::CopyOp& copyop)
  54. : Operator(copy, copyop), _force(copy._force)
  55. {
  56. }
  57. inline const osg::Vec3& ForceOperator::getForce() const
  58. {
  59. return _force;
  60. }
  61. inline void ForceOperator::setForce(const osg::Vec3& v)
  62. {
  63. _force = v;
  64. }
  65. inline void ForceOperator::operate(Particle* P, double dt)
  66. {
  67. P->addVelocity(_xf_force * (P->getMassInv() * dt));
  68. }
  69. inline void ForceOperator::beginOperate(Program *prg)
  70. {
  71. if (prg->getReferenceFrame() == ModularProgram::RELATIVE_RF) {
  72. _xf_force = prg->rotateLocalToWorld(_force);
  73. } else {
  74. _xf_force = _force;
  75. }
  76. }
  77. }
  78. #endif