2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2003-01-22 00:45:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2003-01-22 00:45:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2002-06-05 20:44:55 +08:00
|
|
|
//osgParticle - Copyright (C) 2002 Marco Jez
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
#ifndef OSGPARTICLE_FORCEOPERATOR
|
|
|
|
#define OSGPARTICLE_FORCEOPERATOR 1
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
#include <osgParticle/ModularProgram>
|
|
|
|
#include <osgParticle/Operator>
|
|
|
|
#include <osgParticle/Particle>
|
|
|
|
|
|
|
|
#include <osg/CopyOp>
|
|
|
|
#include <osg/Object>
|
|
|
|
#include <osg/Vec3>
|
|
|
|
|
|
|
|
namespace osgParticle
|
|
|
|
{
|
|
|
|
|
2005-05-21 05:01:57 +08:00
|
|
|
/** An operator that applies a constant force to the particles.
|
|
|
|
* Remember that if the mass of particles is expressed in kg and the lengths are
|
|
|
|
* expressed in meters, then the force should be expressed in Newtons.
|
2002-06-05 20:44:55 +08:00
|
|
|
*/
|
|
|
|
class ForceOperator: public Operator {
|
|
|
|
public:
|
|
|
|
inline ForceOperator();
|
2005-04-29 17:47:57 +08:00
|
|
|
inline ForceOperator(const ForceOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-06 21:25:36 +08:00
|
|
|
META_Object(osgParticle, ForceOperator);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
/// Get the force vector.
|
2005-04-29 17:47:57 +08:00
|
|
|
inline const osg::Vec3& getForce() const;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
/// Set the force vector.
|
2005-04-29 17:47:57 +08:00
|
|
|
inline void setForce(const osg::Vec3& f);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
/// Apply the force to a particle. Do not call this method manually.
|
2005-04-29 17:47:57 +08:00
|
|
|
inline void operate(Particle* P, double dt);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
/// Perform some initialization. Do not call this method manually.
|
|
|
|
inline void beginOperate(Program *prg);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
protected:
|
|
|
|
virtual ~ForceOperator() {};
|
2012-03-22 01:36:20 +08:00
|
|
|
ForceOperator& operator=(const ForceOperator&) { return *this; }
|
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
private:
|
2005-04-29 17:47:57 +08:00
|
|
|
osg::Vec3 _force;
|
|
|
|
osg::Vec3 _xf_force;
|
2002-06-05 20:44:55 +08:00
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
// INLINE FUNCTIONS
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
inline ForceOperator::ForceOperator()
|
2005-04-29 17:47:57 +08:00
|
|
|
: Operator(), _force(0, 0, 0)
|
2012-03-22 01:36:20 +08:00
|
|
|
{
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline ForceOperator::ForceOperator(const ForceOperator& copy, const osg::CopyOp& copyop)
|
|
|
|
: Operator(copy, copyop), _force(copy._force)
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline const osg::Vec3& ForceOperator::getForce() const
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
return _force;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline void ForceOperator::setForce(const osg::Vec3& v)
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
_force = v;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline void ForceOperator::operate(Particle* P, double dt)
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
P->addVelocity(_xf_force * (P->getMassInv() * dt));
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
inline void ForceOperator::beginOperate(Program *prg)
|
|
|
|
{
|
2004-10-25 04:04:00 +08:00
|
|
|
if (prg->getReferenceFrame() == ModularProgram::RELATIVE_RF) {
|
2005-04-29 17:47:57 +08:00
|
|
|
_xf_force = prg->rotateLocalToWorld(_force);
|
2002-06-05 20:44:55 +08:00
|
|
|
} else {
|
2005-04-29 17:47:57 +08:00
|
|
|
_xf_force = _force;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|