2
0

CmDebugCamera.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "CmDebugCamera.h"
  2. #include "CmInput.h"
  3. #include "CmVector3.h"
  4. #include "CmTime.h"
  5. #include "CmMath.h"
  6. #include "CmGameObject.h"
  7. #include "CmCamera.h"
  8. #include "CmOSCursor.h"
  9. namespace CamelotEngine
  10. {
  11. const float DebugCamera::START_SPEED = 40.0f;
  12. const float DebugCamera::TOP_SPEED = 130.0f;
  13. const float DebugCamera::ACCELERATION = 10.0f;
  14. const float DebugCamera::FAST_MODE_MULTIPLIER = 2.0f;
  15. const float DebugCamera::ROTATION_SPEED = 0.5f; // Degrees/pixel
  16. DebugCamera::DebugCamera(GameObjectPtr parent)
  17. :Component(parent), mGoingForward(false), mGoingBack(false), mGoingLeft(false), mGoingRight(false),
  18. mFastMove(false), mCameraRotating(false), mPitch(0.0f), mYaw(0.0f)
  19. {
  20. mCamera = gameObject()->getComponent<Camera>();
  21. mCamera->setNearClipDistance(5);
  22. //gameObject()->setPosition(Vector3(0,0,5050));
  23. //gameObject()->lookAt(Vector3(0,0,-300));
  24. gameObject()->setPosition(Vector3(0,0,0));
  25. gameObject()->lookAt(Vector3(0,0,-1));
  26. gInput().onKeyDown.connect(boost::bind(&DebugCamera::keyDown, this, _1));
  27. gInput().onKeyUp.connect(boost::bind(&DebugCamera::keyUp, this, _1));
  28. gInput().onMouseDown.connect(boost::bind(&DebugCamera::mouseDown, this, _1, _2));
  29. gInput().onMouseUp.connect(boost::bind(&DebugCamera::mouseUp, this, _1, _2));
  30. }
  31. void DebugCamera::keyDown(KeyCode keyCode)
  32. {
  33. if (keyCode == KC_W || keyCode == KC_UP)
  34. mGoingForward = true;
  35. else if (keyCode == KC_S || keyCode== KC_DOWN)
  36. mGoingBack = true;
  37. else if (keyCode == KC_A || keyCode == KC_LEFT)
  38. mGoingLeft = true;
  39. else if (keyCode == KC_D || keyCode == KC_RIGHT)
  40. mGoingRight = true;
  41. else if (keyCode == KC_LSHIFT)
  42. mFastMove = true;
  43. }
  44. void DebugCamera::keyUp(KeyCode keyCode)
  45. {
  46. if (keyCode == KC_W || keyCode == KC_UP)
  47. mGoingForward = false;
  48. else if (keyCode == KC_S || keyCode== KC_DOWN)
  49. mGoingBack = false;
  50. else if (keyCode == KC_A || keyCode == KC_LEFT)
  51. mGoingLeft = false;
  52. else if (keyCode == KC_D || keyCode == KC_RIGHT)
  53. mGoingRight = false;
  54. else if (keyCode == KC_LSHIFT)
  55. mFastMove = false;
  56. }
  57. void DebugCamera::mouseDown(const MouseEvent& event, MouseButton buttonID)
  58. {
  59. if(buttonID == MB_Right)
  60. {
  61. mCameraRotating = true;
  62. OSCursor::hide();
  63. }
  64. }
  65. void DebugCamera::mouseUp(const MouseEvent& event, MouseButton buttonID)
  66. {
  67. if(buttonID == MB_Right)
  68. {
  69. mCameraRotating = false;
  70. OSCursor::show();
  71. }
  72. }
  73. void DebugCamera::update()
  74. {
  75. Vector3 direction = Vector3::ZERO;
  76. if (mGoingForward) direction += GO()->getForward();
  77. if (mGoingBack) direction -= GO()->getForward();
  78. if (mGoingRight) direction += GO()->getRight();
  79. if (mGoingLeft) direction -= GO()->getRight();
  80. if (direction.squaredLength() != 0)
  81. {
  82. direction.normalise();
  83. float multiplier = 1.0f;
  84. if(mFastMove)
  85. multiplier = FAST_MODE_MULTIPLIER;
  86. mCurrentSpeed = Math::Clamp(mCurrentSpeed + ACCELERATION * gTime().getFrameDelta(), START_SPEED, TOP_SPEED);
  87. mCurrentSpeed *= multiplier;
  88. }
  89. else
  90. {
  91. mCurrentSpeed = 0.0f;
  92. }
  93. float tooSmall = std::numeric_limits<float>::epsilon();
  94. if(mCurrentSpeed > tooSmall)
  95. {
  96. Vector3 velocity = direction * mCurrentSpeed;
  97. GO()->move(velocity * gTime().getFrameDelta());
  98. }
  99. if(mCameraRotating)
  100. {
  101. mYaw += Degree(gInput().getHorizontalAxis() * ROTATION_SPEED);
  102. mPitch += Degree(gInput().getVerticalAxis() * ROTATION_SPEED);
  103. Quaternion yRot;
  104. yRot.FromAngleAxis(Radian(mYaw), Vector3::UP);
  105. Quaternion xRot;
  106. xRot.FromAngleAxis(Radian(mPitch), yRot.xAxis());
  107. Quaternion camRot = xRot * yRot;
  108. GO()->setRotation(camRot);
  109. }
  110. }
  111. }