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 = 0.5f; // Degrees/pixel
  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. if (direction.squaredLength() != 0)
  54. {
  55. direction.normalize();
  56. float multiplier = 1.0f;
  57. if (fastMove)
  58. multiplier = FAST_MODE_MULTIPLIER;
  59. mCurrentSpeed = Math::clamp(mCurrentSpeed + ACCELERATION * gTime().getFrameDelta(), START_SPEED, TOP_SPEED);
  60. mCurrentSpeed *= multiplier;
  61. }
  62. else
  63. {
  64. mCurrentSpeed = 0.0f;
  65. }
  66. float tooSmall = std::numeric_limits<float>::epsilon();
  67. if (mCurrentSpeed > tooSmall)
  68. {
  69. Vector3 velocity = direction * mCurrentSpeed;
  70. SO()->move(velocity * gTime().getFrameDelta());
  71. }
  72. if (camRotating)
  73. {
  74. mYaw += Degree(gVirtualInput().getAxisValue(mHorizontalAxis) * ROTATION_SPEED);
  75. mPitch += Degree(gVirtualInput().getAxisValue(mVerticalAxis) * ROTATION_SPEED);
  76. Quaternion yRot;
  77. yRot.fromAxisAngle(Vector3::UNIT_Y, Radian(mYaw));
  78. Quaternion xRot;
  79. xRot.fromAxisAngle(yRot.xAxis(), Radian(mPitch));
  80. Quaternion camRot = xRot * yRot;
  81. SO()->setRotation(camRot);
  82. }
  83. LOGWRN(toString(SO()->getPosition()));
  84. }
  85. }