A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
KartSuspensionPhysics.cc
Go to the documentation of this file.
2
9
10#include <egg/math/Math.hh>
11
12namespace Kinoko::Kart {
13
15WheelPhysics::WheelPhysics(u16 wheelIdx, u16 bspWheelIdx)
16 : m_wheelIdx(wheelIdx), m_bspWheelIdx(bspWheelIdx), m_bspWheel(nullptr) {}
17
22
28
33
53
55void WheelPhysics::realign(const EGG::Vector3f &bottom, const EGG::Vector3f &vehicleMovement) {
56 const EGG::Vector3f topmostPos = m_topmostPos + vehicleMovement;
57 f32 scaledMaxTravel = m_bspWheel->maxTravel * sub()->someScale();
58 f32 suspTravel = bottom.dot(m_pos - topmostPos);
59 m_suspTravel = std::clamp(suspTravel, 0.0f, scaledMaxTravel);
60 m_pos = topmostPos + m_suspTravel * bottom;
62 m_speed -= dynamics()->intVel();
66 m_speed -= collide()->movement();
70}
71
76
78 f32 nextRadius = m_bspWheel->sphereRadius;
79 f32 scalar = m_effectiveRadius * scale().y - nextRadius * move()->totalScale();
80
81 EGG::Vector3f center = m_pos + scalar * bottom;
82 scalar = 0.3f * (nextRadius * move()->leanRot()) * move()->totalScale();
83 center += scalar * bodyForward();
84
87 } else {
88 m_hitboxGroup->setHitboxScale(move()->totalScale());
91 }
92
95
96 if (colData.bFloor || colData.bWall || colData.bWall3) {
97 m_pos += colData.tangentOff;
98 if (colData.intensity > -1) {
99 f32 sinkDepth = 3.0f * static_cast<f32>(colData.intensity);
101 body()->trySetTargetSinkDepth(sinkDepth);
102 }
103 }
104 }
105 m_hitboxGroup->hitbox(0).setLastPos(center);
106 }
107
111 m_suspTravel = bottom.dot(m_pos - topmostPos);
112
113 if (m_suspTravel < 0.0f) {
114 m_74 = 1.0f;
115 EGG::Vector3f suspBottom = m_suspTravel * bottom;
116 sub()->updateSuspOvertravel(suspBottom);
117 } else {
118 m_74 = 0.0f;
119 }
120}
121
125
126 collide()->applySomeFloorMoment(0.1f, rate, m_hitboxGroup, forward, move()->dir(), m_speed,
127 true, true, status().offBit(eStatus::LargeFlipHit, eStatus::WheelieRot));
128}
129
132 : m_tirePhysics(nullptr),
133 m_tireType(tireType),
134 m_bspWheelIdx(bspWheelIdx),
135 m_wheelIdx(wheelIdx) {}
136
139
144
145 // BSP::Wheel::xRot is constant. For Kinoko, we can compute the local bottom direction once
146 // instead of rebuilding the rotation matrix every frame in calcCollision().
147 EGG::Matrix34f xRotMat;
148 xRotMat.makeR(EGG::Vector3f(m_bspWheel->xRot * DEG2RAD, 0.0f, 0.0f));
150}
151
158
163 relPos.x = -relPos.x;
164 }
165
166 const EGG::Vector3f rotatedRelPos = dynamics()->fullRot().rotateVector(relPos) + pos();
168
169 m_tirePhysics->setPos(rotatedRelPos + m_bspWheel->maxTravel * unitRotated);
170 m_tirePhysics->setLastPos(rotatedRelPos + m_bspWheel->maxTravel * unitRotated);
171 m_tirePhysics->setLastPosDiff(m_tirePhysics->pos() - rotatedRelPos);
173 (m_tirePhysics->effectiveRadius() * move()->totalScale() * unitRotated));
176 m_topmostPos = rotatedRelPos;
177}
178
182
183 EGG::Vector3f scaledRelPos = m_bspWheel->relPosition * scale();
185 scaledRelPos.x = -scaledRelPos.x;
186 }
187
188 const EGG::Vector3f topmostPos = mat.ps_multVector(scaledRelPos);
190
191 f32 y_down = m_tirePhysics->suspTravel() + 5.0f * sub()->someScale();
192 m_tirePhysics->setSuspTravel(std::clamp(y_down, 0.0f, m_maxTravelScaled));
193 m_tirePhysics->setColVel(10.0f * gravity);
195
196 if (status().offBit(eStatus::SkipWheelCalc)) {
198 m_topmostPos = topmostPos;
199 }
200}
201
206 const EGG::Vector3f &vehicleMovement) {
207 EGG::Vector3f lastPosDiff = m_tirePhysics->lastPosDiff();
208
209 m_tirePhysics->realign(m_bottomDir, vehicleMovement);
210
212 if (!collisionData.bFloor) {
213 return;
214 }
215
217 f32 yDown = std::max(0.0f, m_bottomDir.dot(topDiff));
218 EGG::Vector3f speed = lastPosDiff - topDiff;
219 f32 travel = m_maxTravelScaled - yDown;
220 f32 speedScalar = m_bottomDir.dot(speed);
221
222 f32 springDamp =
223 -(m_bspWheel->springStiffness * travel + m_bspWheel->dampingFactor * speedScalar);
224
225 EGG::Vector3f fRot = m_bottomDir * springDamp;
226
227 if (isInRespawn()) {
228 fRot.y = std::clamp(fRot.y, -1.0f, 1.0f);
229 }
230
231 EGG::Vector3f fLinear = fRot;
232 EGG::Vector3f rotProj = fRot;
233 rotProj.y = 0.0f;
234
235 rotProj = rotProj.proj(collisionData.floorNrm);
236 fLinear.y += rotProj.y;
237 fLinear.y = std::min(fLinear.y, param()->stats().maxNormalAcceleration);
238
240
242 fLinear.y = 0.0f;
243 }
244
247
249}
250
251} // namespace Kinoko::Kart
A 3 x 4 matrix.
Definition Matrix.hh:10
constexpr Vector3f ps_multVector(const Vector3f &vec) const
Paired-singles impl. of multVector.
Definition Matrix.hh:273
constexpr void makeR(const Vector3f &r)
Sets 3x3 rotation matrix from a vector of Euler angles.
Definition Matrix.hh:129
constexpr Vector3f multVector33(const Vector3f &vec) const
Multiplies a 3x3 matrix by a vector.
Definition Matrix.hh:285
constexpr bool offBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are off.
Definition BitFlag.hh:424
constexpr bool onBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
Definition BitFlag.hh:391
void createSingleHitbox(f32 radius, const EGG::Vector3f &relPos)
Creates a hitbox to represent a tire.
Hitbox & hitbox(u16 hitboxIdx)
void setWorldPos(const EGG::Vector3f &pos)
void setLastPos(const EGG::Vector3f &pos)
void trySetTargetSinkDepth(f32 val)
Definition KartBody.cc:42
void calcWheelCollision(u16 wheelIdx, CollisionGroup *hitboxGroup, const EGG::Vector3f &colVel, const EGG::Vector3f &center, f32 radius)
Checks wheel hitbox collision and stores position/velocity info.
const EGG::Vector3f & movement() const
void applySomeFloorMoment(f32 down, f32 rate, CollisionGroup *hitboxGroup, const EGG::Vector3f &forward, const EGG::Vector3f &nextDir, const EGG::Vector3f &speed, bool b1, bool b2, bool b3)
Applies external and angular velocity based on the collision with the floor.
const EGG::Vector3f & movingRoadVel() const
const EGG::Vector3f & movingObjVel() const
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.
const EGG::Quatf & fullRot() const
const EGG::Vector3f & intVel() const
virtual f32 leanRot() const
Definition KartMove.hh:50
f32 totalScale() const
Definition KartMove.hh:285
const EGG::Vector3f & extVel() const
EGG::Vector3f bodyForward() const
Returns the first column of the rotation matrix, which is the "right" direction.
const EGG::Vector3f & pos() const
const EGG::Vector3f & scale() const
KartTire * tire(u16 tireIdx)
void updateSuspOvertravel(const EGG::Vector3f &suspOvertravel)
Definition KartSub.cc:387
EGG::Vector3f m_localBottomDir
m_bottomDir before applying the wheel matrix
KartSuspensionPhysics(u16 wheelIdx, TireType TireType, u16 bspWheelIdx)
TireType
Every other kart tire is a mirror of the first. Bikes do not leverage this.
void calcSuspension(const EGG::Vector3f &forward, const EGG::Vector3f &vehicleMovement)
Calculates linear force and rotation from the kart's suspension.
void calcCollision(const EGG::Vector3f &gravity, const EGG::Matrix34f &mat)
WheelPhysics * wheelPhysics()
Definition KartTire.hh:20
void calcSuspension(const EGG::Vector3f &forward)
void setLastPosDiff(const EGG::Vector3f &pos)
WheelPhysics(u16 wheelIdx, u16 bspWheelIdx)
const EGG::Vector3f & pos() const
void setLastPos(const EGG::Vector3f &pos)
void setPos(const EGG::Vector3f &pos)
void updateCollision(const EGG::Vector3f &bottom, const EGG::Vector3f &topmostPos)
const EGG::Vector3f & topmostPos() const
void setWheelEdgePos(const EGG::Vector3f &pos)
void realign(const EGG::Vector3f &bottom, const EGG::Vector3f &vehicleMovement)
void setColVel(const EGG::Vector3f &vec)
const EGG::Vector3f & lastPosDiff() const
void egg_delete(const T *ptr)
Definition Heap.hh:154
T * egg_new(Args &&...args)
Definition Heap.hh:148
Pertains to kart-related functionality.
@ SoftWallPush
Tracks whether we are being pushed by a soft wall or HWG.
Definition Status.hh:84
@ SoftWallSuspension
Changes wheel physics after touching a soft wall.
Definition Status.hh:83
@ LargeFlipHit
Set when hitting an exploding object.
Definition Status.hh:36
float f32
Definition Memory.hh:21
uint16_t u16
Definition Memory.hh:17
constexpr Vector3f rotateVector(const Vector3f &vec) const
Rotates a vector based on the quat.
Definition Quat.hh:139
A 3D float vector.
Definition Vector.hh:108
static const Vector3f zero
Definition Vector.hh:386
static const Vector3f ey
Definition Vector.hh:388
constexpr f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
Definition Vector.hh:207
constexpr Vector3f proj(const Vector3f &rhs) const
The projection of this vector onto rhs.
Definition Vector.hh:224
constexpr void setZero()
Definition Vector.hh:122
EGG::Vector3f relPosition
Definition KartParam.hh:26
std::array< Wheel, 4 > wheels
Definition KartParam.hh:45
Information about the current collision and its properties.
bool bWall3
Set if colliding with COL_TYPE_WALL_2.
bool bWall
Set if colliding with KCL which satisfies KCL_TYPE_WALL.
s32 intensity
The KCL flag's "wheel depth".
bool bFloor
Set if colliding with KCL which satisfies KCL_TYPE_FLOOR.