A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
RailInterpolator.hh
1#pragma once
2
3#include <Common.hh>
4
5#include "game/field/Rail.hh"
6
7#include "game/system/map/MapdataPointInfo.hh"
8
9#include <egg/math/Vector.hh>
10
11namespace Field {
12
14public:
15 enum class Status {
16 InProgress = 0,
17 SegmentEnd = 1,
18 ChangingDirection = 2,
19 };
20
21 RailInterpolator(f32 speed, u32 idx);
22 virtual ~RailInterpolator();
23
24 virtual void init(f32 t, u32 idx) = 0;
25 virtual Status calc() = 0;
26 virtual void setCurrVel(f32 speed) = 0;
27
28 virtual f32 getCurrVel() = 0;
29 virtual void evalCubicBezierOnPath(f32 t, EGG::Vector3f &currDir,
30 EGG::Vector3f &curTangentDir) = 0;
31 virtual void getPathLocation(f32 t, s16 &idx, f32 &len) = 0;
32
33 void setPerPointVelocities(bool isSet) {
34 m_usePerPointVelocities = isSet;
35 }
36
38 void setT(f32 t) {
39 m_segmentT = t;
40 }
41
42 [[nodiscard]] const EGG::Vector3f &floorNrm(size_t idx) const;
43 [[nodiscard]] f32 railLength() const;
44
45 [[nodiscard]] const System::MapdataPointInfo::Point &curPoint() const {
46 ASSERT(static_cast<size_t>(m_currPointIdx) < m_points.size());
47 return m_points[m_currPointIdx];
48 }
49
50 [[nodiscard]] const System::MapdataPointInfo::Point &nextPoint() const {
51 ASSERT(static_cast<size_t>(m_nextPointIdx) < m_points.size());
52 return m_points[m_nextPointIdx];
53 }
54
55 [[nodiscard]] f32 speed() const {
56 return m_speed;
57 }
58
59 [[nodiscard]] const EGG::Vector3f &curPos() const {
60 return m_curPos;
61 }
62
63 [[nodiscard]] const EGG::Vector3f &curTangentDir() const {
64 return m_curTangentDir;
65 }
66
67 [[nodiscard]] bool isMovementDirectionForward() const {
68 return m_movementDirectionForward;
69 }
70
71 [[nodiscard]] s16 curPointIdx() const {
72 return m_currPointIdx;
73 }
74
75 [[nodiscard]] s16 nextPointIdx() const {
76 return m_nextPointIdx;
77 }
78
79protected:
80 void updateVel();
81 void calcVelocities();
82 [[nodiscard]] bool shouldChangeDirection() const;
83 void calcDirectionChange();
84 void calcNextIndices();
85
86 s16 m_railIdx;
87 u16 m_pointCount;
88 std::span<System::MapdataPointInfo::Point> m_points;
89 bool m_isOscillating;
90 f32 m_speed;
91 bool m_usePerPointVelocities;
92 EGG::Vector3f m_curPos;
93 EGG::Vector3f m_curTangentDir;
94 f32 m_currVel;
95 f32 m_prevPointVel;
96 f32 m_nextPointVel;
97 f32 m_currSegmentVel;
98 f32 m_segmentT;
99 bool m_movementDirectionForward;
100 s16 m_currPointIdx;
101 s16 m_nextPointIdx;
102 bool m_4a;
103};
104
106public:
107 RailLinearInterpolator(f32 speed, u32 idx);
108 ~RailLinearInterpolator() override;
109
110 void init(f32 t, u32 idx) override;
111 Status calc() override;
112 void setCurrVel(f32 speed) override;
113
115 [[nodiscard]] f32 getCurrVel() override {
116 return m_currVel;
117 }
118
119 void evalCubicBezierOnPath(f32 t, EGG::Vector3f &currDir,
120 EGG::Vector3f &curTangentDir) override;
121 void getPathLocation(f32 t, s16 &idx, f32 &len) override;
122
123private:
124 void calcNextSegment();
125 EGG::Vector3f lerp(f32 t, u32 currIdx, u32 nextIdx) const;
126
127 EGG::Vector3f m_currentDirection;
128 std::span<RailLineTransition> m_transitions;
129};
130
132public:
133 RailSmoothInterpolator(f32 speed, u32 idx);
134 ~RailSmoothInterpolator() override;
135
136 void init(f32 t, u32 idx) override;
137 Status calc() override;
138 void setCurrVel(f32 speed) override;
139
141 [[nodiscard]] f32 getCurrVel() override {
142 return m_velocity;
143 }
144
145 void evalCubicBezierOnPath(f32 t, EGG::Vector3f &currDir,
146 EGG::Vector3f &curTangentDir) override;
147 void getPathLocation(f32 t, s16 &idx, f32 &len) override;
148
149private:
150 void calcCubicBezier(f32 t, u32 currIdx, u32 nextIdx, EGG::Vector3f &pos,
151 EGG::Vector3f &dir) const;
152 [[nodiscard]] EGG::Vector3f calcCubicBezierPos(f32 t, const RailSplineTransition &trans) const;
153 [[nodiscard]] EGG::Vector3f calcCubicBezierTangentDir(f32 t,
154 const RailSplineTransition &trans) const;
155 [[nodiscard]] f32 calcT(f32 t) const;
156 void calcNextSegment();
157
158 std::span<RailSplineTransition> m_transitions;
159 u32 m_estimatorSampleCount;
160 f32 m_estimatorStep;
161 std::span<f32> m_pathPercentages;
162 EGG::Vector3f m_prevPos;
163 f32 m_velocity;
164};
165
166} // namespace Field
This header houses common data types such as our integral types and enums.
Pertains to collision.
A 3D float vector.
Definition Vector.hh:87