SplineCamera.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #pragma once
  9. #include "CameraComponent.h"
  10. #include <vector>
  11. struct Spline
  12. {
  13. // Control points for spline
  14. // (Requires n+2 points where n is number
  15. // of points in segment)
  16. std::vector<Vector3> mControlPoints;
  17. // Given spline segment where startIdx = P1,
  18. // compute position based on t value
  19. Vector3 Compute(size_t startIdx, float t) const;
  20. // Returns number of control points
  21. size_t GetNumPoints() const { return mControlPoints.size(); }
  22. };
  23. class SplineCamera : public CameraComponent
  24. {
  25. public:
  26. SplineCamera(class Actor* owner);
  27. void Update(float deltaTime) override;
  28. // Restart the spline
  29. void Restart();
  30. void SetSpeed(float speed) { mSpeed = speed; }
  31. void SetSpline(const Spline& spline) { mPath = spline; }
  32. void SetPaused(bool pause) { mPaused = pause; }
  33. private:
  34. // Spline path camera follows
  35. Spline mPath;
  36. // Current control point index and t
  37. size_t mIndex;
  38. float mT;
  39. // Amount t changes/sec
  40. float mSpeed;
  41. // Whether to move the camera long the path
  42. bool mPaused;
  43. };