BsSceneCameraController.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "BsSceneCameraController.h"
  2. #include "BsVirtualInput.h"
  3. #include "BsVector3.h"
  4. #include "BsTime.h"
  5. #include "BsMath.h"
  6. #include "BsSceneObject.h"
  7. #include "BsCamera.h"
  8. #include "BsPlatform.h"
  9. #include "BsEditorApplication.h"
  10. #include "BsCursor.h"
  11. namespace BansheeEngine
  12. {
  13. const String SceneCameraController::MOVE_FORWARD_BTN = "SceneForward";
  14. const String SceneCameraController::MOVE_LEFT_BTN = "SceneLeft";
  15. const String SceneCameraController::MOVE_RIGHT_BTN = "SceneRight";
  16. const String SceneCameraController::MOVE_BACKWARD_BTN = "SceneBackward";
  17. const String SceneCameraController::FAST_MOVE_BTN = "SceneFastMove";
  18. const String SceneCameraController::ROTATE_BTN = "SceneRotate";
  19. const String SceneCameraController::HORIZONTAL_AXIS = "SceneHorizontal";
  20. const String SceneCameraController::VERTICAL_AXIS = "SceneVertical";
  21. const float SceneCameraController::START_SPEED = 4.0f;
  22. const float SceneCameraController::TOP_SPEED = 12.0f;
  23. const float SceneCameraController::ACCELERATION = 1.0f;
  24. const float SceneCameraController::FAST_MODE_MULTIPLIER = 2.0f;
  25. const float SceneCameraController::ROTATION_SPEED = 360.0f; // Degrees/second
  26. Degree wrapAngle(Degree angle)
  27. {
  28. if (angle.valueDegrees() < -360.0f)
  29. angle += Degree(360.0f);
  30. if (angle.valueDegrees() > 360.0f)
  31. angle -= Degree(360.0f);
  32. return angle;
  33. }
  34. SceneCameraController::SceneCameraController(const HSceneObject& parent)
  35. :Component(parent), mPitch(0.0f), mYaw(0.0f), mLastButtonState(false)
  36. {
  37. setName("SceneCameraController");
  38. mMoveForward = VirtualButton(MOVE_FORWARD_BTN);
  39. mMoveLeft = VirtualButton(MOVE_LEFT_BTN);
  40. mMoveRight = VirtualButton(MOVE_RIGHT_BTN);
  41. mMoveBackward = VirtualButton(MOVE_BACKWARD_BTN);
  42. mFastMove = VirtualButton(FAST_MOVE_BTN);
  43. mRotate = VirtualButton(ROTATE_BTN);
  44. mHorizontal = VirtualAxis(HORIZONTAL_AXIS);
  45. mVertical = VirtualAxis(VERTICAL_AXIS);
  46. }
  47. void SceneCameraController::update()
  48. {
  49. if (!gEditorApplication().isSceneViewFocused())
  50. return;
  51. bool goingForward = gVirtualInput().isButtonHeld(mMoveForward);
  52. bool goingBack = gVirtualInput().isButtonHeld(mMoveBackward);
  53. bool goingLeft = gVirtualInput().isButtonHeld(mMoveLeft);
  54. bool goingRight = gVirtualInput().isButtonHeld(mMoveRight);
  55. bool fastMove = gVirtualInput().isButtonHeld(mFastMove);
  56. bool camRotating = gVirtualInput().isButtonHeld(mRotate);
  57. if (camRotating != mLastButtonState)
  58. {
  59. if (camRotating)
  60. Cursor::instance().hide();
  61. else
  62. Cursor::instance().show();
  63. mLastButtonState = camRotating;
  64. }
  65. float frameDelta = gTime().getFrameDelta();
  66. if (camRotating)
  67. {
  68. mYaw += Degree(gVirtualInput().getAxisValue(mHorizontal) * ROTATION_SPEED * frameDelta);
  69. mPitch += Degree(gVirtualInput().getAxisValue(mVertical) * ROTATION_SPEED * frameDelta);
  70. mYaw = wrapAngle(mYaw);
  71. mPitch = wrapAngle(mPitch);
  72. Quaternion yRot;
  73. yRot.fromAxisAngle(Vector3::UNIT_Y, Radian(mYaw));
  74. Quaternion xRot;
  75. xRot.fromAxisAngle(Vector3::UNIT_X, Radian(mPitch));
  76. Quaternion camRot = yRot * xRot;
  77. camRot.normalize();
  78. SO()->setRotation(camRot);
  79. }
  80. Vector3 direction = Vector3::ZERO;
  81. if (goingForward) direction += SO()->getForward();
  82. if (goingBack) direction -= SO()->getForward();
  83. if (goingRight) direction += SO()->getRight();
  84. if (goingLeft) direction -= SO()->getRight();
  85. if (direction.squaredLength() != 0)
  86. {
  87. direction.normalize();
  88. float multiplier = 1.0f;
  89. if (fastMove)
  90. multiplier = FAST_MODE_MULTIPLIER;
  91. mCurrentSpeed = Math::clamp(mCurrentSpeed + ACCELERATION * frameDelta, START_SPEED, TOP_SPEED);
  92. mCurrentSpeed *= multiplier;
  93. }
  94. else
  95. {
  96. mCurrentSpeed = 0.0f;
  97. }
  98. float tooSmall = std::numeric_limits<float>::epsilon();
  99. if (mCurrentSpeed > tooSmall)
  100. {
  101. Vector3 velocity = direction * mCurrentSpeed;
  102. SO()->move(velocity * frameDelta);
  103. }
  104. }
  105. }