AccelOperator 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_ACCELOPERATOR
  15. #define OSGPARTICLE_ACCELOPERATOR 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 class that applies a constant acceleration to the particles.
  25. */
  26. class AccelOperator: public Operator {
  27. public:
  28. inline AccelOperator();
  29. inline AccelOperator(const AccelOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  30. META_Object(osgParticle, AccelOperator);
  31. /// Get the acceleration vector.
  32. inline const osg::Vec3& getAcceleration() const;
  33. /// Set the acceleration vector.
  34. inline void setAcceleration(const osg::Vec3& v);
  35. /** Quickly set the acceleration vector to the gravity on earth (0, 0, -9.81).
  36. The acceleration will be multiplied by the <CODE>scale</CODE> parameter.
  37. */
  38. inline void setToGravity(float scale = 1);
  39. /// Apply the acceleration to a particle. Do not call this method manually.
  40. inline void operate(Particle* P, double dt);
  41. /// Perform some initializations. Do not call this method manually.
  42. inline void beginOperate(Program *prg);
  43. protected:
  44. virtual ~AccelOperator() {}
  45. AccelOperator &operator=(const AccelOperator &) { return *this; }
  46. private:
  47. osg::Vec3 _accel;
  48. osg::Vec3 _xf_accel;
  49. };
  50. // INLINE FUNCTIONS
  51. inline AccelOperator::AccelOperator()
  52. : Operator(), _accel(0, 0, 0)
  53. {
  54. }
  55. inline AccelOperator::AccelOperator(const AccelOperator& copy, const osg::CopyOp& copyop)
  56. : Operator(copy, copyop), _accel(copy._accel)
  57. {
  58. }
  59. inline const osg::Vec3& AccelOperator::getAcceleration() const
  60. {
  61. return _accel;
  62. }
  63. inline void AccelOperator::setAcceleration(const osg::Vec3& v)
  64. {
  65. _accel = v;
  66. }
  67. inline void AccelOperator::setToGravity(float scale)
  68. {
  69. _accel.set(0, 0, -9.80665f * scale);
  70. }
  71. inline void AccelOperator::operate(Particle* P, double dt)
  72. {
  73. P->addVelocity(_xf_accel * dt);
  74. }
  75. inline void AccelOperator::beginOperate(Program *prg)
  76. {
  77. if (prg->getReferenceFrame() == ModularProgram::RELATIVE_RF) {
  78. _xf_accel = prg->rotateLocalToWorld(_accel);
  79. } else {
  80. _xf_accel = _accel;
  81. }
  82. }
  83. }
  84. #endif