Game Development Reference
In-Depth Information
/*
GetMethodID
jmethodID GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig);
*/
jmethodID MethodID = (*env)->GetMethodID(env, OrientationClass,"AddRotation", "(F)V");
/*
NativeType Call<type>Method(JNIEnv *env, jobject obj, jmethodID methodID, ...);
*/
(*env)->CallVoidMethod(env, Orient, MethodID, RotationAngle);
}
Modifying the Physics Class
The Physics class must also be modified to use the native rotation function.
The
AddRotationNative()
function has to be declared with the native keyword in the Physics class.
native void AddRotationNative(Orientation O, float RotationAngle);
The
AddRotationToObject()
function calls the
AddRotationNative()
function to perform the rotation
on the object and serves as a Java wrapper interface for the native function. (See Listing 11-12.)
Listing 11-12. Wrapper Function for
AddRotationNative()
void AddRotationToObject(Orientation O, float AngleAmount)
{
AddRotationNative(O, AngleAmount);
}
The
UpdatePhysicsObject()
function is modified by adding the Java function
AddRotationToObject()
that calls the native C function that rotates the object as part of the physics update. The old
AddRotation()
function is also commented out. (See Listing 11-13.)
Listing 11-13. Modifying the
UpdatePhysicsObject()
Function
void UpdatePhysicsObject(Orientation orientation)
{
// 0. Apply Gravity if needed
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);