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