CameraFlyer.cpp 3.5 KB

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