I have a player class and camera class. I want my model to move and the camera to follow it. Simple enough, but when I move my model it decides to fly off the screen and then stops somewhere far in the distance, and then moves up and down on the Y axis. I'm not exactly sure where I went wrong with this one. The camera also doesn't move either, but that's not as much of an issue right now.
class Player
{
Model playerModel;
public Matrix playerWorld;
Matrix[] playerTransforms;
public float speed = 2f;
public float rotationRate = 0.3f;
public Vector3 Position { get; set; }
public float RotationX { get; set; }
public float RotationY { get; set; }
public float Scale { get; set; }
public Player()
{
playerWorld = Matrix.CreateScale(.1f);
this.Position = new Vector3(0, 1f, 0);
}
public void LoadContent(ContentManager content)
{
playerModel = content.Load<Model>("p1_wedge");
playerTransforms = new Matrix[playerModel.Bones.Count];
playerModel.CopyAbsoluteBoneTransformsTo(playerTransforms);
}
public void Update(float dt)
{
KeyboardState kbs = Keyboard.GetState();
if (kbs.IsKeyDown(Keys.W))
{
playerWorld += Matrix.CreateTranslation(Vector3.Forward * speed * dt);
}
if (kbs.IsKeyDown(Keys.S))
{
playerWorld += Matrix.CreateTranslation(playerWorld.Backward * speed * dt);
}
if (kbs.IsKeyDown(Keys.A))
{
Vector3 temp = playerWorld.Translation;
playerWorld.Translation = Vector3.Zero;
playerWorld += Matrix.CreateRotationY(rotationRate * dt);
playerWorld.Translation = temp;
}
if (kbs.IsKeyDown(Keys.D))
{
Vector3 temp = playerWorld.Translation;
playerWorld.Translation = Vector3.Zero;
playerWorld += Matrix.CreateRotationY(-rotationRate * dt);
playerWorld.Translation = temp;
}
}
public void Render(ThirdPersonCam cam)
{
foreach (ModelMesh mesh in playerModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.View = cam.view;
effect.Projection = cam.proj;
effect.World = playerTransforms[mesh.ParentBone.Index] * playerWorld;
}
mesh.Draw();
}
}
}
That's the player class. The error lies within the draw or update method, but I'm not sure what I'm looking for. When I press "w" it seems like the model moves behind me at first.
public class ThirdPersonCam
{
public Matrix view;
public Matrix proj;
public ThirdPersonCam()
{
proj = Matrix.CreatePerspectiveFieldOfView(0.78f, 1.7777f, 1f, 10000f);
}
public void CameraUpdate(Matrix objectToFollow)
{
Vector3 camPosition = objectToFollow.Translation + (objectToFollow.Backward * 10f) + (objectToFollow.Up * 15f) + (objectToFollow.Left * 1f);
Vector3 camTarget = objectToFollow.Translation;
view = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up);
}
There's the camera class. This doesn't work either, as I have called the CameraUpdate method in my main game class and the camera doesn't move at all. The camera is following the player.playerWorld matrix when I call it.