I'm having following problem: Can't set my camera to follow object from behind and rotate when I use left/right arrow. I've tried different tutorial and examples but it still doesn't work for me. Don't know where I'm wrong
this is the code for handle input - so far I've removed the rotation option so the code can compile
if (currentKeyboardState.IsKeyDown(Keys.Up))
{
zTranslationPosition -=5;
//translatePosition.Z=- 1;
animationCat.StartClip(animationCat.walk_R);
animationCat.Update(gameTime.TotalGameTime, true, Matrix.Identity);
}
if (currentKeyboardState.IsKeyDown(Keys.Down))
{
zTranslationPosition += 5;
//translatePosition.Z =+ 1;
animationCat.StartClip(animationCat.walk_R);
animationCat.Update(gameTime.TotalGameTime, true, Matrix.Identity);
}
if (currentKeyboardState.IsKeyDown(Keys.Left))
{
xTranslationPosition -=5;
// turn += 1;
animationCat.StartClip(animationCat.walk_R);
animationCat.Update(gameTime.TotalGameTime, true, Matrix.Identity);
}
if (currentKeyboardState.IsKeyDown(Keys.Right))
{
// turn -= 1;
xTranslationPosition +=5;
animationCat.StartClip(animationCat.walk_R);
animationCat.Update(gameTime.TotalGameTime, true, Matrix.Identity);
}
turn = MathHelper.Clamp(turn, -1, 1);
collision.checkCollision(catBounding, hud);
Vector3 movement = Vector3.Zero;
catPosition.X = xTranslationPosition;
catPosition.Y = yTranslationPosition;
catPosition.Z = zTranslationPosition;
//catPosition.Y = heightmap.getHeight(catPosition) + 10;
//translatePosition = newPosition;
camera.updateCamera(catPosition);
Drawing the object:
Matrix[] bones = animationCat.GetCatTransforms();
Matrix rotationMatrix = Matrix.CreateRotationY(MathHelper.ToRadians(180)) *
Matrix.CreateRotationZ(MathHelper.ToRadians(90));
Matrix[] transforms = new Matrix[currentModel.Bones.Count];
currentModel.Root.Transform = Matrix.CreateRotationY(MathHelper.ToRadians(-90));
currentModel.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in currentModel.Meshes)
{
foreach (Effect effect in mesh.Effects)
{
if (effect is SkinnedEffect)
{
SkinnedEffect thiseffect = effect as SkinnedEffect;
thiseffect.SetBoneTransforms(bones);
thiseffect.View = camera.getView;
thiseffect.Projection = camera.getProjection;
thiseffect.World = transforms[mesh.ParentBone.Index] *
rotationMatrix *
Matrix.CreateTranslation(xTranslationPosition, yTranslationPosition, zTranslationPosition);
catBounding = BoundingBox.CreateFromSphere(collision.CreateSphere(currentModel, thiseffect.World));
}
}
mesh.Draw();
}
And my camera class which I've borrowed from msdn sample for 3rd person camera.
public class GameCamera
{
Matrix view;
Matrix proj;
Vector3 avatarHeadOffset = new Vector3(0, 10, 0);
// Set the direction the camera points without rotation.
Vector3 cameraReference = new Vector3(0, 0, 10);
Vector3 thirdPersonReference = new Vector3(0, 200, -200);
// Set field of view of the camera in radians (pi/4 is 45 degrees).
static float viewAngle = MathHelper.PiOver4;
// Set distance from the camera of the near and far clipping planes.
//static float nearClip = 5.0f;
static float nearClip = 1.0f;
static float farClip = 2000.0f;
public void updateCamera(Vector3 position)
{
Matrix rotationMatrix = Matrix.CreateRotationY(avatarYaw);
// Create a vector pointing the direction the camera is facing.
Vector3 transformedReference =
Vector3.Transform(thirdPersonReference, rotationMatrix);
// Calculate the position the camera is looking from.
Vector3 cameraPosition = transformedReference + position;
// Set up the view matrix and projection matrix.
view = Matrix.CreateLookAt(cameraPosition, position,
new Vector3(0.0f, 1.0f, 0.0f));
Viewport viewport = Game1.graphics.GraphicsDevice.Viewport;
float aspectRatio = (float)viewport.Width / (float)viewport.Height;
proj = Matrix.CreatePerspectiveFieldOfView(viewAngle, aspectRatio,
nearClip, farClip);
}
}
As you can see here cat is very small and thats not the view I want to have. view from camera