Simple Camera Movement

From JPCT
Revision as of 23:39, 1 February 2013 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Camera movement is a very important topic that should be covered when starting to learn jPCT. In the Hello World example we made a cube rotate around its axis when we touched the screen. Now we will be doing almost the same thing, but instead we will move the camera around the cube. We will accomplish this by creating a transformation matrix and applying it to our camera.

We will only be changing the onDrawFrame function to get this result, and the rest of the code from the Hello World example will remain the same.(You may have to change some variables to be global)

In order to move the camera around the cube we will first have to translate back to the origin. As you can see below, we are getting the transformed center of the cube and creating a new Simple Vector to hold this position. This is the original translation that occurred to move the object to its current position. So to translate back to the origin we simply find the product of the transformed center and -1.0f.

Now that we have translated back to the origin we can rotate the camera around the Y axis. I simply use the value that is touchTurn as it will allow us to rotate the object in either direction. Finally we translate back to the objects starting translation.

public void onDrawFrame(GL10 gl) {

	if (touchTurn != 0) {
		SimpleVector backVect = cube.getTransformedCenter();
		backVect.scalarMul(-1.0f);
		rotationmatrix.translate(backVect);
		rotationmatrix.rotateY(touchTurn);
		rotationmatrix.translate(cube.getTransformedCenter());
		touchTurn = 0;
	}

As shown above we now have a matrix that represents the rotation around the cube’s Y axis. To do a translation we simply translate the matrix with a new Simple Vector. I have chosen to move my camera in the Y axis and have given it a speed of 30. Again the touchTurnUp value will determine which direction the camera should move in.

	if (touchTurnUp != 0) {
		transformMatrix.translate(new SimpleVector(0, -touchTurnUp * 30,0));
		touchTurnUp = 0;
	}

Finally we can apply this transformation matrix to the camera position. We set the new camera position to be the product of the old position and the transformation matrix.

	camPos.matMul(transformMatrix);
	cam.setPosition(camPos);
	cam.lookAt(cube.getTransformedCenter());

This last part is to set up the orientation of the Camera. This needs to be set if you want to alter the Up Vector. The Up vector is the vector that tells that camera which way is up. The Hello World example has the up vector pointing downwards, so I multiply the current Up Vector by -1 to make it face in the positive direction.

	SimpleVector upVector = cam.getUpVector();
	upVector.scalarMul(-1.0f);
	cam.setOrientation(cam.getDirection(), upVector);

Lastly we make the transformation matrix an identity matrix, so that the matrix is only applied once to the camera.

	transformMatrix.setIdentity();
			
	fb.clear(back);
	world.renderScene(fb);
	world.draw(fb);
	fb.display();

	
}