Game Development Reference
In-Depth Information
*/
class ParticleFakeSpring : public ParticleForceGenerator
{
/** The location of the anchored end of the spring. */
Vector3 *anchor;
/** Holds the spring constant. */
real springConstant;
/** Holds the damping on the oscillation of the spring. */
real damping;
public:
/** Creates a new spring with the given parameters. */
ParticleFakeSpring(Vector3 *anchor, real springConstant,
real damping);
/** Applies the spring force to the given particle. */
virtual void updateForce(Particle *particle, real duration);
};
Excerpt from src/pfgen.cpp
void ParticleFakeSpring::updateForce(Particle* particle, real duration)
{
// Check that we do not have infinite mass.
if (!particle->hasFiniteMass()) return;
// Calculate the relative position of the particle to the anchor.
Vector3 position;
particle->getPosition(&position);
position -= *anchor;
// Calculate the constants and check whether they are in bounds.
real gamma = 0.5f * real_sqrt(4 * springConstant - damping*damping);
if (gamma == 0.0f) return;
Vector3 c = position * (damping / (2.0f * gamma)) +
particle->getVelocity() * (1.0f / gamma);
// Calculate the target position.
Vector3 target = position * real_cos(gamma * duration) +
c * real_sin(gamma * duration);
target *= real_exp(-0.5f * duration * damping);
Search Nedrilad ::




Custom Search