A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
KartDynamics.cc
Go to the documentation of this file.
1#include "KartDynamics.hh"
2
3#include <egg/math/Math.hh>
4
5namespace Kinoko::Kart {
6
14
17
22 if (EGG::Mathf::abs(top.dot(m_top)) >= 0.9999f) {
23 return;
24 }
25
26 EGG::Quatf q;
29}
30
61
64 constexpr f32 TWELFTH = 1.0f / 12.0f;
65
67 m_inertiaTensor[0, 0] = TWELFTH * (m.y * m.y + m.z * m.z) + (n.y * n.y + n.z * n.z);
68 m_inertiaTensor[1, 1] = TWELFTH * (m.z * m.z + m.x * m.x) + (n.z * n.z + n.x * n.x);
69 m_inertiaTensor[2, 2] = TWELFTH * (m.x * m.x + m.y * m.y) + (n.x * n.x + n.y * n.y);
71}
72
76 bool skipInertia) {
77 m_angVel0Factor = rotSpeed;
78
79 if (skipInertia) {
80 return;
81 }
82
83 setInertia(m, n);
84}
85
90void KartDynamics::calc(f32 maxSpeed, bool air) {
91 constexpr f32 TERMINAL_Y_VEL = 120.0f;
92
93 if (!m_noGravity) {
95 }
96
99
100 if (m_killExtVelY) {
101 m_extVel.y = std::min(0.0f, m_extVel.y);
102 }
103
104 m_extVel *= 0.998f;
105 m_angVel0 *= 0.98f;
106
108 EGG::Vector3f playerBackHoriz = playerBack;
109 playerBackHoriz.y = 0.0f;
110
111 if (playerBackHoriz.squaredLength() > std::numeric_limits<f32>::epsilon()) {
112 playerBackHoriz.normalise();
113 const auto [proj, rej] = m_extVel.projAndRej(playerBackHoriz);
114 const EGG::Vector3f &speedBack = proj;
115 m_extVel = rej;
116
117 f32 norm = speedBack.squaredLength();
118 norm = norm > std::numeric_limits<f32>::epsilon() ? EGG::Mathf::sqrt(norm) : 0.0f;
119
120 m_speedFix = norm * playerBack.dot(playerBackHoriz);
121 if (speedBack.dot(playerBackHoriz) < 0.0f) {
123 }
124 }
125
126 if (air) {
127 m_intVel.y = std::min(TERMINAL_Y_VEL, m_intVel.y);
128 }
129
131
132 if (m_scale.z > 1.0f) {
133 maxSpeed *= m_scale.z;
134 }
135
136 m_speedNorm = std::min(m_velocity.normalise(), maxSpeed);
138 m_pos += m_velocity;
139
142
143 m_angVel0.x = std::clamp(m_angVel0.x, -0.4f, 0.4f);
144 m_angVel0.y = std::clamp(m_angVel0.y, -0.4f, 0.4f) * m_angVel0YFactor;
145 m_angVel0.z = std::clamp(m_angVel0.z, -0.8f, 0.8f);
146
147 if (m_forceUpright) {
148 forceUpright();
149 }
150
152
153 if (angVelSum.squaredLength() > std::numeric_limits<f32>::epsilon()) {
154 m_mainRot += m_mainRot.multSwap(angVelSum) * 0.5f;
155
156 if (EGG::Mathf::abs(m_mainRot.squaredNorm()) > std::numeric_limits<f32>::epsilon()) {
158 } else {
160 }
161 }
162
163 if (m_forceUpright) {
164 stabilize();
165 }
166
167 if (EGG::Mathf::abs(m_mainRot.squaredNorm()) > std::numeric_limits<f32>::epsilon()) {
169 } else {
171 }
172
175
179}
180
194
203 const EGG::Vector3f &Frot, bool ignoreX) {
204 m_totalForce.y += Flinear.y;
207 EGG::Vector3f torque = rBody.cross(fBody);
208
209 if (ignoreX) {
210 torque.x = 0.0f;
211 }
212 torque.y = 0.0f;
213 m_totalTorque += torque;
214}
215
220 m_totalForce += f;
221
222 EGG::Vector3f invForceRot = m_fullRot.rotateVectorInv(f);
223 EGG::Vector3f relPos = p - m_pos;
224 EGG::Vector3f invPosRot = m_fullRot.rotateVectorInv(relPos);
225
226 m_totalTorque += invPosRot.cross(invForceRot) * scale;
227}
228
235
237
240
245
251 forward.normalise();
252 EGG::Vector3f local_4c = forward.cross(m_top_.cross(forward));
253 local_4c.normalise();
254
256 if (EGG::Mathf::abs(top.dot(local_4c)) >= 0.9999f) {
257 return;
258 }
259
260 EGG::Quatf q;
261 q.makeVectorRotation(top, local_4c);
263}
264
265} // namespace Kinoko::Kart
A 3 x 4 matrix.
Definition Matrix.hh:10
constexpr void makeQ(const Quatf &q)
Sets rotation matrix from quaternion.
Definition Matrix.hh:72
constexpr Matrix34f transpose() const
Transposes the 3x3 portion of the matrix.
Definition Matrix.hh:389
constexpr void inverseTo33(Matrix34f &out) const
Inverts the 3x3 portion of the 3x4 matrix.
Definition Matrix.hh:331
static const Matrix34f zero
Definition Matrix.hh:413
constexpr Vector3f multVector33(const Vector3f &vec) const
Multiplies a 3x3 matrix by a vector.
Definition Matrix.hh:285
constexpr Matrix34f multiplyTo(const Matrix34f &rhs) const
Multiplies two matrices.
Definition Matrix.hh:238
static const Matrix34f ident
Definition Matrix.hh:412
void stabilize() override
Stabilizes the bike by rotating towards the y-axis unit vector.
bool m_noGravity
Disables gravity. Relevant when respawning.
EGG::Quatf m_fullRot
The combination of the other rotations.
bool m_forceUpright
Specifies if we should return the vehicle to upwards orientation.
EGG::Matrix34f calcWorldInertiaTensor() const
EGG::Vector3f m_acceleration
Basically just m_totalForce.
EGG::Vector3f m_movingObjVel
Velocity from things like TF conveyors.
EGG::Vector3f m_totalTorque
Torque from linear motion and rotation.
EGG::Vector3f m_movingRoadVel
Velocity from Koopa Cape water.
EGG::Matrix34f m_invInertiaTensor
The inverse of m_inertiaTensor.
EGG::Vector3f m_top
The unit vector pointing up from the vehicle.
EGG::Vector3f m_angVel0
Angular velocity from m_totalTorque.
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.
EGG::Vector3f m_extVel
Velocity induced by collisions.
EGG::Vector3f m_angVel2
The main component of angular velocity.
EGG::Vector3f m_top_
Basically m_top biased towards absolute up.
EGG::Vector3f m_pos
The vehicle's position.
virtual void stabilize()
Stabilizes the kart by rotating towards the y-axis unit vector.
void calc(f32 maxSpeed, bool air)
Every frame, computes acceleration, velocity, position and rotation of the kart.
void setInertia(const EGG::Vector3f &m, const EGG::Vector3f &n)
EGG::Matrix34f m_inertiaTensor
Resistance to rotational change, as a 3x3 matrix.
EGG::Quatf m_extraRot
[Unused]
EGG::Vector3f m_intVel
What you typically consider to be the vehicle's speed.
EGG::Vector3f m_velocity
Sum of the linear velocities.
EGG::Vector3f m_totalForce
Basically just gravity.
EGG::Quatf m_specialRot
Rotation from trick animations. Copied from KartPhysics.
f32 m_speedNorm
Min of the max speed and m_velocity magnitude.
void applyWrenchScaled(const EGG::Vector3f &p, const EGG::Vector3f &f, f32 scale)
Applies a force linearly and rotationally to the kart.
f32 m_angVel0Factor
Scalar for damping angular velocity.
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.
f32 m_gravity
Usually -1.3f, also affected by KartMove::calcDive.
f32 m_stabilizationFactor
Scalar for damping the main rotation.
EGG::Quatf m_mainRot
Rotation based on the angular velocities.
f32 m_angVel0YFactor
Scalar for damping angular velocity.
bool m_killExtVelY
Caps external velocity at 0.
EGG::Vector3f m_angVel1
[Unused]
Pertains to kart-related functionality.
float f32
Definition Memory.hh:21
A quaternion, used to represent 3D rotation.
Definition Quat.hh:12
static const Quatf ident
Definition Quat.hh:259
constexpr void makeVectorRotation(const Vector3f &from, const Vector3f &to)
Captures rotation between two vectors.
Definition Quat.hh:119
constexpr void normalise()
Scales the quaternion to a unit length.
Definition Quat.hh:107
constexpr Vector3f rotateVector(const Vector3f &vec) const
Rotates a vector based on the quat.
Definition Quat.hh:139
constexpr f32 squaredNorm() const
Computes .
Definition Quat.hh:195
constexpr Quatf multSwap(const Vector3f &vec) const
Definition Quat.hh:220
constexpr Quatf slerpTo(const Quatf &q1, f32 t) const
Performs spherical linear interpolation.
Definition Quat.hh:168
constexpr Vector3f rotateVectorInv(const Vector3f &vec) const
Rotates a vector on the inverse quat.
Definition Quat.hh:153
A 3D float vector.
Definition Vector.hh:108
constexpr f32 squaredLength() const
The dot product between the vector and itself.
Definition Vector.hh:202
static const Vector3f zero
Definition Vector.hh:386
static const Vector3f ey
Definition Vector.hh:388
static const Vector3f ez
Definition Vector.hh:388
constexpr f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
Definition Vector.hh:207
constexpr f32 normalise()
Normalizes the vector and returns the original length.
Definition Vector.hh:279
constexpr void setZero()
Definition Vector.hh:122
constexpr Vector3f cross(const Vector3f &rhs) const
Definition Vector.hh:197
static const Vector3f unit
Definition Vector.hh:387
constexpr std::pair< Vector3f, Vector3f > projAndRej(const Vector3f &rhs) const
Definition Vector.hh:235