/Pipe/FpsCameraController.cs

https://github.com/konlil/pipe
C# | 162 lines | 12 code | 1 blank | 149 comment | 0 complexity | 7a0ec69a01df9d1ba7140b2535424e9e MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework.Input;
  6. using Microsoft.Xna.Framework;
  7. namespace Pipe
  8. {
  9. public class FpsCameraController //:Controller
  10. {
  11. /* private Camera camera;
  12. private Vector3 target_position; //当前相机的目标位置
  13. private Vector3 desired_position; //目的点位置
  14. private Vector3 moving_direction;
  15. private float moving_speed = 1.0f;
  16. public FpsCameraController(PipeEngine engine)
  17. : base(engine)
  18. {
  19. }
  20. public override void Action(GameTime gameTime)
  21. {
  22. if(camera == null)
  23. {
  24. camera = (Camera)controlled_entity;
  25. }
  26. Vector3 prev_pos = this.target_position;
  27. UpdateMoving();
  28. ProcessInput();
  29. Vector3 ofs = this.target_position - prev_pos;
  30. UpdateCamera(ofs);
  31. }
  32. private void UpdateCamera(Vector3 ofs)
  33. {
  34. this.camera.Position += ofs;
  35. }
  36. private void UpdateMoving()
  37. {
  38. Vector3 prev_pos = target_position;
  39. moving_direction = GetMovingDir();
  40. if(moving_direction.X == 0 && moving_direction.Y == 0 && moving_direction.Z == 0)
  41. {
  42. return;
  43. }
  44. Vector3 cur_moving_dir = moving_direction;
  45. if( Move() == null )
  46. {
  47. return;
  48. }
  49. moving_direction = cur_moving_dir;
  50. target_position = prev_pos;
  51. MoveSlide();
  52. }
  53. private bool Move()
  54. {
  55. if(moving_direction.X == 0 && moving_direction.Y == 0 && moving_direction.Z == 0)
  56. {
  57. return null;
  58. }
  59. desired_position = target_position + moving_direction;
  60. bool result = SweepTest(target_position, desired_position);
  61. if(result == false)
  62. {
  63. target_position = desired_position;
  64. camera.TargetPosition = target_position;
  65. return false;
  66. }
  67. return true;
  68. }
  69. private bool Slide()
  70. {
  71. }
  72. private void MoveSlide()
  73. {
  74. }
  75. private Vector3 GetMovingDir()
  76. {
  77. float speed = moving_speed;
  78. Vector3 dir = new Vector3(0, 0, 0);
  79. if(Input.Keyboard.IsKeyDown(Keys.W))
  80. {
  81. dir.Z += 1.0f;
  82. }
  83. if(Input.Keyboard.IsKeyDown(Keys.S))
  84. {
  85. dir.Z -= 1.0f;
  86. }
  87. if(Input.Keyboard.IsKeyDown(Keys.A))
  88. {
  89. dir.X -= 1.0f;
  90. }
  91. if(Input.Keyboard.IsKeyDown(Keys.D))
  92. {
  93. dir.X += 1.0f;
  94. }
  95. if (dir.X == 0 && dir.Y == 0 && dir.Z == 0)
  96. return dir;
  97. if (dir.Z < 0)
  98. speed *= 0.5;
  99. dir = Vector3.Transform(dir, camera.RotationMatrix);
  100. dir.Y = 0;
  101. dir.Normalize();
  102. dir *= speed;
  103. return dir;
  104. }
  105. private void ProcessInput(float ellapsedTime)
  106. {
  107. if(Input.IsMouseDragging)
  108. {
  109. float pixel_to_angle = 500.0f;
  110. //向量先绕up旋转
  111. Vector3 dir = camera.Position - desired_position;
  112. Matrix rot = Matrix.CreateFromAxisAngle(Vector3.Up, Input.MouseDeltaX * 3.14159 / pixel_to_angle);
  113. dir = Vector3.Transform(dir, rot);
  114. //绕up旋转之后的相机旋转矩阵
  115. Matrix cam_rot = camera.RotationMatrix * rot;
  116. //绕相机的right向量旋转(俯仰)
  117. rot = Matrix.CreateFromAxisAngle(cam_rot.Right, Input.MouseDeltaY * 3.14159 / pixel_to_angle);
  118. dir = Vector3.Transform(dir, rot);
  119. camera.Position = desired_position + dir;
  120. camera.RotationMatrix = Matrix.CreateLookAt(Vector3.Zero, -dir, cam_rot.Up);
  121. }
  122. }
  123. */
  124. }
  125. }