CmDebugCamera.cpp 2.7 KB

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