XNA Camera Class

wazzup dawgs
For any of my fellow XNA using peeps, here is a 3D camera class


public class Camera
{
protected Vector3 position, target, up;
protected float fov;

public virtual Vector3 Position { get { return this.position; } set { this.position = value; } }

public virtual Vector3 Target { get { return this.target; } set { this.target = value; } }

public virtual Vector3 Up { get { return this.up; } set { this.up = value; } }

public virtual float FOV { get { return this.fov; } set { this.fov = value; } }

public Camera(Vector3 position, Vector3 target, Vector3 up, float fov)
{
this.Position = position;
this.Target = target;
this.Up = up;
this.FOV = fov;
}

public virtual void rotate(float xdeg, float ydeg)
{
Vector3 direction = (Target - Position);

Matrix yrot = Matrix.CreateFromAxisAngle(-up, MathHelper.ToRadians(ydeg));

Vector3 xaxis = Vector3.Cross(up, direction);

xaxis.Normalize();
xaxis = Vector3.Transform(xaxis, yrot);

Matrix xrot = Matrix.CreateFromAxisAngle(xaxis, MathHelper.ToRadians(xdeg));

direction = Vector3.Transform(direction, yrot);
direction = Vector3.Transform(direction, xrot);

Target = Position + (direction);
}

public virtual void forward(float distance)
{
Vector3 direction = (Target - Position);
direction.Normalize();

this.position += direction * distance;
this.target += direction * distance;
}

public virtual void left(float distance)
{
Vector3 direction = (Target - Position);
direction.Normalize();

Vector3 xaxis = -Vector3.Cross(up, direction);
xaxis.Normalize();

this.position += xaxis * distance;
this.target += xaxis * distance;
}

public virtual void upward(float distance)
{
this.position += this.up * distance;
this.target += this.up * distance;
}

public virtual void move(Vector3 direction)
{
forward(direction.Z);
left(direction.X);
upward(direction.Y);
}
}

Peace out

3

This entry was posted on Friday, August 19th, 2011 at 8:55 pm and is filed under LD #21. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.