Skip to content

Instantly share code, notes, and snippets.

@johdah
Last active December 11, 2015 05:59
Show Gist options
  • Save johdah/4556192 to your computer and use it in GitHub Desktop.
Save johdah/4556192 to your computer and use it in GitHub Desktop.
A Simple XNA Camera
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace XNAGists
{
public class Camera
{
#region Fields
private float aspectRatio;
protected Matrix projection;
protected Matrix view;
private Vector3 cameraPosition = new Vector3(0.0f, 0.0f, 15f);
private Vector3 cameraTarget = Vector3.Zero;
private Vector3 cameraUpVector = Vector3.Up;
#endregion
#region Properties
public Matrix Projection
{
get { return this.projection; }
}
public Matrix View
{
get { return this.view; }
}
#endregion
public SimpleCamera(float aspectRatio)
{
Matrix.CreatePerspectiveFieldOfView(
MathHelper.PiOver4,
aspectRatio,
0.1f,
1000.0f,
out projection);
Matrix.CreateLookAt(
ref cameraPosition,
ref cameraTarget,
ref cameraUpVector,
out view);
}
public void CreateLookAt(Vector3 pos, Vector3 target, Vector3 upVector) {
this.cameraPosition = pos;
this.cameraTarget = target;
this.cameraUpVector = upVector;
Matrix.CreateLookAt(
ref cameraPosition,
ref cameraTarget,
ref cameraUpVector,
out view);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment