FreeCamera.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // FreeCamera.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 can rotate and move freely.
  19. /// </summary>
  20. public class FreeCamera : CameraBase
  21. {
  22. #region Fields
  23. /// <summary>
  24. /// Rotation of the camera matrix
  25. /// </summary>
  26. protected Matrix rotateMatrix = Matrix.Identity;
  27. #endregion
  28. #region Properties
  29. public Matrix RotateMatrix
  30. {
  31. get { return rotateMatrix; }
  32. }
  33. #endregion
  34. /// <summary>
  35. /// Constructor.
  36. /// </summary>
  37. public FreeCamera()
  38. : base()
  39. {
  40. rotateMatrix = Matrix.Identity;
  41. }
  42. /// <summary>
  43. /// Update the camera
  44. /// </summary>
  45. protected override void OnUpdate(GameTime gameTime)
  46. {
  47. base.OnUpdate(gameTime);
  48. }
  49. /// <summary>
  50. /// Rotate the camera
  51. /// </summary>
  52. public Matrix Rotate(Vector3 rotationAmount)
  53. {
  54. // Add rotation amount per second
  55. rotationAmount *= (float)FrameworkCore.ElapsedDeltaTime.TotalSeconds;
  56. // To radians amount
  57. float rotationAmountX = MathHelper.ToRadians(rotationAmount.X);
  58. float rotationAmountY = MathHelper.ToRadians(rotationAmount.Y);
  59. // Create rotation matrix from rotation amount
  60. rotateMatrix = Matrix.CreateFromAxisAngle(Right, rotationAmountY) *
  61. Matrix.CreateFromAxisAngle(Vector3.Up, rotationAmountX);
  62. // Rotate orientation vectors
  63. Vector3 dir = Vector3.TransformNormal(this.Direction, rotateMatrix);
  64. Vector3 up = Vector3.TransformNormal(this.Up, rotateMatrix);
  65. // Re-normalize orientation vectors
  66. dir.Normalize();
  67. up.Normalize();
  68. // Re-calculate Right
  69. Vector3 right = Vector3.Cross(dir, up);
  70. // The same instability may cause the 3 orientation vectors may
  71. // also diverge. Either the Up or Direction vector needs to be
  72. // re-computed with a cross product to ensure orthagonality
  73. up = Vector3.Cross(right, dir);
  74. Vector3 tar = this.Position + dir;
  75. SetView(this.Position, tar, up);
  76. return rotateMatrix;
  77. }
  78. /// <summary>
  79. /// if the velocity is a positive number, it moves forward.
  80. /// If negative, it moves backward.
  81. /// Until velocity is set to 0, the camera will keep moving.
  82. /// </summary>
  83. /// <param name="velocity">movement per second</param>
  84. public void MoveForward(float velocity)
  85. {
  86. float vel = velocity * (float)FrameworkCore.ElapsedDeltaTime.TotalSeconds;
  87. // Moves to forward or backward
  88. Vector3 pos = this.Position + (this.Direction * vel);
  89. Vector3 tar = pos + this.Direction;
  90. SetView(pos, tar, this.Up);
  91. }
  92. /// <summary>
  93. /// If the velocity is positive, it moves to the right.
  94. /// If negative, to the left.
  95. /// Until the velocity is set to 0 again, the camera will keep moving.
  96. /// </summary>
  97. /// <param name="velocity">move amount per second</param>
  98. public void MoveSide(float velocity)
  99. {
  100. float vel = velocity * (float)FrameworkCore.ElapsedDeltaTime.TotalSeconds;
  101. // Moves to left or right
  102. Vector3 pos = this.Position + (this.Right * vel);
  103. Vector3 tar = pos + this.Direction;
  104. SetView(pos, tar, this.Up);
  105. }
  106. /// <summary>
  107. /// If the velocity is positive, it moves to the upward.
  108. /// If negative, to the downward.
  109. /// Until the velocity is set to 0 again, the camera will keep moving.
  110. /// </summary>
  111. /// <param name="velocity">move amount per second</param>
  112. public void MoveUp(float velocity)
  113. {
  114. float vel = velocity * (float)FrameworkCore.ElapsedDeltaTime.TotalSeconds;
  115. // Moves to left or right
  116. Vector3 pos = this.Position + (this.Up * vel);
  117. Vector3 tar = pos + this.Direction;
  118. SetView(pos, tar, this.Up);
  119. }
  120. }
  121. }