To test my new site and syntax highlighter I am posting a snippet used inside of the XNA game I am making.
This is a Basic Orthographic Camera Class that is an excellent choice for any 3D game where the view doesn’t really change or any 2.5D game.
public class Camera
{
//View
Vector3 cameraPosition = new Vector3(-12, 10, 0);
Vector3 cameraLookAt = Vector3.Zero;
//Matrix
public Matrix Projection { get; private set; }
public Matrix View { get; private set; }
public Camera(int windowWidth, int windowHeight)
{
Projection = Matrix.CreateOrthographic(windowWidth, windowHeight, -1000f, 1000f);
View = Matrix.CreateLookAt(cameraPosition, cameraLookAt, Vector3.Zero);
}
public void HandleInput(KeyboardState currentKeyboardState)
{
int x = 2;
#region camera
if (currentKeyboardState.IsKeyDown(Keys.Left))
{
if (currentKeyboardState.IsKeyDown(Keys.LeftControl))
cameraLookAt.X += x;
else if (currentKeyboardState.IsKeyDown(Keys.LeftShift))
cameraLookAt.Z += x;
else
cameraLookAt.Y += x;
}
if (currentKeyboardState.IsKeyDown(Keys.Right))
{
if (currentKeyboardState.IsKeyDown(Keys.LeftControl))
cameraLookAt.X -= x;
else if (currentKeyboardState.IsKeyDown(Keys.LeftShift))
cameraLookAt.Z -= x;
else
cameraLookAt.Y -= x;
}
if (currentKeyboardState.IsKeyDown(Keys.Up))
{
if (currentKeyboardState.IsKeyDown(Keys.LeftControl))
cameraPosition.Y -= x; //Zoom in
else if (currentKeyboardState.IsKeyDown(Keys.LeftShift))
cameraPosition.Z += x;
else
cameraPosition.X += x; //UP
}
if (currentKeyboardState.IsKeyDown(Keys.Down))
{
if (currentKeyboardState.IsKeyDown(Keys.LeftControl))
cameraPosition.Y += x; //Zoom out;
else if (currentKeyboardState.IsKeyDown(Keys.LeftShift))
cameraPosition.Z -= x;
else
cameraPosition.X -= x; //Down
}
View = Matrix.CreateLookAt(
cameraPosition,
cameraLookAt,
Vector3.Up);
#endregion
}
}