CameraFlyer.cpp 3.4 KB

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