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