Time for action – expanding the Camera
1. In the
Helper Methods
region of theCamera
class, add the following new methods:public Vector3 PreviewMove(float scale) { Matrix rotate = Matrix.CreateRotationY(rotation); Vector3 forward = new Vector3(0, 0, scale); forward = Vector3.Transform(forward, rotate); return (position + forward); } public void MoveForward(float scale) { MoveTo(PreviewMove(scale), rotation); }
What just happened?
PreviewMove()
accepts a distance we wish to move along the direction that the camera is facing. It then calculates a matrix which is used to rotate a vector by the current camera rotation. Recall that an unrotated camera will always be pointing in the 0, 0, 1 direction, so we replace the 1 in this vector with the distance we wish to move, creating vector forward
. We then apply the rotate transform to this vector, resulting in a vector that points in the direction the camera is actually facing, with a length equal to the distance we want to...