FluidFrictionOperator 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_FLUIDFRICTIONOPERATOR
  15. #define OSGPARTICLE_FLUIDFRICTIONOPERATOR 1
  16. #include <osgParticle/Export>
  17. #include <osgParticle/Operator>
  18. #include <osg/CopyOp>
  19. #include <osg/Object>
  20. #include <osg/Math>
  21. namespace osgParticle
  22. {
  23. class Particle;
  24. /** An operator that simulates the friction of a fluid.
  25. By using this operator you can let the particles move in a fluid of a given <I>density</I>
  26. and <I>viscosity</I>. There are two functions to quickly setup the parameters for pure water
  27. and air. You can decide whether to compute the forces using the particle's physical
  28. radius or another value, by calling the <CODE>setOverrideRadius()</CODE> method.
  29. */
  30. class OSGPARTICLE_EXPORT FluidFrictionOperator: public Operator {
  31. public:
  32. FluidFrictionOperator();
  33. FluidFrictionOperator(const FluidFrictionOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  34. META_Object(osgParticle, FluidFrictionOperator);
  35. /// Set the density of the fluid.
  36. inline void setFluidDensity(float d);
  37. /// Get the density of the fluid.
  38. inline float getFluidDensity() const;
  39. /// Set the viscosity of the fluid.
  40. inline void setFluidViscosity(float v);
  41. /// Get the viscosity of the fluid.
  42. inline float getFluidViscosity() const;
  43. /// Set the wind vector.
  44. inline void setWind(const osg::Vec3& wind) { _wind = wind; }
  45. /// Get the wind vector.
  46. inline const osg::Vec3& getWind() const { return _wind; }
  47. /// Set the overriden radius value (pass 0 if you want to use particle's radius).
  48. inline void setOverrideRadius(float r);
  49. /// Get the overriden radius value.
  50. inline float getOverrideRadius() const;
  51. /// Set the fluid parameters as for air (20°C temperature).
  52. inline void setFluidToAir();
  53. /// Set the fluid parameters as for pure water (20°C temperature).
  54. inline void setFluidToWater();
  55. /// Apply the friction forces to a particle. Do not call this method manually.
  56. void operate(Particle* P, double dt);
  57. /// Perform some initializations. Do not call this method manually.
  58. inline void beginOperate(Program* prg);
  59. protected:
  60. virtual ~FluidFrictionOperator() {}
  61. FluidFrictionOperator &operator=(const FluidFrictionOperator &) { return *this; }
  62. private:
  63. float _coeff_A;
  64. float _coeff_B;
  65. float _density;
  66. float _viscosity;
  67. float _ovr_rad;
  68. osg::Vec3 _wind;
  69. Program* _current_program;
  70. };
  71. // INLINE FUNCTIONS
  72. inline float FluidFrictionOperator::getFluidDensity() const
  73. {
  74. return _density;
  75. }
  76. inline float FluidFrictionOperator::getFluidViscosity() const
  77. {
  78. return _viscosity;
  79. }
  80. inline void FluidFrictionOperator::setFluidDensity(float d)
  81. {
  82. _density = d;
  83. _coeff_B = 0.2f * osg::PI * _density;
  84. }
  85. inline void FluidFrictionOperator::setFluidViscosity(float v)
  86. {
  87. _viscosity = v;
  88. _coeff_A = 6 * osg::PI * _viscosity;
  89. }
  90. inline void FluidFrictionOperator::setFluidToAir()
  91. {
  92. setFluidViscosity(1.8e-5f);
  93. setFluidDensity(1.2929f);
  94. }
  95. inline void FluidFrictionOperator::setFluidToWater()
  96. {
  97. setFluidViscosity(1.002e-3f);
  98. setFluidDensity(1.0f);
  99. }
  100. inline float FluidFrictionOperator::getOverrideRadius() const
  101. {
  102. return _ovr_rad;
  103. }
  104. inline void FluidFrictionOperator::setOverrideRadius(float r)
  105. {
  106. _ovr_rad = r;
  107. }
  108. inline void FluidFrictionOperator::beginOperate(Program* prg)
  109. {
  110. _current_program = prg;
  111. }
  112. }
  113. #endif