Here’s the full code for my Camera:
public class Camera
{
private GraphicsDevice graphicsDevice = null;
private Vector3 position = new Vector3(0f, 1f, 10f);
private Vector3 lookDir = Vector3.Forward;
private float yAngle = 0f;
private float xAngle = 0f;
private MouseState mState = default(MouseState);
private KeyboardState kbState = default(KeyboardState);
private GameWindow gameWindow = null;
private float unitsPerSecond = 5;
private float anglesPerSecond = 3f;
public float FieldOfView = MathHelper.PiOver4;
public float NearClipPlane = .1f;
public float FarClipPlane = 200f;
private bool Angling = false;
private Matrix viewMatrix = Matrix.Identity;
public Matrix ViewMatrix
{
get
{
//Adding the position to the forward matrix causes it to not change the direction it's facing when translating
Vector3 lookForward = lookDir + position;
//Create a look at Matrix
viewMatrix = Matrix.CreateLookAt(position, lookForward, Vector3.Up);
return viewMatrix;
}
}
public Matrix ProjectionMatrix
{
get
{
float aspectRatio = graphicsDevice.Viewport.Width / (float)graphicsDevice.Viewport.Height;
return Matrix.CreatePerspectiveFieldOfView(FieldOfView, aspectRatio, NearClipPlane, FarClipPlane);
}
}
public Camera(GraphicsDevice gfxDevice, GameWindow window)
{
graphicsDevice = gfxDevice;
gameWindow = window;
}
public void Update(GameTime gameTime)
{
Console.WriteLine(viewMatrix.Forward);
MouseState state = Mouse.GetState(gameWindow);
KeyboardState kstate = Keyboard.GetState();
if (kstate.IsKeyDown(Keys.Space) && kbState.IsKeyDown(Keys.Space) == false)
{
//Reset everything
position = new Vector3(0f, 1f, 10f);
lookDir = Vector3.Forward;
yAngle = 0f;
xAngle = 0f;
}
if (kstate.IsKeyDown(Keys.W))
{
Vector3 forward = viewMatrix.Forward;
forward.Z = -forward.Z;
position -= (forward * unitsPerSecond) * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
else if (kstate.IsKeyDown(Keys.S) == true)
{
Vector3 forward = viewMatrix.Forward;
forward.Z = -forward.Z;
position += (forward * unitsPerSecond) * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (kstate.IsKeyDown(Keys.A) == true)
{
Vector3 right = viewMatrix.Right;
right.Z = -right.Z;
right.Y = 0f;
position -= (right * unitsPerSecond) * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
else if (kstate.IsKeyDown(Keys.D) == true)
{
Vector3 right = viewMatrix.Right;
right.Z = -right.Z;
right.Y = 0f;
position += (right * unitsPerSecond) * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (kstate.IsKeyDown(Keys.Q) == true)
{
position.Y -= (unitsPerSecond) * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
else if (kstate.IsKeyDown(Keys.E) == true)
{
position.Y += (unitsPerSecond) * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (state.RightButton == ButtonState.Pressed)
{
if (Angling == false)
{
Angling = true;
}
else
{
Vector2 diff = state.Position.ToVector2() - mState.Position.ToVector2();
if (diff.X != 0f)
{
float diff2 = -(diff.X * anglesPerSecond) * (float)gameTime.ElapsedGameTime.TotalSeconds;
Matrix matrix = Matrix.CreateRotationY(MathHelper.ToRadians(diff2));
lookDir = Vector3.Transform(lookDir, matrix);
}
if (diff.Y != 0f)
{
float diff2 = -(diff.Y * anglesPerSecond) * (float)gameTime.ElapsedGameTime.TotalSeconds;
Matrix matrix = Matrix.CreateRotationX(MathHelper.ToRadians(diff2));
lookDir = Vector3.Transform(lookDir, matrix);
}
}
}
else
{
Angling = false;
}
mState = state;
kbState = kstate;
}
}