CameraFlyer.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "CameraFlyer.h"
  4. #include "Math/BsVector3.h"
  5. #include "Utility/BsTime.h"
  6. #include "Math/BsMath.h"
  7. #include "Scene/BsSceneObject.h"
  8. #include "Components/BsCCamera.h"
  9. #include "Platform/BsCursor.h"
  10. namespace bs
  11. {
  12. const float CameraFlyer::START_SPEED = 4.0f;
  13. const float CameraFlyer::TOP_SPEED = 13.0f;
  14. const float CameraFlyer::ACCELERATION = 1.0f;
  15. const float CameraFlyer::FAST_MODE_MULTIPLIER = 2.0f;
  16. const float CameraFlyer::ROTATION_SPEED = 360.0f; // Degrees/second
  17. /** Wraps an angle so it always stays in [0, 360) range. */
  18. Degree wrapAngle(Degree angle)
  19. {
  20. if (angle.valueDegrees() < -360.0f)
  21. angle += Degree(360.0f);
  22. if (angle.valueDegrees() > 360.0f)
  23. angle -= Degree(360.0f);
  24. return angle;
  25. }
  26. CameraFlyer::CameraFlyer(const HSceneObject& parent)
  27. :Component(parent), mPitch(0.0f), mYaw(0.0f), mLastButtonState(false)
  28. {
  29. // Set a name for the component, so we can find it later if needed
  30. setName("CameraFlyer");
  31. // Get handles for key bindings. Actual keys attached to these bindings will be registered during app start-up.
  32. mMoveForward = VirtualButton("Forward");
  33. mMoveBack = VirtualButton("Back");
  34. mMoveLeft = VirtualButton("Left");
  35. mMoveRight = VirtualButton("Right");
  36. mFastMove = VirtualButton("FastMove");
  37. mRotateCam = VirtualButton("RotateCam");
  38. mHorizontalAxis = VirtualAxis("Horizontal");
  39. mVerticalAxis = VirtualAxis("Vertical");
  40. }
  41. void CameraFlyer::update()
  42. {
  43. // Check if any movement or rotation keys are being held
  44. bool goingForward = gVirtualInput().isButtonHeld(mMoveForward);
  45. bool goingBack = gVirtualInput().isButtonHeld(mMoveBack);
  46. bool goingLeft = gVirtualInput().isButtonHeld(mMoveLeft);
  47. bool goingRight = gVirtualInput().isButtonHeld(mMoveRight);
  48. bool fastMove = gVirtualInput().isButtonHeld(mFastMove);
  49. bool camRotating = gVirtualInput().isButtonHeld(mRotateCam);
  50. // If switch to or from rotation mode, hide or show the cursor
  51. if (camRotating != mLastButtonState)
  52. {
  53. if (camRotating)
  54. Cursor::instance().hide();
  55. else
  56. Cursor::instance().show();
  57. mLastButtonState = camRotating;
  58. }
  59. // If camera is rotating, apply new pitch/yaw rotation values depending on the amount of rotation from the
  60. // vertical/horizontal axes.
  61. float frameDelta = gTime().getFrameDelta();
  62. if (camRotating)
  63. {
  64. mYaw += Degree(gVirtualInput().getAxisValue(mHorizontalAxis) * ROTATION_SPEED * frameDelta);
  65. mPitch += Degree(gVirtualInput().getAxisValue(mVerticalAxis) * ROTATION_SPEED * frameDelta);
  66. mYaw = wrapAngle(mYaw);
  67. mPitch = wrapAngle(mPitch);
  68. Quaternion yRot;
  69. yRot.fromAxisAngle(Vector3::UNIT_Y, Radian(mYaw));
  70. Quaternion xRot;
  71. xRot.fromAxisAngle(Vector3::UNIT_X, Radian(mPitch));
  72. Quaternion camRot = yRot * xRot;
  73. camRot.normalize();
  74. SO()->setRotation(camRot);
  75. }
  76. // If the movement button is pressed, determine direction to move in
  77. Vector3 direction = Vector3::ZERO;
  78. if (goingForward) direction += SO()->getForward();
  79. if (goingBack) direction -= SO()->getForward();
  80. if (goingRight) direction += SO()->getRight();
  81. if (goingLeft) direction -= SO()->getRight();
  82. // If a direction is chosen, normalize it to determine final direction.
  83. if (direction.squaredLength() != 0)
  84. {
  85. direction.normalize();
  86. // Apply fast move multiplier if the fast move button is held.
  87. float multiplier = 1.0f;
  88. if (fastMove)
  89. multiplier = FAST_MODE_MULTIPLIER;
  90. // Calculate current speed of the camera
  91. mCurrentSpeed = Math::clamp(mCurrentSpeed + ACCELERATION * frameDelta, START_SPEED, TOP_SPEED);
  92. mCurrentSpeed *= multiplier;
  93. }
  94. else
  95. {
  96. mCurrentSpeed = 0.0f;
  97. }
  98. // If the current speed isn't too small, move the camera in the wanted direction
  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. }