CmDebugCamera.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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)
  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().isKeyDown(KC_W) || gInput().isKeyDown(KC_UP);
  30. bool goingBack = gInput().isKeyDown(KC_S) || gInput().isKeyDown(KC_DOWN);
  31. bool goingLeft = gInput().isKeyDown(KC_A) || gInput().isKeyDown(KC_LEFT);
  32. bool goingRight = gInput().isKeyDown(KC_D) || gInput().isKeyDown(KC_RIGHT);
  33. bool fastMove = gInput().isKeyDown(KC_LSHIFT);
  34. bool camRotating = gInput().isButtonDown(MB_Right);
  35. Vector3 direction = Vector3::ZERO;
  36. if (goingForward) direction += SO()->getForward();
  37. if (goingBack) direction -= SO()->getForward();
  38. if (goingRight) direction += SO()->getRight();
  39. if (goingLeft) direction -= SO()->getRight();
  40. if (direction.squaredLength() != 0)
  41. {
  42. direction.normalize();
  43. float multiplier = 1.0f;
  44. if(fastMove)
  45. multiplier = FAST_MODE_MULTIPLIER;
  46. mCurrentSpeed = Math::Clamp(mCurrentSpeed + ACCELERATION * gTime().getFrameDelta(), START_SPEED, TOP_SPEED);
  47. mCurrentSpeed *= multiplier;
  48. }
  49. else
  50. {
  51. mCurrentSpeed = 0.0f;
  52. }
  53. float tooSmall = std::numeric_limits<float>::epsilon();
  54. if(mCurrentSpeed > tooSmall)
  55. {
  56. Vector3 velocity = direction * mCurrentSpeed;
  57. SO()->move(velocity * gTime().getFrameDelta());
  58. }
  59. if(camRotating)
  60. {
  61. mYaw += Degree(gInput().getHorizontalAxis() * ROTATION_SPEED);
  62. mPitch += Degree(gInput().getVerticalAxis() * ROTATION_SPEED);
  63. Quaternion yRot;
  64. yRot.FromAngleAxis(Radian(mYaw), Vector3::UP);
  65. Quaternion xRot;
  66. xRot.FromAngleAxis(Radian(mPitch), yRot.xAxis());
  67. Quaternion camRot = xRot * yRot;
  68. SO()->setRotation(camRot);
  69. }
  70. }
  71. }