Test Your Knowledge: Exercise

  1. Remember that to move a 3D camera forward, you use the following code:

    cameraPosition += cameraDirection * speed;

    There is a small problem with this method, however, when it's applied to land-based cameras. Essentially, to eliminate the ability to "fly" when looking up, you remove the Y component of the cameraDirection vector prior to moving the camera with this line of code. This causes your camera to stay at the same Y value, which keeps the camera on the ground.

    However, what happens as a result is that the higher you pitch your camera, the slower you end up moving. For example, if your cameraDirection vector was (10, 2, 0) when you removed the Y component, you would end up with the vector (10, 0, 0) and you'd move at that speed. If your camera was pitched at a larger angle and your cameraDirection vector was (2, 10, 0), the resulting vector after removing the Y component would be (2, 0, 0) and you'd move forward at that speed.

    Convert your 3D Flying Camera solution to a land-based camera and solve this problem so that when moving forward and backward your camera moves at the same speed regardless of the pitch angle. Use the code you created in the first half of this chapter.

    Hint: remember that Vector3.Normalize will take any Vector3 and give it a magnitude or length of 1.

Get Learning XNA 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.