Game Development Reference
In-Depth Information
m_Orientation.GetPosition(),
m_Orientation.GetRotationAxis(),
m_Orientation.GetScale());
}
}
Modifying the MyGLRenderer Class
The MyGLRenderer class must also be modified, because the final program will consist of a cube
that is affected by gravity. A linear upward force is applied to the cube, and a rotational force is
applied to the cube when it hits the ground. The net result is that the cube appears to jump when it
hits the ground and starts spinning faster and faster.
The variable
m_Force1
is the linear force that is applied to the cube when it hits the ground level.
private Vector3 m_Force1 = new Vector3(0,20,0);
The
m_RotationalForce
variable is the rotational force that is applied to the cube every time it hits
the ground.
private float m_RotationalForce = 3;
In the
CreateCube()
function,
Cube.CubeData4Sided
is used to provide texture mapping on four sides
of the cube instead of two.
MeshEx CubeMesh = new MeshEx(8,0,3,5,Cube.CubeData4Sided, Cube.CubeDrawOrder);
The gravity for
m_Cube
is set to true, so that the cube will fall until it hits the ground level.
m_Cube.GetObjectPhysics().SetGravity(true);
In the
onDrawFrame()
function (see Listing 5-14), new code in bold has been added, which
1.
Updates the cube physics by calling
UpdateObject3d()
2.
Tests if the cube has just hit the ground by calling
GetHitGroundStatus()
3.
Applies, the upward translational force
m_Force1
and the rotational force
m_RotationalForce
to the cube, if the cube has just hit the ground
4.
Resets the just-hit-ground status
This is the key code that creates the bouncing/rotating cube effect. (See Figure
5-16
.)
Listing 5-14. Modifying the
onDrawFrame()
Function
@Override
public void onDrawFrame(GL10 unused)
{
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);