Camera.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Microsoft.Xna.Framework;
  2. namespace FuelCell
  3. {
  4. public class Camera
  5. {
  6. public Vector3 AvatarHeadOffset { get; set; }
  7. public Vector3 TargetOffset { get; set; }
  8. public Matrix ViewMatrix { get; set; }
  9. public Matrix ProjectionMatrix { get; set; }
  10. public Camera()
  11. {
  12. AvatarHeadOffset = new Vector3(0, 7, -15);
  13. TargetOffset = new Vector3(0, 5, 0);
  14. ViewMatrix = Matrix.Identity;
  15. ProjectionMatrix = Matrix.Identity;
  16. }
  17. public void Update(float avatarYaw, Vector3 position, float aspectRatio)
  18. {
  19. Matrix rotationMatrix = Matrix.CreateRotationY(avatarYaw);
  20. Vector3 transformedheadOffset = Vector3.Transform(AvatarHeadOffset, rotationMatrix);
  21. Vector3 transformedReference = Vector3.Transform(TargetOffset, rotationMatrix);
  22. Vector3 cameraPosition = position + transformedheadOffset;
  23. Vector3 cameraTarget = position + transformedReference;
  24. //Calculate the camera's view and projection matrices based on current values.
  25. ViewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
  26. ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(GameConstants.ViewAngle),
  27. aspectRatio, GameConstants.NearClip, GameConstants.FarClip);
  28. }
  29. }
  30. }