Game Development Reference
In-Depth Information
The generator can be implemented like this:
Excerpt from include/cyclone/pfgen.h
/**
* A force generator that applies a spring force only
* when extended.
*/
class ParticleBungee : public ParticleForceGenerator
{
/** The particle at the other end of the spring. */
Particle *other;
/** Holds the spring constant. */
real springConstant;
/**
* Holds the length of the bungee at the point it begins to
* generate a force.
*/
real restLength;
public:
/** Creates a new bungee with the given parameters. */
ParticleBungee(Particle *other,
real springConstant, real restLength);
/** Applies the spring force to the given particle. */
virtual void updateForce(Particle *particle, real duration);
};
Excerpt from src/pfgen.cpp
void ParticleBungee::updateForce(Particle* particle, real duration)
{
// Calculate the vector of the spring.
Vector3 force;
particle->getPosition(&force);
force -= other->getPosition();
// Check if the bungee is compressed.
real magnitude = force.magnitude();
if (magnitude <= restLength) return;
// Calculate the magnitude of the force.
Search Nedrilad ::




Custom Search