BsSceneCameraController.cpp 4.2 KB

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