CameraFlyer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "CameraFlyer.h"
  2. #include "BsVector3.h"
  3. #include "BsTime.h"
  4. #include "BsMath.h"
  5. #include "BsSceneObject.h"
  6. #include "BsCCamera.h"
  7. #include "BsPlatform.h"
  8. #include "BsCursor.h"
  9. #include "BsDebug.h"
  10. namespace BansheeEngine
  11. {
  12. const float CameraFlyer::START_SPEED = 40.0f;
  13. const float CameraFlyer::TOP_SPEED = 130.0f;
  14. const float CameraFlyer::ACCELERATION = 10.0f;
  15. const float CameraFlyer::FAST_MODE_MULTIPLIER = 2.0f;
  16. const float CameraFlyer::ROTATION_SPEED = 360.0f; // Degrees/second
  17. Degree wrapAngle(Degree angle)
  18. {
  19. if (angle.valueDegrees() < -360.0f)
  20. angle += Degree(360.0f);
  21. if (angle.valueDegrees() > 360.0f)
  22. angle -= Degree(360.0f);
  23. return angle;
  24. }
  25. CameraFlyer::CameraFlyer(const HSceneObject& parent)
  26. :Component(parent), mPitch(0.0f), mYaw(0.0f), mLastButtonState(false)
  27. {
  28. setName("CameraFlyer");
  29. mCamera = sceneObject()->getComponent<CCamera>();
  30. mCamera->setNearClipDistance(5);
  31. mMoveForward = VirtualButton("Forward");
  32. mMoveBack = VirtualButton("Back");
  33. mMoveLeft = VirtualButton("Left");
  34. mMoveRight = VirtualButton("Right");
  35. mFastMove = VirtualButton("FastMove");
  36. mRotateCam = VirtualButton("RotateCam");
  37. mHorizontalAxis = VirtualAxis("Horizontal");
  38. mVerticalAxis = VirtualAxis("Vertical");
  39. }
  40. void CameraFlyer::update()
  41. {
  42. bool goingForward = gVirtualInput().isButtonHeld(mMoveForward);
  43. bool goingBack = gVirtualInput().isButtonHeld(mMoveBack);
  44. bool goingLeft = gVirtualInput().isButtonHeld(mMoveLeft);
  45. bool goingRight = gVirtualInput().isButtonHeld(mMoveRight);
  46. bool fastMove = gVirtualInput().isButtonHeld(mFastMove);
  47. bool camRotating = gVirtualInput().isButtonHeld(mRotateCam);
  48. if (camRotating != mLastButtonState)
  49. {
  50. if (camRotating)
  51. Cursor::instance().hide();
  52. else
  53. Cursor::instance().show();
  54. mLastButtonState = camRotating;
  55. }
  56. float frameDelta = gTime().getFrameDelta();
  57. if (camRotating)
  58. {
  59. mYaw += Degree(gVirtualInput().getAxisValue(mHorizontalAxis) * ROTATION_SPEED * frameDelta);
  60. mPitch += Degree(gVirtualInput().getAxisValue(mVerticalAxis) * ROTATION_SPEED * frameDelta);
  61. mYaw = wrapAngle(mYaw);
  62. mPitch = wrapAngle(mPitch);
  63. Quaternion yRot;
  64. yRot.fromAxisAngle(Vector3::UNIT_Y, Radian(mYaw));
  65. Quaternion xRot;
  66. xRot.fromAxisAngle(Vector3::UNIT_X, Radian(mPitch));
  67. Quaternion camRot = yRot * xRot;
  68. camRot.normalize();
  69. SO()->setRotation(camRot);
  70. }
  71. Vector3 direction = Vector3::ZERO;
  72. if (goingForward) direction += SO()->getForward();
  73. if (goingBack) direction -= SO()->getForward();
  74. if (goingRight) direction += SO()->getRight();
  75. if (goingLeft) direction -= SO()->getRight();
  76. if (direction.squaredLength() != 0)
  77. {
  78. direction.normalize();
  79. float multiplier = 1.0f;
  80. if (fastMove)
  81. multiplier = FAST_MODE_MULTIPLIER;
  82. mCurrentSpeed = Math::clamp(mCurrentSpeed + ACCELERATION * frameDelta, START_SPEED, TOP_SPEED);
  83. mCurrentSpeed *= multiplier;
  84. }
  85. else
  86. {
  87. mCurrentSpeed = 0.0f;
  88. }
  89. float tooSmall = std::numeric_limits<float>::epsilon();
  90. if (mCurrentSpeed > tooSmall)
  91. {
  92. Vector3 velocity = direction * mCurrentSpeed;
  93. SO()->move(velocity * frameDelta);
  94. }
  95. }
  96. }