PositionClass.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows.Forms;
  4. using SlimDX;
  5. using SlimDX.D3DCompiler;
  6. using SlimDX.Direct3D11;
  7. using SlimDX.DXGI;
  8. using SlimDX.Windows;
  9. using Device = SlimDX.Direct3D11.Device;
  10. using Resource = SlimDX.Direct3D11.Resource;
  11. using Buffer = SlimDX.Direct3D11.Buffer;
  12. namespace EQ2ModelViewer
  13. {
  14. public class PositionClass
  15. {
  16. private float m_PositionX;
  17. private float m_positionY;
  18. private float m_PositionZ;
  19. private float m_RotationX;
  20. private float m_RotationY;
  21. private float m_RotationZ;
  22. private float m_FrameTime;
  23. private float m_ForwardSpeed;
  24. private float m_BackwardSpeed;
  25. private float m_UpwardSpeed;
  26. private float m_DownwardSpeed;
  27. private float m_LeftTurnSpeed;
  28. private float m_RightTurnSpeed;
  29. private float m_LookUpSpeed;
  30. private float m_LookDownSpeed;
  31. public bool m_ShiftDown;
  32. public void SetPosition(float x, float y, float z)
  33. {
  34. m_PositionX = x;
  35. m_positionY = y;
  36. m_PositionZ = z;
  37. }
  38. public void SetRotation(float x, float y, float z)
  39. {
  40. m_RotationX = x;
  41. m_RotationY = y;
  42. m_RotationZ = z;
  43. }
  44. public Vector3 GetPosition()
  45. {
  46. return new Vector3(m_PositionX, m_positionY, m_PositionZ);
  47. }
  48. public Vector3 GetRotation()
  49. {
  50. return new Vector3(m_RotationX, m_RotationY, m_RotationZ);
  51. }
  52. public void SetFrameTime(float time)
  53. {
  54. m_FrameTime = time;
  55. }
  56. public void MoveForward(bool keydown)
  57. {
  58. float radians;
  59. float mod = 1.0f;
  60. if (m_ShiftDown)
  61. mod = 10.0f;
  62. if (keydown)
  63. {
  64. m_ForwardSpeed += m_FrameTime * 0.001f * mod;
  65. if (m_ForwardSpeed > (m_FrameTime * 0.03f) * mod)
  66. m_ForwardSpeed = m_FrameTime * 0.03f * mod;
  67. }
  68. else
  69. {
  70. m_ForwardSpeed -= m_FrameTime * 0.0007f * mod;
  71. if (m_ForwardSpeed < 0.0f)
  72. m_ForwardSpeed = 0.0f;
  73. }
  74. radians = m_RotationY * 0.0174532925f;
  75. m_PositionX += (float)Math.Sin(radians) * m_ForwardSpeed;
  76. m_PositionZ += (float)Math.Cos(radians) * m_ForwardSpeed;
  77. }
  78. public void MoveBackward(bool keydown)
  79. {
  80. float radians;
  81. float mod = 1.0f;
  82. if (m_ShiftDown)
  83. mod = 10.0f;
  84. if (keydown)
  85. {
  86. m_BackwardSpeed += m_FrameTime * 0.001f * mod;
  87. if (m_BackwardSpeed > (m_FrameTime * 0.03f) * mod)
  88. m_BackwardSpeed = m_FrameTime * 0.03f * mod;
  89. }
  90. else
  91. {
  92. m_BackwardSpeed -= m_FrameTime * 0.0007f * mod;
  93. if (m_BackwardSpeed < 0.0f)
  94. m_BackwardSpeed = 0.0f;
  95. }
  96. radians = m_RotationY * 0.0174532925f;
  97. m_PositionX -= (float)Math.Sin(radians) * m_BackwardSpeed;
  98. m_PositionZ -= (float)Math.Cos(radians) * m_BackwardSpeed;
  99. }
  100. public void MoveUpward(bool keydown)
  101. {
  102. if (keydown)
  103. {
  104. m_UpwardSpeed += m_FrameTime * 0.003f;
  105. if (m_UpwardSpeed > (m_FrameTime * 0.03f))
  106. m_UpwardSpeed = m_FrameTime * 0.03f;
  107. }
  108. else
  109. {
  110. m_UpwardSpeed -= m_FrameTime * 0.002f;
  111. if (m_UpwardSpeed < 0.0f)
  112. m_UpwardSpeed = 0.0f;
  113. }
  114. m_positionY += m_UpwardSpeed;
  115. }
  116. public void MoveDownward(bool keydown)
  117. {
  118. if (keydown)
  119. {
  120. m_DownwardSpeed += m_FrameTime * 0.003f;
  121. if (m_DownwardSpeed > (m_FrameTime * 0.03f))
  122. m_DownwardSpeed = m_FrameTime * 0.03f;
  123. }
  124. else
  125. {
  126. m_DownwardSpeed -= m_FrameTime * 0.002f;
  127. if (m_DownwardSpeed < 0.0f)
  128. m_DownwardSpeed = 0.0f;
  129. }
  130. m_positionY -= m_DownwardSpeed;
  131. }
  132. public void TurnLeft(bool keydown)
  133. {
  134. // Update the left turn speed movement based on the frame time and whether the user is holding the key down or not.
  135. if (keydown)
  136. {
  137. m_LeftTurnSpeed += m_FrameTime * 0.01f;
  138. if (m_LeftTurnSpeed > (m_FrameTime * 0.15f))
  139. m_LeftTurnSpeed = m_FrameTime * 0.15f;
  140. }
  141. else
  142. {
  143. m_LeftTurnSpeed -= m_FrameTime * 0.005f;
  144. if (m_LeftTurnSpeed < 0.0f)
  145. m_LeftTurnSpeed = 0.0f;
  146. }
  147. // Update the rotation.
  148. m_RotationY -= m_LeftTurnSpeed;
  149. // Keep the rotation in the 0 to 360 range.
  150. if (m_RotationY < 0.0f)
  151. m_RotationY += 360.0f;
  152. }
  153. public void TurnRight(bool keydown)
  154. {
  155. // Update the right turn speed movement based on the frame time and whether the user is holding the key down or not.
  156. if (keydown)
  157. {
  158. m_RightTurnSpeed += m_FrameTime * 0.01f;
  159. if (m_RightTurnSpeed > (m_FrameTime * 0.15f))
  160. m_RightTurnSpeed = m_FrameTime * 0.15f;
  161. }
  162. else
  163. {
  164. m_RightTurnSpeed -= m_FrameTime * 0.005f;
  165. if (m_RightTurnSpeed < 0.0f)
  166. m_RightTurnSpeed = 0.0f;
  167. }
  168. // Update the rotation.
  169. m_RotationY += m_RightTurnSpeed;
  170. // Keep the rotation in the 0 to 360 range.
  171. if (m_RotationY > 360.0f)
  172. m_RotationY -= 360.0f;
  173. }
  174. public void LookUpward(bool keydown)
  175. {
  176. // Update the upward rotation speed movement based on the frame time and whether the user is holding the key down or not.
  177. if (keydown)
  178. {
  179. m_LookUpSpeed += m_FrameTime * 0.01f;
  180. if (m_LookUpSpeed > (m_FrameTime * 0.15f))
  181. m_LookUpSpeed = m_FrameTime * 0.15f;
  182. }
  183. else
  184. {
  185. m_LookUpSpeed -= m_FrameTime * 0.005f;
  186. if (m_LookUpSpeed < 0.0f)
  187. m_LookUpSpeed = 0.0f;
  188. }
  189. // Update the rotation.
  190. m_RotationX -= m_LookUpSpeed;
  191. // Keep the rotation maximum 90 degrees.
  192. if (m_RotationX > 90.0f)
  193. m_RotationX = 90.0f;
  194. }
  195. public void LookDownward(bool keydown)
  196. {
  197. // Update the downward rotation speed movement based on the frame time and whether the user is holding the key down or not.
  198. if (keydown)
  199. {
  200. m_LookDownSpeed += m_FrameTime * 0.01f;
  201. if (m_LookDownSpeed > (m_FrameTime * 0.15f))
  202. m_LookDownSpeed = m_FrameTime * 0.15f;
  203. }
  204. else
  205. {
  206. m_LookDownSpeed -= m_FrameTime * 0.005f;
  207. if (m_LookDownSpeed < 0.0f)
  208. m_LookDownSpeed = 0.0f;
  209. }
  210. // Update the rotation.
  211. m_RotationX += m_LookDownSpeed;
  212. // Keep the rotation maximum 90 degrees.
  213. if (m_RotationX < -90.0f)
  214. m_RotationX = -90.0f;
  215. }
  216. }
  217. }