Game Development Reference
In-Depth Information
Next, add in the m_CubePositionDelta variable to the MyGLRenderer class. This variable holds
the direction and magnitude of the position change that will be applied to the cube each time
onDrawFrame() is called.
The key part of the new code performs the actual position update, the testing of boundaries, and the
change in direction of the m_CubePositionDelta variable.
That code does the following:
1.
Gets the current position of the cube.
2.
Tests the position to see if it is within the z position 4 to -4. If the cube is
outside these boundaries, then the cube's direction is reversed. That is, the
m_CubePositionDelta vector is negated.
The current position vector of the cube is added to the m_CubePositionDelta
vector and then set as the new position of the cube.
3.
Add in this new code that is highlighted in Listing 3-10 and run the program.
Listing 3-12. Adding Code to Move the Cube Along the Z Axis
private Vector3 m_CubePositionDelta = new Vector3(0.0f,0,0.1f);
@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);
m_Camera.UpdateCamera();
// Add Rotation to Cube
//m_Cube.m_Orientation.AddRotation(1);
// Add Translation to Cube
Vector3 Position = m_Cube.m_Orientation.GetPosition();
if ((Position.z > 4) || (Position.z < -4))
{
m_CubePositionDelta.Negate();
}
Vector3 NewPosition = Vector3.Add(Position, m_CubePositionDelta);
Position.Set(NewPosition.x, NewPosition.y, NewPosition.z);
m_Cube.DrawObject(m_Camera, m_PointLight);
}
You should see the android image moving toward and away from you in a loop (see Figure 3-25 ).
Search WWH ::




Custom Search