2
0

OrbitActor.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "OrbitActor.h"
  9. #include "MeshComponent.h"
  10. #include "Game.h"
  11. #include "Renderer.h"
  12. #include "OrbitCamera.h"
  13. #include "MoveComponent.h"
  14. OrbitActor::OrbitActor(Game* game)
  15. :Actor(game)
  16. {
  17. mMeshComp = new MeshComponent(this);
  18. mMeshComp->SetMesh(game->GetRenderer()->GetMesh("Assets/RacingCar.gpmesh"));
  19. SetPosition(Vector3(0.0f, 0.0f, -100.0f));
  20. mCameraComp = new OrbitCamera(this);
  21. }
  22. void OrbitActor::ActorInput(const uint8_t* keys)
  23. {
  24. // Mouse rotation
  25. // Get relative movement from SDL
  26. int x, y;
  27. Uint32 buttons = SDL_GetRelativeMouseState(&x, &y);
  28. // Only apply rotation if right-click is held
  29. if (buttons & SDL_BUTTON(SDL_BUTTON_RIGHT))
  30. {
  31. // Assume mouse movement is usually between -500 and +500
  32. const int maxMouseSpeed = 500;
  33. // Rotation/sec at maximum speed
  34. const float maxOrbitSpeed = Math::Pi * 8;
  35. float yawSpeed = 0.0f;
  36. if (x != 0)
  37. {
  38. // Convert to ~[-1.0, 1.0]
  39. yawSpeed = static_cast<float>(x) / maxMouseSpeed;
  40. // Multiply by rotation/sec
  41. yawSpeed *= maxOrbitSpeed;
  42. }
  43. mCameraComp->SetYawSpeed(-yawSpeed);
  44. // Compute pitch
  45. float pitchSpeed = 0.0f;
  46. if (y != 0)
  47. {
  48. // Convert to ~[-1.0, 1.0]
  49. pitchSpeed = static_cast<float>(y) / maxMouseSpeed;
  50. pitchSpeed *= maxOrbitSpeed;
  51. }
  52. mCameraComp->SetPitchSpeed(pitchSpeed);
  53. }
  54. }
  55. void OrbitActor::SetVisible(bool visible)
  56. {
  57. mMeshComp->SetVisible(visible);
  58. }