Game Development Reference
In-Depth Information
The CalculateCollisionImpulseNative() function calculates the collision reaction force from two
objects colliding with each other and returns the value. (See Listing 11-16.)
Listing 11-16. Calculating the Reaction Force for a Collision
jfloat
Java_com_robsexample_glhelloworld_Physics_CalculateCollisionImpulseNative(JNIEnv* env,
jobject thiz,
jfloat CoefficientOfRestitution,
jfloat Mass1,
jfloat Mass2,
jfloat RelativeVelocityX,
jfloat RelativeVelocityY,
jfloat RelativeVelocityZ,
jfloat CollisionNormalX,
jfloat CollisionNormalY,
jfloat CollisionNormalZ)
{
// 1. Calculate the impulse along the line of action of the Collision Normal
//float Impulse = (-(1+CoefficientOfRestitution) * (RelativeVelocity.
DotProduct(CollisionNormal))) /
// (1/Mass1 + 1/Mass2);
float RelativeVelocityDotCollisionNormal = DotProduct(RelativeVelocityX, RelativeVelocityY,
RelativeVelocityZ, CollisionNormalX, CollisionNormalY, CollisionNormalZ);
float Impulse = (-(1+CoefficientOfRestitution) *
RelativeVelocityDotCollisionNormal)/(1/Mass1 + 1/Mass2);
return Impulse;
}
Modifying the Physics Class
The Physics class must also be modified to implement the reaction force calculation.
The CalculateCollisionImpulseNative() function has to be declared as native in order to be used.
native float CalculateCollisionImpulseNative(float CoefficientOfRestitution,
float Mass1,float Mass2,
float RelativeVelocityX, float RelativeVelocityY, float
RelativeVelocityZ,
float CollisionNormalX, float CollisionNormalY, float
CollisionNormalZ);
The ApplyLinearImpulse() function has to be modified so that it calls the
CalculateCollisionImpulseNative() function to calculate the reaction force of the collision. The
existing calculation for the reaction force is commented out. (See Listing 11-17.)
 
Search WWH ::




Custom Search