I currently have a camera class set up and have my model/player loaded but I can't get my camera in the correct position by changing the x,y,z coordinates.
I have set up my "game" with a separate class for the model/player and a class for the camera. I am fairly new to XNA and most of what I have already got is from a tutorial from my lecturer.
Here is my player/model class:
public class Player
{
public Model model;
public Matrix world = Matrix.Identity;
public Vector3 position;
public Vector3 direction;
public Quaternion rotation = Quaternion.Identity;
public BoundingSphere collision;
public float speed = 2f;
public float moveForwards;
public float moveLeft;
public float forward;
Matrix[] transform;
public Player(Model m, Vector3 iPos)
{
model = m;
position = iPos;
direction = new Vector3(0, 0, 1);
transform = new Matrix[m.Bones.Count];
m.CopyAbsoluteBoneTransformsTo(transform);
foreach (ModelMesh mesh in m.Meshes)
{
collision = BoundingSphere.CreateMerged(collision, mesh.BoundingSphere);
}
}
public Player()
{
}
public BoundingSphere getSphere()
{
return collision.Transform(world);
}
public void sampleInputMovement(GamePadState player)
{
moveForwards = (player.ThumbSticks.Left.Y / 5);
moveLeft = (player.ThumbSticks.Left.X / 32) * -1;
}
public void update()
{
rotation *= Quaternion.CreateFromAxisAngle(new Vector3(0, 0, -1), moveLeft) * Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), moveForwards);
Vector3 motion = Vector3.Transform(direction, rotation);
position += motion * forward;
world = Matrix.CreateFromQuaternion(rotation) * Matrix.CreateTranslation(position);
}
public void Draw(Matrix projection, Matrix view)
{
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.Projection = projection;
effect.View = view;
effect.World = transforms[mesh.ParentBone.Index] * world;
}
mesh.Draw();
}
}
}
And here is my camera class:
public class Camera
{
Vector3 position;
Vector3 newposition;
Quaternion cameraRotation;
Vector3 targetOffset = new Vector3(0, 1f, -6f);
public Matrix view = Matrix.Identity;
public Matrix projection = Matrix.Identity;
public Camera(Game game, Vector3 xPostion)
{
position = xPostion;
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, game.GraphicsDevice.Viewport.AspectRatio, 1.0f, 1000000f);
}
public void update(Matrix targetWorld)
{
Quaternion rotation;
Vector3 target;
Vector3 scale;
targetWorld.Decompose(out scale, out rotation, out target);
cameraRotation = Quaternion.Lerp(cameraRotation, rotation, 0.15f);
newposition = Vector3.Transform(targetOffset, targetWorld);
position = Vector3.SmoothStep(position, newposition, 0.8f);
Vector3 cameraup = Vector3.Transform(new Vector3(0, 1, 0), Matrix.CreateFromQuaternion(cameraRotation));
view = Matrix.CreateLookAt(position, target, cameraup);
}
}
Could someone help me get my camera in the correct position?