Game Development Reference
In-Depth Information
Listing 5-4. Gravity-Related Variables
private boolean m_ApplyGravity = false;
private float m_Gravity = 0.010f;
private float m_GroundLevel = 0;
private boolean m_JustHitGround = false;
private float m_Mass = 100.0f;
The ApplyTranslationalForce() function takes a force vector as an input and converts this
force into a linear acceleration value that is added to the total linear acceleration for the object.
Basically, this function adds a new translational force to the object. The acceleration value is
calculated from Newton's second law, which is F = ma. The acceleration based on this formula is
a = F/m, or linear acceleration is equal to the force applied to the object divided by the mass of
the object. (See Listing 5-5.)
Listing 5-5. Applying the Translational Force
void ApplyTranslationalForce(Vector3 Force)
{
// Apply a force to the object
// F = ma
// F/m = a
// 1. Calculate translational acceleration on object due to new force and add this
// to the current acceleration for this object.
Vector3 a = new Vector3(Force);
if (m_Mass != 0)
{
a.Divide(m_Mass);
}
m_Acceleration.Add(a);
}
The ApplyRotationalForce() function applies a new rotational force to the object. The function
takes a force and the perpendicular length from the application of this force to the object's rotation
axis as input.
The force is converted to angular acceleration using the formula
AngularAcceleration = (Force * r) / Rotational Inertia.
This new angular acceleration is then added to the total angular acceleration to be applied to this
object. The rotational inertia is simplified to a hoop with a radius of 1, so that the rotational inertia is
just the mass of the object. (See Listing 5-6.)
Listing 5-6. Applying Rotation Forces to an Object
void ApplyRotationalForce(float Force, float r)
{
// 1. Torque = r X F;
// T = I * AngularAcceleration;
// T/I = AngularAccleration;
//
 
Search WWH ::




Custom Search