Game Development Reference
In-Depth Information
{
retvalue = limit;
}
else if (value < -limit)
{
retvalue = -limit;
}
return retvalue;
}
The ApplyGravityToObject() function applies the force of the gravitational acceleration to the y
component of the object's acceleration. (See Listing 5-9.)
Listing 5-9. Applying Gravity to an Object
void ApplyGravityToObject()
{
// Apply gravity to object - Assume standard OpenGL axis orientation of positive y being up
m_Acceleration.y = m_Acceleration.y - m_Gravity;
}
The UpdatePhysicsObject() function is the main update function where the position, velocity, and
acceleration of the object is updated, based on the linear and angular accelerations to the object
caused by external forces that have been applied. (See Listing 5-10.)
The function does the following:
1.
Adds the acceleration caused by the force of gravity to the object,
if m_ApplyGravity is true
2.
Updates the linear acceleration of the object and clamps the values to within
the range -m_MaxAcceleration to m_MaxAcceleration . Updates the linear
velocity of the object, based on the linear acceleration, and clamps the value
to within the range -m_MaxVelocity to m_MaxVelocity
3.
Updates the angular acceleration and clamps the value to within the range
-m_MaxAngularAcceleration to m_MaxAngularAcceleration . Updates the
angular velocity, based on the angular acceleration, and clamps the value to
-m_MaxAngularVelocity to m_MaxAngularVelocity
4.
Sets the linear and angular accelerations to 0. All linear and angular
accelerations caused by external forces acting on this object have been
accounted for and processed.
5.
Updates the linear position and takes gravity and the height of the
ground into account, if applicable. If the object has just hit the ground,
m_JustHitGround is set to true. The y component of the object's velocity is
set to 0, and the position of the object is set to the ground level specified by
m_GroundLevel , if the object is below the ground level and falling.
6.
Updates the angular position of the object
 
Search WWH ::




Custom Search