Monday, June 7, 2010

The Camera Class

This is the Camera Class you will need for the Demo


Note that the full visual studio project can be downloaded from source forge
https://sourceforge.net/projects/aphysicsengine/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using PhysicsEngine;

namespace WindowsGame1
{
public class Camera
{

public Vector3 cameraPosition, cameraTarget, cameraUpVector;
public float fieldOfView, aspectRatio, nearPlaneDistance, farPlaneDistance;

public Camera(Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector, float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance)
{
this.cameraPosition = cameraPosition;
this.cameraTarget = cameraTarget;
this.cameraUpVector = cameraUpVector;
this.fieldOfView = fieldOfView;
this.aspectRatio = aspectRatio;
this.nearPlaneDistance = nearPlaneDistance;
this.farPlaneDistance = farPlaneDistance;
}

public Matrix View
{
get
{
return Matrix.CreateLookAt(cameraPosition, cameraTarget, cameraUpVector);
}
}

public Matrix Projection
{
get
{
return Matrix.CreatePerspectiveFieldOfView
(
fieldOfView,
aspectRatio,
nearPlaneDistance,
farPlaneDistance
);
}
}

public void Draw(DefaultEntity entity, Model model)
{
Matrix[] boneTransforms = new Matrix[model.Bones.Count];
Matrix rotation = entity.Rotation;
Matrix position = Matrix.CreateTranslation(entity.Position);
model.Root.Transform = Matrix.Identity * rotation * position;
model.CopyAbsoluteBoneTransformsTo(boneTransforms);

foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = boneTransforms[mesh.ParentBone.Index];
effect.View = this.View;
effect.Projection = this.Projection;
}
mesh.Draw();
}
}
}
}

No comments:

Post a Comment