Game Development Reference
virtual void updateForce(RigidBody *body, real duration);
Excerpt from src/fgen.cpp
void Spring::updateForce(RigidBody* body, real duration)
// Calculate the two ends in world space.
Vector3 lws = body->getPointInWorldSpace(connectionPoint);
Vector3 ows = other->getPointInWorldSpace(otherConnectionPoint);
// Calculate the vector of the spring.
Vector3 force = lws - ows;
// Calculate the magnitude of the force.
real magnitude = force.magnitude();
magnitude = real_abs(magnitude - restLength);
magnitude *= springConstant;
// Calculate the final force and apply it.
force *= -magnitude;
To r q u e G e n e r a t o r s
We could follow the lead of the force generators and create a set of torque generators.
They fit into the same force generator structure we've used so far: calling the rigid
body's addTorque method.
You can use this to constantly drive a rotating object, such as a set of fan blades or
the wheels of a car.
T HE R IGID -B ODY I NTEGRATION
So, we're finally in the position to write the integration routine that will update the
position and orientation of a rigid body based on its forces and torques. It will have
the same format as the integration for a particle, with the rotation components added.
To correspond with the linear case, we add an additional data member to the rigid
body to control angular velocity damping—the amount of angular velocity the body
loses each second:
Excerpt from include/cyclone/body.h