1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
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.
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.
13// Written by Wang Rui, (C) 2010
15#ifndef OSGPARTICLE_ORBITOPERATOR
16#define OSGPARTICLE_ORBITOPERATOR
18#include <osgParticle/ModularProgram>
19#include <osgParticle/Operator>
20#include <osgParticle/Particle>
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)
29class OrbitOperator : public Operator
33 : Operator(), _magnitude(1.0f), _epsilon(1e-3), _maxRadius(FLT_MAX)
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)
41 META_Object( osgParticle, OrbitOperator );
43 /// Set the center of orbit
44 void setCenter( const osg::Vec3& c ) { _center = c; }
46 /// Get the center of orbit
47 const osg::Vec3& getCenter() const { return _center; }
49 /// Set the acceleration scale
50 void setMagnitude( float mag ) { _magnitude = mag; }
52 /// Get the acceleration scale
53 float getMagnitude() const { return _magnitude; }
55 /// Set the acceleration epsilon
56 void setEpsilon( float eps ) { _epsilon = eps; }
58 /// Get the acceleration epsilon
59 float getEpsilon() const { return _epsilon; }
61 /// Set max radius between the center and the particle
62 void setMaxRadius( float max ) { _maxRadius = max; }
64 /// Get max radius between the center and the particle
65 float getMaxRadius() const { return _maxRadius; }
67 /// Apply the acceleration to a particle. Do not call this method manually.
68 inline void operate( Particle* P, double dt );
70 /// Perform some initializations. Do not call this method manually.
71 inline void beginOperate( Program* prg );
74 virtual ~OrbitOperator() {}
75 OrbitOperator& operator=( const OrbitOperator& ) { return *this; }
86inline void OrbitOperator::operate( Particle* P, double dt )
88 osg::Vec3 dir = _xf_center - P->getPosition();
89 float length = dir.length();
90 if ( length<_maxRadius )
92 P->addVelocity( dir * ((_magnitude * dt) /
93 (length * (_epsilon+length*length))) );
97inline void OrbitOperator::beginOperate( Program* prg )
99 if ( prg->getReferenceFrame()==ModularProgram::RELATIVE_RF )
101 _xf_center = prg->transformLocalToWorld(_center);
105 _xf_center = _center;