Game Development Reference
In-Depth Information
Modifying the Physics Class
Next, we have to add code, to process a collision.
The ApplyLinearImpulse() function (see Listing 5-19) in the Physics class actually implements the
collision action and reaction forces. This function has three main components, which
1.
Calculate the force generated by the collision along the collision normal of the
two objects.
2.
Find the vector form of the action force, by taking that magnitude of the
collision force found in step 1 and putting this along the collision normal
between the objects. The reaction force is found by taking the negative of the
action force.
3.
Add the forces acting on both objects to each of the objects, by using the
ApplyTranslationalForce() function.
Listing 5-19. The ApplyLinearImpulse() Function
void ApplyLinearImpulse(Object3d body1, Object3d body2)
{
float m_Impulse;
// 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()));
// 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);
}
Hands-on Example: Collisions
In this exercise, we will create another cube above the cube created in the previous hands-on
example. This cube will fall and collide with the first cube. The net effect will be to have two cubes
continuously colliding with each other.
Modifying the MyGLRenderer Class
We need to make some modifications to the MyGLRenderer class in our project. The modifications
involve adding code to create a second cube and adding code to process the collision between this
new cube and old cube from the previous hands-on example.
 
Search WWH ::




Custom Search