CmDebugCamera.cpp 3.7 KB

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