CmDebugCamera.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. setName("DebugCamera");
  21. mCamera = sceneObject()->getComponent<Camera>();
  22. mCamera->setNearClipDistance(5);
  23. sceneObject()->setPosition(Vector3(0,0,0));
  24. sceneObject()->lookAt(Vector3(0,0,-1));
  25. }
  26. void DebugCamera::update()
  27. {
  28. bool goingForward = gInput().isButtonHeld(BC_W) || gInput().isButtonHeld(BC_UP);
  29. bool goingBack = gInput().isButtonHeld(BC_S) || gInput().isButtonHeld(BC_DOWN);
  30. bool goingLeft = gInput().isButtonHeld(BC_A) || gInput().isButtonHeld(BC_LEFT);
  31. bool goingRight = gInput().isButtonHeld(BC_D) || gInput().isButtonHeld(BC_RIGHT);
  32. bool fastMove = gInput().isButtonHeld(BC_LSHIFT);
  33. bool camRotating = gInput().isButtonHeld(BC_MOUSE_RIGHT);
  34. if(camRotating != mLastButtonState)
  35. {
  36. if(camRotating)
  37. Platform::hideCursor();
  38. else
  39. Platform::showCursor();
  40. mLastButtonState = camRotating;
  41. }
  42. Vector3 direction = Vector3::ZERO;
  43. if (goingForward) direction += SO()->getForward();
  44. if (goingBack) direction -= SO()->getForward();
  45. if (goingRight) direction += SO()->getRight();
  46. if (goingLeft) direction -= SO()->getRight();
  47. if (direction.squaredLength() != 0)
  48. {
  49. direction.normalize();
  50. float multiplier = 1.0f;
  51. if(fastMove)
  52. multiplier = FAST_MODE_MULTIPLIER;
  53. mCurrentSpeed = Math::clamp(mCurrentSpeed + ACCELERATION * gTime().getFrameDelta(), START_SPEED, TOP_SPEED);
  54. mCurrentSpeed *= multiplier;
  55. }
  56. else
  57. {
  58. mCurrentSpeed = 0.0f;
  59. }
  60. float tooSmall = std::numeric_limits<float>::epsilon();
  61. if(mCurrentSpeed > tooSmall)
  62. {
  63. Vector3 velocity = direction * mCurrentSpeed;
  64. SO()->move(velocity * gTime().getFrameDelta());
  65. }
  66. if(camRotating)
  67. {
  68. mYaw += Degree(gInput().getHorizontalAxis() * ROTATION_SPEED);
  69. mPitch += Degree(gInput().getVerticalAxis() * ROTATION_SPEED);
  70. Quaternion yRot;
  71. yRot.fromAxisAngle(Vector3::UNIT_Y, Radian(mYaw));
  72. Quaternion xRot;
  73. xRot.fromAxisAngle(yRot.xAxis(), Radian(mPitch));
  74. Quaternion camRot = xRot * yRot;
  75. SO()->setRotation(camRot);
  76. }
  77. }
  78. }