Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Test3D.Objects
  11. {
  12. public class Camera
  13. {
  14. // We need this to calculate the aspectRatio in the ProjectionMatrix property.
  15. GraphicsDevice graphicsDevice;
  16.  
  17. Vector3 position = new Vector3();
  18. Vector3 rotation = new Vector3();
  19.  
  20. Vector3 up;
  21. Vector3 right;
  22. Vector3 look;
  23.  
  24. public enum CameraRotations
  25. {
  26. YawLeft,
  27. YawRight,
  28. PitchUp,
  29. PitchDown,
  30. RollClockwise,
  31. RollAntiClockwise,
  32. }
  33.  
  34. public enum CameraMovements
  35. {
  36. StrafeLeft,
  37. StrafeRight,
  38. ThrustForward,
  39. ThrustBackward,
  40.  
  41. }
  42.  
  43. public Matrix ViewMatrix
  44. {
  45. get
  46. {
  47. /* To apply Roll (Z rotation) we rotate up and right around the look vector: */
  48. Matrix rollMatrix = Matrix.CreateFromAxisAngle(look, rotation.Z);
  49. up = Vector3.Transform(up, rollMatrix);
  50. right = Vector3.Transform(right, rollMatrix);
  51.  
  52. /* To apply Yaw (Y rotation) we rotate the look and right vectors around the up vector: */
  53. Matrix yawMatrix = Matrix.CreateFromAxisAngle(up, rotation.Y);
  54. look = Vector3.Transform(look, yawMatrix);
  55. right = Vector3.Transform(right, yawMatrix);
  56.  
  57. /* To apply Pitch (X rotation) we rotate look and up around the right vector: */
  58. Matrix pitchMatrix = Matrix.CreateFromAxisAngle(right, rotation.X);
  59. look = Vector3.Transform(look, pitchMatrix);
  60. up = Vector3.Transform(up, pitchMatrix);
  61.  
  62. /* The CreateLookAt function requires a camera position, a target and an up direction.
  63. * We know the camera position and the up vector but we do not know the target.
  64. * However the look vector points in the direction the camera is facing so we can use that to create a target by adding it to the position.
  65. */
  66. Vector3 target = position + look;
  67.  
  68. return Matrix.CreateLookAt(position, target, up);
  69. }
  70. }
  71.  
  72. public Matrix ProjectionMatrix
  73. {
  74. get
  75. {
  76. float fieldOfView = Microsoft.Xna.Framework.MathHelper.PiOver4;
  77. float nearClipPlane = 1;
  78. float farClipPlane = 200;
  79. float aspectRatio = graphicsDevice.Viewport.Width / (float)graphicsDevice.Viewport.Height;
  80.  
  81. return Matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, nearClipPlane, farClipPlane);
  82. }
  83. }
  84.  
  85. public Camera(GraphicsDevice graphicsDevice)
  86. {
  87. this.graphicsDevice = graphicsDevice;
  88. }
  89.  
  90. public void SetPosition(Vector3 pos)
  91. {
  92. position = pos;
  93. }
  94.  
  95. public void SetRotation(Vector3 rot)
  96. {
  97. rotation = rot;
  98. }
  99.  
  100. public void Rotate(CameraRotations rot, float angle)
  101. {
  102. switch (rot)
  103. {
  104. case CameraRotations.YawLeft:
  105. RotateAroundY(angle);
  106. break;
  107. case CameraRotations.YawRight:
  108. RotateAroundY(-angle);
  109. break;
  110. case CameraRotations.PitchUp:
  111. RotateAroundX(angle);
  112. break;
  113. case CameraRotations.PitchDown:
  114. RotateAroundX(-angle);
  115. break;
  116. case CameraRotations.RollClockwise:
  117. RotateAroundZ(angle);
  118. break;
  119. case CameraRotations.RollAntiClockwise:
  120. RotateAroundZ(-angle);
  121. break;
  122. default:
  123. break;
  124. }
  125. }
  126.  
  127. public void Move(CameraMovements mov, float speed)
  128. {
  129. const float unitsPerSecond = 10;
  130.  
  131. switch (mov)
  132. {
  133. case CameraMovements.StrafeLeft:
  134. position += right * unitsPerSecond * speed;
  135. break;
  136. case CameraMovements.StrafeRight:
  137. position -= right * unitsPerSecond * speed;
  138. break;
  139. case CameraMovements.ThrustForward:
  140. position += look * unitsPerSecond * speed;
  141. break;
  142. case CameraMovements.ThrustBackward:
  143. position -= look * unitsPerSecond * speed;
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149.  
  150. private void RotateAroundY(float angle)
  151. {
  152. rotation.Y += angle;
  153.  
  154. // keep the value in the range 0-360 (0 - 2 PI radians)
  155. if (rotation.Y > Math.PI * 2)
  156. rotation.Y -= MathHelper.Pi * 2;
  157. else if (rotation.Y < 0)
  158. rotation.Y += MathHelper.Pi * 2;
  159. }
  160.  
  161. private void RotateAroundX(float angle)
  162. {
  163. rotation.X += angle;
  164.  
  165. // keep the value in the range 0-360 (0 - 2 PI radians)
  166. if (rotation.X > Math.PI * 2)
  167. rotation.X -= MathHelper.Pi * 2;
  168. else if (rotation.X < 0)
  169. rotation.X += MathHelper.Pi * 2;
  170. }
  171.  
  172. private void RotateAroundZ(float angle)
  173. {
  174. rotation.Z += angle;
  175.  
  176. // keep the value in the range 0-360 (0 - 2 PI radians)
  177. if (rotation.Z > Math.PI * 2)
  178. rotation.Z -= MathHelper.Pi * 2;
  179. else if (rotation.Z < 0)
  180. rotation.Z += MathHelper.Pi * 2;
  181. }
  182.  
  183. public void Update(GameTime gameTime)
  184. {
  185. float unit = (float)gameTime.ElapsedGameTime.TotalSeconds;
  186.  
  187. // Rotation
  188. if (Keyboard.GetState().IsKeyDown(Keys.NumPad4))
  189. {
  190. Rotate(CameraRotations.YawRight, unit);
  191. }
  192. else if (Keyboard.GetState().IsKeyDown(Keys.NumPad6))
  193. {
  194. Rotate(CameraRotations.YawLeft, unit);
  195. }
  196.  
  197. if (Keyboard.GetState().IsKeyDown(Keys.PageDown))
  198. {
  199. Rotate(CameraRotations.PitchDown, unit);
  200. }
  201. else if (Keyboard.GetState().IsKeyDown(Keys.PageUp))
  202. {
  203. Rotate(CameraRotations.PitchUp, unit);
  204. }
  205.  
  206. if (Keyboard.GetState().IsKeyDown(Keys.NumPad1))
  207. {
  208. Rotate(CameraRotations.RollAntiClockwise, unit);
  209. }
  210. else if (Keyboard.GetState().IsKeyDown(Keys.NumPad3))
  211. {
  212. Rotate(CameraRotations.RollClockwise, unit);
  213. }
  214.  
  215. // Movement
  216. if (Keyboard.GetState().IsKeyDown(Keys.Left))
  217. {
  218. Move(CameraMovements.StrafeLeft, unit);
  219. }
  220. else if (Keyboard.GetState().IsKeyDown(Keys.Right))
  221. {
  222. Move(CameraMovements.StrafeRight, unit);
  223. }
  224.  
  225. if (Keyboard.GetState().IsKeyDown(Keys.Up))
  226. {
  227. Move(CameraMovements.ThrustForward, unit);
  228. }
  229. else if (Keyboard.GetState().IsKeyDown(Keys.Down))
  230. {
  231. Move(CameraMovements.ThrustBackward, unit);
  232. }
  233. }
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement