2
0

BsSceneCameraController.cpp 3.8 KB

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