XNA – Rotated Camera

Posted: December 1, 2009 in C#, Game Programming, Programming, XNA
Tags: , ,

xna_logo

What’s a game or an interactive presentation without a way to move around? So in this tutorial, I’ll show you how to make a rotated camera. This is a simple class to make a camera that can rotating about a fixed point rather than origin point (0, 0, 0)

Keyboard Control :

KEY UP – for rotating up

KEY DOWN – for rotating down

KEY RIGHT – for rotating right

KEY LEFT – for rotating left

Note : for using this code in your application, do not forget to change camera position, camera target and camera up vector (as you needed)

public class RotatedCamera
{

    Matrix viewMatrix;
    Matrix projectionMatrix;

    Vector3 camPosition;
    Vector3 camTarget;
    Vector3 camUpVector;

    float viewAngle;
    float aspectRatio;
    float nearPlane;
    float farPlane;

    private float yAngle = 0f;
    private float xAngle = 0f;

    #region Properties

    public Vector3 CamPosition
    {
        get { return camPosition; }
        set { camPosition = value; }
    }

    public Vector3 CamTarget
    {
        get { return camTarget; }
        set { camTarget = value; }
    }

    public Vector3 CamUpVector
    {
        get { return camUpVector; }
        set { camUpVector = value; }
    }

    public Matrix ViewMatrix
    {
        get { return viewMatrix; }
    }

    public Matrix ProjectionMatrix
    {
        get { return projectionMatrix; }
    }

    #endregion

    public RotatedCamera()
    {
    }

    public void Update(GameTime gameTime, KeyboardState keyboardState)
    {
        bool isChanged = false;

        if (keyboardState.IsKeyDown(Keys.Right))
        {
            yAngle -= 0.010f;
            isChanged = true;
        }
        else if (keyboardState.IsKeyDown(Keys.Left))
        {
            yAngle += 0.010f;
            isChanged = true;
        }

        if (keyboardState.IsKeyDown(Keys.Up))
        {
            xAngle -= 0.010f;
            isChanged = true;
        }
        else if (keyboardState.IsKeyDown(Keys.Down))
        {
            xAngle += 0.010f;
            isChanged = true;
        }

        if (xAngle > 0.2f)
            xAngle = 0.2f;
        else if (xAngle < -0.2f)
            xAngle = -0.2f;

        if (isChanged)
            UpdateViewMatrix();
    }

    private void UpdateViewMatrix()
    {
        Matrix rotationMatrix =
            Matrix.CreateTranslation(new Vector3(-camTarget.X, -camTarget.Y, -camTarget.Z)) *
            Matrix.CreateRotationX(3 * xAngle) *
            Matrix.CreateRotationY(3 * yAngle) *
            Matrix.CreateTranslation(camTarget);
        Vector3 transformedVector = Vector3.Transform(camPosition, rotationMatrix);

        viewMatrix = Matrix.CreateLookAt(transformedVector, camTarget, camUpVector);
    }

    public void SetUpCamera(GraphicsDevice device)
    {
        camPosition = new Vector3(350, -700, -500);  // default
        camTarget = new Vector3(350, 0, 300);        // default
        camUpVector = new Vector3(0, -1, 0);         // default

        viewMatrix = Matrix.CreateLookAt(camPosition, camTarget, camUpVector);

        viewAngle = MathHelper.PiOver4;
        aspectRatio = device.Viewport.AspectRatio;
        nearPlane = 5.0f;
        farPlane = 10000.0f;
        projectionMatrix = Matrix.CreatePerspectiveFieldOfView(viewAngle, aspectRatio, nearPlane, farPlane);
    }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s