Advertisement
gr4viton

position

Apr 26th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. public class Position : ICloneable
  2. {
  3. #region Structures / Enums
  4. private enum Movement
  5. {
  6. Forward,
  7. Backward,
  8. Upward,
  9. Downward,
  10. LeftTurn,
  11. RightTurn,
  12. LookUp,
  13. LookDown
  14. }
  15. #endregion
  16. #region Properties / Variables
  17. public float FrameTime { get; set; }
  18.  
  19. private float positionX, positionY, positionZ;
  20. private float rotationX, rotationY, rotationZ;
  21.  
  22. private float forwardSpeed, backwardSpeed;
  23. private float upwardSpeed, downwardSpeed;
  24. private float leftTurnSpeed, rightTurnSpeed;
  25. private float lookUpSpeed, lookDownSpeed;
  26. #endregion
  27.  
  28. #region Public Methods
  29. public void SetPosition(float x, float y, float z)
  30. {
  31. positionX = x;
  32. positionY = y;
  33. positionZ = z;
  34. }
  35. public void SetPosition(Vector3 position)
  36. {
  37. SetPosition(position.X, position.Y, position.Z);
  38. }
  39.  
  40. public void SetRotation(float x, float y, float z)
  41. {
  42. rotationX = x;
  43. rotationY = y;
  44. rotationZ = z;
  45. }
  46. public void SetRotation(Vector3 rotation)
  47. {
  48. SetRotation(rotation.X, rotation.Y, rotation.Z);
  49. }
  50.  
  51. public Vector3 GetPosition()
  52. {
  53. return new Vector3(positionX, positionY, positionZ);
  54. }
  55. public void GetPosition(out float x, out float y, out float z)
  56. {
  57. x = positionX;
  58. y = positionY;
  59. z = positionZ;
  60. }
  61.  
  62. public Vector3 GetRotation()
  63. {
  64. return new Vector3(rotationX, rotationY, rotationZ);
  65. }
  66. public void GetRotation(out float x, out float y, out float z)
  67. {
  68. x = rotationX;
  69. y = rotationY;
  70. z = rotationZ;
  71. }
  72.  
  73. public void MoveForward(bool keydown)
  74. {
  75. // Update the forward speed movement based on the frame time and whether the user is holding the key down or not.
  76. if (keydown)
  77. {
  78. forwardSpeed += FrameTime * 0.001f;
  79. if (forwardSpeed > FrameTime * 0.03)
  80. forwardSpeed = FrameTime * 0.03f;
  81. }
  82. else
  83. {
  84. forwardSpeed -= FrameTime * 0.0007f;
  85. if (forwardSpeed < 0)
  86. forwardSpeed = 0;
  87. }
  88.  
  89. // Convert degrees to radians.
  90. var radians = rotationY * 0.0174532925f;
  91.  
  92. // Update the position.
  93. positionX += (float)Math.Sin(radians) * forwardSpeed;
  94. positionZ += (float)Math.Cos(radians) * forwardSpeed;
  95. }
  96.  
  97. public void MoveBackward(bool keydown)
  98. {
  99. // Update the backward speed movement based on the frame time and whether the user is holding the key down or not.
  100. if (keydown)
  101. {
  102. backwardSpeed += FrameTime * 0.001f;
  103. if (backwardSpeed > FrameTime * 0.03)
  104. backwardSpeed = FrameTime * 0.03f;
  105. }
  106. else
  107. {
  108. backwardSpeed -= FrameTime * 0.0007f;
  109. if (backwardSpeed < 0)
  110. backwardSpeed = 0;
  111. }
  112.  
  113. // Convert degrees to radians.
  114. var radians = rotationY * 0.0174532925f;
  115.  
  116. // Update the position.
  117. positionX -= (float)Math.Sin(radians) * backwardSpeed;
  118. positionZ -= (float)Math.Cos(radians) * backwardSpeed;
  119. }
  120.  
  121. public void MoveUpward(bool keydown)
  122. {
  123. // Update the upward speed movement based on the frame time and whether the user is holding the key down or not.
  124. if (keydown)
  125. {
  126. upwardSpeed += FrameTime * 0.003f;
  127. if (upwardSpeed > FrameTime * 0.03)
  128. upwardSpeed = FrameTime * 0.03f;
  129. }
  130. else
  131. {
  132. upwardSpeed -= FrameTime * 0.0002f;
  133. if (upwardSpeed < 0)
  134. upwardSpeed = 0;
  135. }
  136.  
  137. // Update the height position.
  138. positionY += upwardSpeed;
  139. }
  140.  
  141. public void MoveDownward(bool keydown)
  142. {
  143. // Update the upward speed movement based on the frame time and whether the user is holding the key down or not.
  144. if (keydown)
  145. {
  146. downwardSpeed += FrameTime * 0.003f;
  147. if (downwardSpeed > FrameTime * 0.03)
  148. downwardSpeed = FrameTime * 0.03f;
  149. }
  150. else
  151. {
  152. downwardSpeed -= FrameTime * 0.0002f;
  153. if (downwardSpeed < 0)
  154. downwardSpeed = 0;
  155. }
  156.  
  157. // Update the height position.
  158. positionY -= downwardSpeed;
  159. }
  160.  
  161. public void TurnLeft(bool keydown)
  162. {
  163. // If the key is pressed increase the speed at which the camera turns left. If not slow down the turn speed.
  164. if (keydown)
  165. {
  166. leftTurnSpeed += FrameTime * 0.01f;
  167. if (leftTurnSpeed > FrameTime * 0.15f)
  168. leftTurnSpeed = FrameTime * 0.15f;
  169. }
  170. else
  171. {
  172. leftTurnSpeed -= FrameTime * 0.005f;
  173. if (leftTurnSpeed < 0)
  174. leftTurnSpeed = 0;
  175. }
  176.  
  177. // Update the rotation using the turning speed.
  178. rotationY -= leftTurnSpeed;
  179.  
  180. // Keep the rotation in the 0 to 360
  181. if (rotationY < 0)
  182. rotationY += 360;
  183. }
  184.  
  185. public void TurnRight(bool keydown)
  186. {
  187. // If the key is pressed increase the speed at which the camera turns right. If not slow down the turn speed.
  188. if (keydown)
  189. {
  190. rightTurnSpeed += FrameTime * 0.01f;
  191. if (rightTurnSpeed > FrameTime * 0.15)
  192. rightTurnSpeed = FrameTime * 0.15f;
  193. }
  194. else
  195. {
  196. rightTurnSpeed -= FrameTime * 0.005f;
  197. if (rightTurnSpeed < 0)
  198. rightTurnSpeed = 0;
  199. }
  200.  
  201. // Update the rotation using the turning speed.
  202. rotationY += rightTurnSpeed;
  203.  
  204. // Keep the rotation in the range 0 to 360 range.
  205. if (rotationY > 360)
  206. rotationY -= 360;
  207. }
  208.  
  209. public void LookUpward(bool keydown)
  210. {
  211. // If the key is pressed increase the speed at which the camera turns up. If not slow down the turn speed.
  212. if (keydown)
  213. {
  214. lookUpSpeed += FrameTime * 0.01f;
  215. if (lookUpSpeed > FrameTime * 0.15)
  216. lookUpSpeed = FrameTime * 0.15f;
  217. }
  218. else
  219. {
  220. lookUpSpeed -= FrameTime * 0.005f;
  221. if (lookUpSpeed < 0)
  222. lookUpSpeed = 0;
  223. }
  224.  
  225. // Update the rotation using the turning speed.
  226. rotationX -= lookUpSpeed;
  227.  
  228. // Keep the rotation maximum 90 degrees.
  229. if (rotationX > 90)
  230. rotationX = 90;
  231. }
  232.  
  233. public void LookDownward(bool keydown)
  234. {
  235. // If the key is pressed increase the speed at which the camera turns down. If not slow down the turn speed.
  236. if (keydown)
  237. {
  238. lookDownSpeed += FrameTime * 0.01f;
  239. if (lookDownSpeed > FrameTime * 0.15)
  240. lookDownSpeed = FrameTime * 0.15f;
  241. }
  242. else
  243. {
  244. lookDownSpeed -= FrameTime * 0.005f;
  245. if (lookDownSpeed < 0)
  246. lookDownSpeed = 0;
  247. }
  248.  
  249. // Update the rotation using the turning speed.
  250. rotationX += lookDownSpeed;
  251.  
  252. // Keep the rotation maximum 90 degrees
  253. if (rotationX < -90)
  254. rotationX = -90;
  255. }
  256. #endregion
  257.  
  258. #region Override Methods
  259. public object Clone()
  260. {
  261. return MemberwiseClone();
  262. }
  263. #endregion
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement