Game Development Reference
In-Depth Information
Be careful : The direction of the force is expected in world coordinates, whereas the
application point is expected in object coordinates! This matches the way that these
calculations are normally performed, but you could create yet another version that
transforms both the force and the position into world coordinates. In this case, be
careful with the transformation of the force: it should be rotated only; it shouldn't be
transformed by the full 4
×
3 matrix (which adds the offset position to the vector).
10.3.1
F ORCE G ENERATORS
We need to update the force generators we created for particles to work with rigid
bodies. In particular they may have to be able to apply a force at a specific point on
the rigid body. If this isn't at the center of mass, we'll be generating both a force and a
torque for our rigid body, as we saw in the previous section.
This is the logic of not having a force generator return just a single force vector: we
won't know where the force is applied. Instead we allow the force generator to apply a
force in whatever way it wants. We can create force generators that call the method to
apply a force at a point other than the body's center of mass, or they may just apply a
force to the center of mass.
This means that the gravity force generator is almost the same. It is changed only
to accept the rigid-body type rather than a particle:
Excerpt from include/cyclone/fgen.h
/**
* A force generator that applies a gravitational force. One instance
* can be used for multiple rigid bodies.
*/
class Gravity : public ForceGenerator
{
/** Holds the acceleration due to gravity. */
Vector3 gravity;
public:
/** Creates the generator with the given acceleration. */
Gravity(const Vector3 &gravity);
/** Applies the gravitational force to the given rigid body. */
virtual void updateForce(RigidBody *body, real duration);
};
Excerpt from src/fgen.cpp
void Gravity::updateForce(RigidBody* body, real duration)
{
// Check that we do not have infinite mass