Game Development Reference
In-Depth Information
Modifying the Physics Class
Next, the Physics class has to be modified to call the native class.
The
ApplyGravityToObjectNative()
function has to be declared as a native function in the
Physics class.
native float ApplyGravityToObjectNative(float YAccel);
The
ApplyGravityToObject()
function calculates the new y component of the acceleration by calling
the native function
ApplyGravityToObjectNative()
with the current y acceleration of the object.
(See Listing 11-10.)
Listing 11-10. Calling the Native Gravity Calculation Function
void ApplyGravityToObject()
{
// Do Native Apply Gravity
float YAccel = m_Acceleration.y;
m_Acceleration.y = ApplyGravityToObjectNative(YAccel);
}
Rotating Objects from Native Code
Demonstrating the rotation of objects from native C code using Java functions requires modifications
to the
hello-jni.c
file, the Physics class, and the MyGLRenderer class.
Modifying the
hello-jni.c
File
The
hello-jni.c
file has to be modified to add the native class
AddRotationNative()
.
The
AddRotationNative()
function is almost identical to the function we discussed in Listing
11-5. The difference is that the full function name involves a different package and class. What
AddRotationNative()
does is add
RotationAngle
degrees to the rotation of the object whose
orientation is represented by the input
Orient
parameter. This is done by calling the actual Java
language method
"AddRotation()"
. (See Listing 11-11.)
Listing 11-11. Adding a Rotation
Java_
com_robsexample_glhelloworld_Physics
_AddRotationNative(JNIEnv* env,
jobject thiz,
jobject Orient,
jfloat RotationAngle)
{
/*
GetObjectClass
jclass GetObjectClass(JNIEnv *env, jobject obj);
*/
jclass OrientationClass = (*env)->GetObjectClass(env, Orient);