Game Development Reference
In-Depth Information
Listing 11-17. Modifying the ApplyLinearImpulse() Function
void ApplyLinearImpulse(Object3d body1, Object3d body2)
{
float m_Impulse = 0;
/*
// 1. Calculate the impulse along the line of action of the Collision Normal
m_Impulse = (-(1+m_CoefficientOfRestitution) *
(m_RelativeVelocity.DotProduct(m_CollisionNormal))) / ((1/body1.GetObjectPhysics().GetMass() +
1/body2.GetObjectPhysics().GetMass()));
*/
m_Impulse = CalculateCollisionImpulseNative(m_CoefficientOfRestitution,
body1.GetObjectPhysics().GetMass(),
body2.GetObjectPhysics().GetMass(),
m_RelativeVelocity.x,
m_RelativeVelocity.y,
m_RelativeVelocity.z,
m_CollisionNormal.x,
m_CollisionNormal.y,
m_CollisionNormal.z);
// 2. Apply Translational Force to bodies
// f = ma;
// f/m = a;
Vector3 Force1 = Vector3.Multiply( m_Impulse, m_CollisionNormal);
Vector3 Force2 = Vector3.Multiply(-m_Impulse, m_CollisionNormal);
body1.GetObjectPhysics().ApplyTranslationalForce(Force1);
body2.GetObjectPhysics().ApplyTranslationalForce(Force2);
}
Run the program. The natively calculated gravity should pull toward the ground objects such as the tank
in Figure 11-11 . The arena objects should rotate as in Figure 11-12 . Collision forces acting on objects
after a collision should work to deflect objects away from one another, as shown in Figure 11-13 .
 
Search WWH ::




Custom Search