A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
KartDynamics.hh
1#pragma once
2
3#include <egg/math/Matrix.hh>
4
5namespace Kinoko::Kart {
6
12class KartDynamics {
13public:
14 KartDynamics();
15 virtual ~KartDynamics();
16
17 virtual void forceUpright() {}
18 virtual void stabilize();
19
20 void init();
21
22 void resetInternalVelocity() {
23 m_intVel.setZero();
24 }
25
26 void setInertia(const EGG::Vector3f &m, const EGG::Vector3f &n);
27 void setBspParams(f32 rotSpeed, const EGG::Vector3f &m, const EGG::Vector3f &n,
28 bool skipInertia);
29
30 void calc(f32 maxSpeed, bool air);
31 void reset();
32
33 void applySuspensionWrench(const EGG::Vector3f &p, const EGG::Vector3f &Flinear,
34 const EGG::Vector3f &Frot, bool ignoreX);
35 void applyWrenchScaled(const EGG::Vector3f &p, const EGG::Vector3f &f, f32 scale);
36
38 void addForce(const EGG::Vector3f &pos) {
39 m_totalForce += pos;
40 }
41
42 [[nodiscard]] EGG::Matrix34f calcWorldInertiaTensor() const;
43
45 void setPos(const EGG::Vector3f &pos) {
46 m_pos = pos;
47 }
48
49 void setGravity(f32 gravity) {
50 m_gravity = gravity;
51 }
52
53 void setMainRot(const EGG::Quatf &q) {
54 m_mainRot = q;
55 }
56
57 void setFullRot(const EGG::Quatf &q) {
58 m_fullRot = q;
59 }
60
61 void setSpecialRot(const EGG::Quatf &q) {
62 m_specialRot = q;
63 }
64
65 void setExtraRot(const EGG::Quatf &q) {
66 m_extraRot = q;
67 }
68
69 void setIntVel(const EGG::Vector3f &v) {
70 m_intVel = v;
71 }
72
73 void setTop(const EGG::Vector3f &v) {
74 m_top = v;
75 }
76
77 void setStabilizationFactor(f32 val) {
79 }
80
81 void setTotalForce(const EGG::Vector3f &v) {
82 m_totalForce = v;
83 }
84
85 void setExtVel(const EGG::Vector3f &v) {
86 m_extVel = v;
87 }
88
89 void setAngVel0(const EGG::Vector3f &v) {
90 m_angVel0 = v;
91 }
92
93 void setMovingObjVel(const EGG::Vector3f &v) {
95 }
96
97 void setMovingRoadVel(const EGG::Vector3f &v) {
99 }
100
101 void setAngVel2(const EGG::Vector3f &v) {
102 m_angVel2 = v;
103 }
104
105 void setAngVel0YFactor(f32 val) {
106 m_angVel0YFactor = val;
107 }
108
109 void setScale(const EGG::Vector3f &v) {
110 m_scale = v;
111 }
112
113 void setTop_(const EGG::Vector3f &v) {
114 m_top_ = v;
115 }
116
117 void setForceUpright(bool isSet) {
118 m_forceUpright = isSet;
119 }
120
121 void setNoGravity(bool isSet) {
122 m_noGravity = isSet;
123 }
124
125 void setKillExtVelY(bool isSet) {
126 m_killExtVelY = isSet;
127 }
129
131 [[nodiscard]] const EGG::Matrix34f &invInertiaTensor() const {
132 return m_invInertiaTensor;
133 }
134
135 [[nodiscard]] f32 angVel0Factor() const {
136 return m_angVel0Factor;
137 }
138
139 [[nodiscard]] const EGG::Vector3f &pos() const {
140 return m_pos;
141 }
142
143 [[nodiscard]] const EGG::Vector3f &velocity() const {
144 return m_velocity;
145 }
146
147 [[nodiscard]] f32 gravity() const {
148 return m_gravity;
149 }
150
151 [[nodiscard]] const EGG::Vector3f &intVel() const {
152 return m_intVel;
153 }
154
155 [[nodiscard]] const EGG::Quatf &mainRot() const {
156 return m_mainRot;
157 }
158
159 [[nodiscard]] const EGG::Quatf &fullRot() const {
160 return m_fullRot;
161 }
162
163 [[nodiscard]] const EGG::Vector3f &totalForce() const {
164 return m_totalForce;
165 }
166
167 [[nodiscard]] const EGG::Vector3f &extVel() const {
168 return m_extVel;
169 }
170
171 [[nodiscard]] const EGG::Vector3f &angVel0() const {
172 return m_angVel0;
173 }
174
175 [[nodiscard]] const EGG::Vector3f &movingObjVel() const {
176 return m_movingObjVel;
177 }
178
179 [[nodiscard]] const EGG::Vector3f &movingRoadVel() const {
180 return m_movingRoadVel;
181 }
182
183 [[nodiscard]] const EGG::Vector3f &angVel2() const {
184 return m_angVel2;
185 }
186
187 [[nodiscard]] f32 speedFix() const {
188 return m_speedFix;
189 }
191
192protected:
216 f32 m_speedFix;
218
220 EGG::Vector3f m_scale;
224};
225
229class KartDynamicsBike : public KartDynamics {
230public:
231 KartDynamicsBike();
232 ~KartDynamicsBike();
233
234private:
235 void forceUpright() override;
236 void stabilize() override;
237};
238
239} // namespace Kinoko::Kart
A 3 x 4 matrix.
Definition Matrix.hh:10
void stabilize() override
Stabilizes the bike by rotating towards the y-axis unit vector.
void setBspParams(f32 rotSpeed, const EGG::Vector3f &m, const EGG::Vector3f &n, bool skipInertia)
On init, takes elements from the kart's BSP and computes the moment of inertia tensor.
EGG::Matrix34f m_inertiaTensor
Resistance to rotational change, as a 3x3 matrix.
void applySuspensionWrench(const EGG::Vector3f &p, const EGG::Vector3f &Flinear, const EGG::Vector3f &Frot, bool ignoreX)
Every frame, computes torque from linear motion and rotation.
f32 m_angVel0YFactor
Scalar for damping angular velocity.
EGG::Quatf m_extraRot
[Unused]
virtual void stabilize()
Stabilizes the kart by rotating towards the y-axis unit vector.
bool m_noGravity
Disables gravity. Relevant when respawning.
EGG::Quatf m_mainRot
Rotation based on the angular velocities.
EGG::Vector3f m_extVel
Velocity induced by collisions.
void applyWrenchScaled(const EGG::Vector3f &p, const EGG::Vector3f &f, f32 scale)
Applies a force linearly and rotationally to the kart.
void calc(f32 maxSpeed, bool air)
Every frame, computes acceleration, velocity, position and rotation of the kart.
EGG::Vector3f m_angVel1
[Unused]
EGG::Vector3f m_velocity
Sum of the linear velocities.
EGG::Vector3f m_intVel
What you typically consider to be the vehicle's speed.
f32 m_angVel0Factor
Scalar for damping angular velocity.
EGG::Vector3f m_totalForce
Basically just gravity.
EGG::Quatf m_fullRot
The combination of the other rotations.
EGG::Vector3f m_movingObjVel
Velocity from things like TF conveyers.
f32 m_speedNorm
Min of the max speed and m_velocity magnitude.
bool m_killExtVelY
Caps external velocity at 0.
EGG::Vector3f m_totalTorque
Torque from linear motion and rotation.
EGG::Vector3f m_angVel0
Angular velocity from m_totalTorque.
EGG::Vector3f m_movingRoadVel
Velocity from Koopa Cape water.
EGG::Vector3f m_top_
Basically m_top biased towards absolute up.
EGG::Vector3f m_top
The unit vector pointing up from the vehicle.
EGG::Vector3f m_angVel2
The main component of angular velocity.
f32 m_stabilizationFactor
Scalar for damping the main rotation.
f32 m_gravity
Usually -1.3f, also affected by KartMove::calcDive.
bool m_forceUpright
Specifies if we should return the vehicle to upwards orientation.
EGG::Vector3f m_pos
The vehicle's position.
EGG::Vector3f m_acceleration
Basically just m_totalForce.
EGG::Quatf m_specialRot
Rotation from trick animations. Copied from KartPhysics.
EGG::Matrix34f m_invInertiaTensor
The inverse of m_inertiaTensor.
Pertains to kart-related functionality.
A quaternion, used to represent 3D rotation.
Definition Quat.hh:12
A 3D float vector.
Definition Vector.hh:107