CameraFlyer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "CameraFlyer.h"
  2. #include "BsVector3.h"
  3. #include "BsTime.h"
  4. #include "BsMath.h"
  5. #include "BsSceneObject.h"
  6. #include "BsCamera.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. CameraFlyer::CameraFlyer(const HSceneObject& parent)
  18. :Component(parent), mPitch(0.0f), mYaw(0.0f), mLastButtonState(false)
  19. {
  20. setName("CameraFlyer");
  21. mCamera = sceneObject()->getComponent<Camera>();
  22. mCamera->setNearClipDistance(5);
  23. mMoveForward = VirtualButton("Forward");
  24. mMoveBack = VirtualButton("Back");
  25. mMoveLeft = VirtualButton("Left");
  26. mMoveRight = VirtualButton("Right");
  27. mFastMove = VirtualButton("FastMove");
  28. mRotateCam = VirtualButton("RotateCam");
  29. mHorizontalAxis = VirtualAxis("Horizontal");
  30. mVerticalAxis = VirtualAxis("Vertical");
  31. }
  32. void CameraFlyer::update()
  33. {
  34. bool goingForward = gVirtualInput().isButtonHeld(mMoveForward);
  35. bool goingBack = gVirtualInput().isButtonHeld(mMoveBack);
  36. bool goingLeft = gVirtualInput().isButtonHeld(mMoveLeft);
  37. bool goingRight = gVirtualInput().isButtonHeld(mMoveRight);
  38. bool fastMove = gVirtualInput().isButtonHeld(mFastMove);
  39. bool camRotating = gVirtualInput().isButtonHeld(mRotateCam);
  40. if (camRotating != mLastButtonState)
  41. {
  42. if (camRotating)
  43. Cursor::instance().hide();
  44. else
  45. Cursor::instance().show();
  46. mLastButtonState = camRotating;
  47. }
  48. Vector3 direction = Vector3::ZERO;
  49. if (goingForward) direction += SO()->getForward();
  50. if (goingBack) direction -= SO()->getForward();
  51. if (goingRight) direction += SO()->getRight();
  52. if (goingLeft) direction -= SO()->getRight();
  53. float frameDelta = gTime().getFrameDelta();
  54. if (direction.squaredLength() != 0)
  55. {
  56. direction.normalize();
  57. float multiplier = 1.0f;
  58. if (fastMove)
  59. multiplier = FAST_MODE_MULTIPLIER;
  60. mCurrentSpeed = Math::clamp(mCurrentSpeed + ACCELERATION * frameDelta, START_SPEED, TOP_SPEED);
  61. mCurrentSpeed *= multiplier;
  62. }
  63. else
  64. {
  65. mCurrentSpeed = 0.0f;
  66. }
  67. float tooSmall = std::numeric_limits<float>::epsilon();
  68. if (mCurrentSpeed > tooSmall)
  69. {
  70. Vector3 velocity = direction * mCurrentSpeed;
  71. SO()->move(velocity * frameDelta);
  72. }
  73. if (camRotating)
  74. {
  75. mYaw += Degree(gVirtualInput().getAxisValue(mHorizontalAxis) * ROTATION_SPEED * frameDelta);
  76. mPitch += Degree(gVirtualInput().getAxisValue(mVerticalAxis) * ROTATION_SPEED * frameDelta);
  77. Quaternion yRot;
  78. yRot.fromAxisAngle(Vector3::UNIT_Y, Radian(mYaw));
  79. Quaternion xRot;
  80. xRot.fromAxisAngle(yRot.xAxis(), Radian(mPitch));
  81. Quaternion camRot = xRot * yRot;
  82. SO()->setRotation(camRot);
  83. }
  84. }
  85. }