Game Development Reference
In-Depth Information
Listing 8-49. Updating the Tank's Physics
void UpdatePhysicsObjectHeading(Vector3 Heading, Orientation orientation)
{
// Adjust for Gravity
if (m_ApplyGravity)
{
ApplyGravityToObject();
}
// 1. Update Linear Velocity
/////////////////////////////////////////////////////////////////////////////
m_Acceleration.x = TestSetLimitValue(m_Acceleration.x, m_MaxAcceleration.x);
m_Acceleration.y = TestSetLimitValue(m_Acceleration.y, m_MaxAcceleration.y);
m_Acceleration.z = TestSetLimitValue(m_Acceleration.z, m_MaxAcceleration.z);
m_Velocity.Add(m_Acceleration);
m_Velocity.x = TestSetLimitValue(m_Velocity.x, m_MaxVelocity.x);
m_Velocity.y = TestSetLimitValue(m_Velocity.y, m_MaxVelocity.y);
m_Velocity.z = TestSetLimitValue(m_Velocity.z, m_MaxVelocity.z);
// 2. Update Angular Velocity
/////////////////////////////////////////////////////////////////////////////////
m_AngularAcceleration = TestSetLimitValue(m_AngularAcceleration, m_MaxAngularAcceleration);
m_AngularVelocity += m_AngularAcceleration;
m_AngularVelocity = TestSetLimitValue(m_AngularVelocity, m_MaxAngularVelocity);
// 3. Reset Forces acting on Object
// Rebuild forces acting on object for each update
////////////////////////////////////////////////////////////////////////////////
m_Acceleration.Clear();
m_AngularAcceleration = 0;
// 4. Adjust Velocity so that all the velocity is redirected along the heading.
//////////////////////////////////////////////////////////////////////////////////
float VelocityMagnitude = m_Velocity.Length();
if (VelocityMagnitude > m_MaxSpeed)
{
VelocityMagnitude = m_MaxSpeed;
}
Vector3 NewVelocity = new Vector3(Heading);
NewVelocity.Normalize();
NewVelocity.Multiply(VelocityMagnitude);
Vector3 OldVelocity = new Vector3(m_Velocity);
if (m_ApplyGravity)
{
m_Velocity.Set(NewVelocity.x, OldVelocity.y, NewVelocity.z);
}
 
Search WWH ::




Custom Search