FollowCamera.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // FollowCamera.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using System.Text;
  14. #endregion
  15. namespace RobotGameData.Camera
  16. {
  17. /// <summary>
  18. /// This camera moves by following the target matrix at a constant distance.
  19. /// The camera always positions itself from the target matrix
  20. /// at a specific offset distance.
  21. /// </summary>
  22. public class FollowCamera : GameCamera
  23. {
  24. #region Fields
  25. /// <summary>
  26. /// It's a camera's target matrix
  27. /// </summary>
  28. protected Matrix targetMatrix = Matrix.Identity;
  29. /// <summary>
  30. /// It's a offset position of the camera look at target position
  31. /// </summary>
  32. protected Vector3 targetOffset = Vector3.Zero;
  33. /// <summary>
  34. /// It's a offset position of the camera's position.
  35. /// </summary>
  36. protected Vector3 positionOffset = Vector3.Zero;
  37. #endregion
  38. #region Properties
  39. public Matrix TargetMatrix
  40. {
  41. set { targetMatrix = value; }
  42. get { return targetMatrix; }
  43. }
  44. public Vector3 TargetOffset
  45. {
  46. set { targetOffset = value; }
  47. get { return targetOffset; }
  48. }
  49. public Vector3 PositionOffset
  50. {
  51. set { positionOffset = value; }
  52. get { return positionOffset; }
  53. }
  54. #endregion
  55. /// <summary>
  56. /// Constructor.
  57. /// </summary>
  58. public FollowCamera()
  59. : base()
  60. {
  61. targetMatrix = Matrix.Identity;
  62. targetOffset = Vector3.Zero;
  63. positionOffset = Vector3.Zero;
  64. }
  65. /// <summary>
  66. /// This configures a new view matrix by using target matrix and offset position.
  67. /// </summary>
  68. protected override void OnUpdate(GameTime gameTime)
  69. {
  70. // Make a camera's target position using offset position
  71. Vector3 target = TargetMatrix.Translation +
  72. Vector3.Multiply(TargetMatrix.Right, targetOffset.X) +
  73. Vector3.Multiply(TargetMatrix.Up, targetOffset.Y) +
  74. Vector3.Multiply(TargetMatrix.Forward, targetOffset.Z);
  75. Vector3 position = target + this.TrembleOffset +
  76. Vector3.Multiply(TargetMatrix.Right, positionOffset.X) +
  77. Vector3.Multiply(TargetMatrix.Up, positionOffset.Y) +
  78. Vector3.Multiply(TargetMatrix.Forward, positionOffset.Z);
  79. // Recalculate new view matrix
  80. SetView(position, target, Up);
  81. base.OnUpdate(gameTime);
  82. }
  83. /// <summary>
  84. /// Set the target matrix
  85. /// </summary>
  86. public void SetFollow(Matrix targetMatrix)
  87. {
  88. // Set to following target
  89. this.targetMatrix = targetMatrix;
  90. }
  91. /// <summary>
  92. /// Set the target matrix and camera's offset position
  93. /// </summary>
  94. public void SetFollow(Matrix targetMatrix,
  95. Vector3 targetOffset,
  96. Vector3 cameraOffset)
  97. {
  98. // Set to following target
  99. this.targetMatrix = targetMatrix;
  100. // Set to target offset value
  101. this.TargetOffset = targetOffset;
  102. // Set to position offset value
  103. this.PositionOffset = cameraOffset;
  104. }
  105. }
  106. }