Advertisement
Diamond32_Tutoriales

MouseStateMoveCamera

Oct 25th, 2022
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. if (MouseState.IsButtonDown(MouseButton.Right))
  2. {
  3. CursorState = CursorState.Grabbed;
  4.  
  5. var input = KeyboardState;
  6.  
  7. const float cameraSpeed = 1.5f;
  8. const float sensitivity = 0.2f;
  9.  
  10. if (input.IsKeyDown(Keys.W))
  11. {
  12. _camera.Position += _camera.Front * cameraSpeed * (float)e.Time; // Forward
  13. }
  14.  
  15. if (input.IsKeyDown(Keys.S))
  16. {
  17. _camera.Position -= _camera.Front * cameraSpeed * (float)e.Time; // Backwards
  18. }
  19. if (input.IsKeyDown(Keys.A))
  20. {
  21. _camera.Position -= _camera.Right * cameraSpeed * (float)e.Time; // Left
  22. }
  23. if (input.IsKeyDown(Keys.D))
  24. {
  25. _camera.Position += _camera.Right * cameraSpeed * (float)e.Time; // Right
  26. }
  27. if (input.IsKeyDown(Keys.Space))
  28. {
  29. _camera.Position += _camera.Up * cameraSpeed * (float)e.Time; // Up
  30. }
  31. if (input.IsKeyDown(Keys.LeftShift))
  32. {
  33. _camera.Position -= _camera.Up * cameraSpeed * (float)e.Time; // Down
  34. }
  35.  
  36. // Get the mouse state
  37. var mouse = MouseState;
  38.  
  39. if (_firstMove) // This bool variable is initially set to true.
  40. {
  41. _lastPos = new Vector2(mouse.X, mouse.Y);
  42. _firstMove = false;
  43. }
  44. else
  45. {
  46. // Calculate the offset of the mouse position
  47. var deltaX = mouse.X - _lastPos.X;
  48. var deltaY = mouse.Y - _lastPos.Y;
  49. _lastPos = new Vector2(mouse.X, mouse.Y);
  50.  
  51. // Apply the camera pitch and yaw (we clamp the pitch in the camera class)
  52. _camera.Yaw += deltaX * sensitivity;
  53. _camera.Pitch -= deltaY * sensitivity; // Reversed since y-coordinates range from bottom to top
  54. }
  55. }
  56. else
  57. {
  58. CursorState = CursorState.Normal;
  59. } if (MouseState.IsButtonDown(MouseButton.Right))
  60. {
  61. CursorState = CursorState.Grabbed;
  62.  
  63. var input = KeyboardState;
  64.  
  65. const float cameraSpeed = 1.5f;
  66. const float sensitivity = 0.2f;
  67.  
  68. if (input.IsKeyDown(Keys.W))
  69. {
  70. _camera.Position += _camera.Front * cameraSpeed * (float)e.Time; // Forward
  71. }
  72.  
  73. if (input.IsKeyDown(Keys.S))
  74. {
  75. _camera.Position -= _camera.Front * cameraSpeed * (float)e.Time; // Backwards
  76. }
  77. if (input.IsKeyDown(Keys.A))
  78. {
  79. _camera.Position -= _camera.Right * cameraSpeed * (float)e.Time; // Left
  80. }
  81. if (input.IsKeyDown(Keys.D))
  82. {
  83. _camera.Position += _camera.Right * cameraSpeed * (float)e.Time; // Right
  84. }
  85. if (input.IsKeyDown(Keys.Space))
  86. {
  87. _camera.Position += _camera.Up * cameraSpeed * (float)e.Time; // Up
  88. }
  89. if (input.IsKeyDown(Keys.LeftShift))
  90. {
  91. _camera.Position -= _camera.Up * cameraSpeed * (float)e.Time; // Down
  92. }
  93.  
  94. // Get the mouse state
  95. var mouse = MouseState;
  96.  
  97. if (_firstMove) // This bool variable is initially set to true.
  98. {
  99. _lastPos = new Vector2(mouse.X, mouse.Y);
  100. _firstMove = false;
  101. }
  102. else
  103. {
  104. // Calculate the offset of the mouse position
  105. var deltaX = mouse.X - _lastPos.X;
  106. var deltaY = mouse.Y - _lastPos.Y;
  107. _lastPos = new Vector2(mouse.X, mouse.Y);
  108.  
  109. // Apply the camera pitch and yaw (we clamp the pitch in the camera class)
  110. _camera.Yaw += deltaX * sensitivity;
  111. _camera.Pitch -= deltaY * sensitivity; // Reversed since y-coordinates range from bottom to top
  112. }
  113. }
  114. else
  115. {
  116. CursorState = CursorState.Normal;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement