CmDebugCamera.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "CmDebugCamera.h"
  2. #include "CmInput.h"
  3. #include "CmVector3.h"
  4. #include "CmTime.h"
  5. #include "CmMath.h"
  6. #include "CmSceneObject.h"
  7. #include "BsCamera.h"
  8. #include "CmPlatform.h"
  9. using namespace BansheeEngine;
  10. namespace CamelotFramework
  11. {
  12. const float DebugCamera::START_SPEED = 40.0f;
  13. const float DebugCamera::TOP_SPEED = 130.0f;
  14. const float DebugCamera::ACCELERATION = 10.0f;
  15. const float DebugCamera::FAST_MODE_MULTIPLIER = 2.0f;
  16. const float DebugCamera::ROTATION_SPEED = 0.5f; // Degrees/pixel
  17. DebugCamera::DebugCamera(const HSceneObject& parent)
  18. :Component(parent), mPitch(0.0f), mYaw(0.0f), mLastButtonState(false)
  19. {
  20. mCamera = sceneObject()->getComponent<Camera>();
  21. mCamera->setNearClipDistance(5);
  22. sceneObject()->setPosition(Vector3(0,0,0));
  23. sceneObject()->lookAt(Vector3(0,0,-1));
  24. }
  25. void DebugCamera::update()
  26. {
  27. bool goingForward = gInput().isButtonHeld(BC_W) || gInput().isButtonHeld(BC_UP);
  28. bool goingBack = gInput().isButtonHeld(BC_S) || gInput().isButtonHeld(BC_DOWN);
  29. bool goingLeft = gInput().isButtonHeld(BC_A) || gInput().isButtonHeld(BC_LEFT);
  30. bool goingRight = gInput().isButtonHeld(BC_D) || gInput().isButtonHeld(BC_RIGHT);
  31. bool fastMove = gInput().isButtonHeld(BC_LSHIFT);
  32. bool camRotating = gInput().isButtonHeld(BC_MOUSE_RIGHT);
  33. if(camRotating != mLastButtonState)
  34. {
  35. if(camRotating)
  36. Platform::hideCursor();
  37. else
  38. Platform::showCursor();
  39. mLastButtonState = camRotating;
  40. }
  41. Vector3 direction = Vector3::ZERO;
  42. if (goingForward) direction += SO()->getForward();
  43. if (goingBack) direction -= SO()->getForward();
  44. if (goingRight) direction += SO()->getRight();
  45. if (goingLeft) direction -= SO()->getRight();
  46. if (direction.squaredLength() != 0)
  47. {
  48. direction.normalize();
  49. float multiplier = 1.0f;
  50. if(fastMove)
  51. multiplier = FAST_MODE_MULTIPLIER;
  52. mCurrentSpeed = Math::clamp(mCurrentSpeed + ACCELERATION * gTime().getFrameDelta(), START_SPEED, TOP_SPEED);
  53. mCurrentSpeed *= multiplier;
  54. }
  55. else
  56. {
  57. mCurrentSpeed = 0.0f;
  58. }
  59. float tooSmall = std::numeric_limits<float>::epsilon();
  60. if(mCurrentSpeed > tooSmall)
  61. {
  62. Vector3 velocity = direction * mCurrentSpeed;
  63. SO()->move(velocity * gTime().getFrameDelta());
  64. }
  65. if(camRotating)
  66. {
  67. mYaw += Degree(gInput().getHorizontalAxis() * ROTATION_SPEED);
  68. mPitch += Degree(gInput().getVerticalAxis() * ROTATION_SPEED);
  69. Quaternion yRot;
  70. yRot.fromAxisAngle(Vector3::UNIT_Y, Radian(mYaw));
  71. Quaternion xRot;
  72. xRot.fromAxisAngle(yRot.xAxis(), Radian(mPitch));
  73. Quaternion camRot = xRot * yRot;
  74. SO()->setRotation(camRot);
  75. }
  76. }
  77. }