PositionClass.cs 7.2 KB

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