A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
KartHalfPipe.cc
1#include "KartHalfPipe.hh"
2
3#include "game/kart/KartCollide.hh"
4#include "game/kart/KartDynamics.hh"
5#include "game/kart/KartMove.hh"
6#include "game/kart/KartParam.hh"
7#include "game/kart/KartPhysics.hh"
8#include "game/kart/KartState.hh"
9
10#include "game/field/CourseColMgr.hh"
11
12#include <egg/math/Math.hh>
13
14namespace Kart {
15
17KartHalfPipe::KartHalfPipe() = default;
18
20KartHalfPipe::~KartHalfPipe() = default;
21
23void KartHalfPipe::reset() {
24 m_stunt = StuntType::None;
25 m_touchingZipper = false;
26 m_timer = 0;
27}
28
30void KartHalfPipe::calc() {
31 constexpr s16 LANDING_BOOST_DELAY = 3;
32
33 if (state()->airtime() > 15 && state()->isOverZipper()) {
34 m_timer = LANDING_BOOST_DELAY;
35 }
36
37 bool isLanding = state()->isHalfPipeRamp() && m_timer <= 0;
38
39 calcTrick();
40
41 if (!state()->isInAction() &&
42 collide()->surfaceFlags().offBit(KartCollide::eSurfaceFlags::StopHalfPipeState) &&
43 m_touchingZipper && state()->isAirStart()) {
44 dynamics()->setExtVel(EGG::Vector3f::zero);
45 state()->setOverZipper(true);
46
47 EGG::Vector3f upXZ = move()->up();
48 upXZ.y = 0.0f;
49 upXZ.normalise();
50 EGG::Vector3f up = move()->dir().perpInPlane(upXZ, true);
51
52 EGG::Vector3f local_64 = up.cross(bodyUp().perpInPlane(up, true));
53 m_nextSign = local_64.dot(EGG::Vector3f::ey) > 0.0f ? 1.0f : -1.0f;
54
55 EGG::Vector3f velNorm = velocity();
56 velNorm.normalise();
57 EGG::Vector3f rot = dynamics()->mainRot().rotateVectorInv(velNorm);
58
59 m_rot.makeVectorRotation(rot, EGG::Vector3f::ez);
60 m_prevPos = prevPos();
61
62 calcLanding(false);
63
64 f32 scaledDir = std::min(65.0f, move()->dir().y * move()->speed());
65 m_attemptedTrickTimer = std::max<s32>(0, scaledDir * 2.0f / 1.3f - 1.0f);
66 } else {
67 if (state()->isOverZipper()) {
68 dynamics()->setGravity(-1.3f);
69
70 EGG::Vector3f side = mainRot().rotateVector(EGG::Vector3f::ez);
71 EGG::Vector3f velNorm = velocity();
72 velNorm.normalise();
73
74 EGG::Quatf sideRot;
75 sideRot.makeVectorRotation(side, velNorm);
76 sideRot = sideRot.multSwap(mainRot()).multSwap(m_rot);
77
78 f32 t = move()->calcSlerpRate(DEG2RAD360, mainRot(), sideRot);
79 EGG::Quatf slerp = mainRot().slerpTo(sideRot, t);
80 dynamics()->setFullRot(slerp);
81 dynamics()->setMainRot(slerp);
82
84
85 calcRot();
86 calcLanding(false);
87 } else {
88 if (state()->isHalfPipeRamp()) {
89 calcLanding(true);
90 }
91 }
92 }
93
94 m_timer = std::max(0, m_timer - 1);
95 m_touchingZipper = isLanding;
96}
97
99void KartHalfPipe::calcTrick() {
100 constexpr s16 TRICK_COOLDOWN = 10;
101
102 auto &trick = inputs()->currentState().trick;
103
104 if (trick != System::Trick::None) {
105 m_nextTimer = TRICK_COOLDOWN;
106 m_trick = trick;
107 }
108
109 if (state()->isOverZipper()) {
110 if (!state()->isZipperTrick() && m_nextTimer > 0 && state()->airtime() > 3 &&
111 state()->airtime() < 10) {
112 activateTrick(m_attemptedTrickTimer, m_trick);
113 }
114 }
115
116 m_nextTimer = std::max(0, m_nextTimer - 1);
117}
118
120void KartHalfPipe::calcRot() {
121 if (m_stunt == StuntType::None) {
122 return;
123 }
124
125 m_stuntManager.calcAngle();
126
127 f32 angle = m_rotSign * (DEG2RAD * m_stuntManager.angle);
128
129 switch (m_stunt) {
130 case StuntType::Side360:
131 case StuntType::Side720:
132 m_stuntRot.setRPY(EGG::Vector3f(0.0f, angle, 0.0f));
133 break;
134 case StuntType::Backside: {
135 EGG::Quatf rpy;
136 rpy.setRPY(
137 EGG::Vector3f(0.0f, DEG2RAD * (0.25f * -m_rotSign * m_stuntManager.angle), 0.0f));
138 EGG::Vector3f rot = rpy.rotateVector(EGG::Vector3f::ez);
139 m_stuntRot.setAxisRotation(angle, rot);
140 } break;
141 case StuntType::Frontside: {
142 EGG::Quatf rpy;
143 rpy.setRPY(EGG::Vector3f(0.0f, 0.0f, DEG2RAD * (0.2f * -m_rotSign * m_stuntManager.angle)));
144 EGG::Vector3f rot = rpy.rotateVector(EGG::Vector3f::ey);
145 m_stuntRot.setAxisRotation(angle, rot);
146 } break;
147 case StuntType::Frontflip:
148 m_stuntRot.setRPY(EGG::Vector3f(m_rotSign * angle, 0.0f, 0.0f));
149 break;
150 case StuntType::Backflip:
151 m_stuntRot.setRPY(EGG::Vector3f(-m_rotSign * angle, 0.0f, 0.0f));
152 break;
153 default:
154 break;
155 }
156
157 physics()->composeStuntRot(m_stuntRot);
158}
159
161void KartHalfPipe::calcLanding(bool) {
162 constexpr f32 LANDING_RADIUS = 150.0f;
163 constexpr f32 PREVIOUS_RADIUS = 200.0f;
164 constexpr f32 MIDAIR_RADIUS = 50.0f;
165 constexpr f32 WALL_RADIUS = 100.0f;
166
167 constexpr f32 COS_PI_OVER_4 = 0.707f;
168
169 Field::CollisionInfo colInfo;
170 Field::CollisionInfo colInfo2;
171 Field::KCLTypeMask maskOut;
172 EGG::Vector3f pos;
173 EGG::Vector3f upLocal;
174
175 Field::KCLTypeMask mask = state()->isOverZipper() ?
178 EGG::Vector3f prevPos = m_prevPos + EGG::Vector3f::ey * PREVIOUS_RADIUS;
179
180 bool hasDriverFloorCollision = move()->calcZipperCollision(LANDING_RADIUS, bsp().initialYPos,
181 pos, upLocal, prevPos, &colInfo, &maskOut, KCL_TYPE_DRIVER_FLOOR);
182
183 prevPos = hasDriverFloorCollision ? EGG::Vector3f::inf : prevPos;
184
185 if (state()->isOverZipper()) {
186 if (!move()->calcZipperCollision(MIDAIR_RADIUS, bsp().initialYPos, pos, upLocal, prevPos,
187 &colInfo2, &maskOut, mask)) {
188 mask |= KCL_TYPE_DRIVER_WALL;
189 }
190 }
191
192 if (move()->calcZipperCollision(WALL_RADIUS, bsp().initialYPos, pos, upLocal, prevPos,
193 &colInfo2, &maskOut, mask)) {
194 EGG::Vector3f up = move()->up();
195 move()->setUp(up + (colInfo2.wallNrm - up) * 0.2f);
196 move()->setSmoothedUp(move()->up());
197
198 f32 yScale = bsp().initialYPos * scale().y;
199 EGG::Vector3f newPos =
200 pos + colInfo2.tangentOff + -WALL_RADIUS * colInfo2.wallNrm + yScale * upLocal;
201 newPos.y += move()->hopPosY();
202
203 dynamics()->setPos(newPos);
204 move()->setDir(move()->dir().perpInPlane(move()->up(), true));
205 move()->setVel1Dir(move()->dir());
206
207 if (state()->isOverZipper()) {
208 state()->setZipperStick(true);
209 }
210
211 m_prevPos = newPos;
212 } else {
213 if (state()->isOverZipper()) {
214 state()->setZipperStick(false);
215 }
216 }
217
218 if (!hasDriverFloorCollision || state()->airtime() <= 5) {
219 return;
220 }
221
222 if (colInfo.floorNrm.dot(EGG::Vector3f::ey) <= COS_PI_OVER_4) {
223 return;
224 }
225
226 if (state()->isOverZipper()) {
227 state()->setZipperStick(false);
228 }
229}
230
232void KartHalfPipe::activateTrick(s32 duration, System::Trick trick) {
233 if (duration < 51 || trick == System::Trick::None) {
234 m_stunt = StuntType::None;
235 } else {
236 m_rotSign = m_nextSign;
237 bool timerThreshold = duration > 70;
238
239 switch (trick) {
240 case System::Trick::Up:
241 m_stunt = timerThreshold ? StuntType::Backside : StuntType::Backflip;
242 break;
243 case System::Trick::Down:
244 m_stunt = timerThreshold ? StuntType::Frontside : StuntType::Frontflip;
245 break;
246 case System::Trick::Left:
247 case System::Trick::Right:
248 m_stunt = timerThreshold ? StuntType::Side720 : StuntType::Side360;
249 m_rotSign = trick == System::Trick::Left ? 1.0f : -1.0f;
250 break;
251 default:
252 break;
253 }
254
255 m_stuntManager.setProperties(static_cast<size_t>(m_stunt));
256
257 state()->setZipperTrick(true);
258 }
259
260 m_stuntRot = EGG::Quatf::ident;
261}
262
264void KartHalfPipe::end(bool boost) {
265 if (state()->isOverZipper() && state()->airtime() > 5 && boost) {
266 move()->activateZipperBoost();
267 }
268
269 if (state()->isZipperTrick()) {
270 physics()->composeDecayingStuntRot(m_stuntRot);
271 }
272
273 if (state()->isOverZipper()) {
274 move()->setDir(mainRot().rotateVector(EGG::Vector3f::ez));
275 move()->setVel1Dir(move()->dir());
276 }
277
278 state()->setOverZipper(false);
279 state()->setZipperTrick(false);
280 state()->setZipperStick(false);
281
282 m_stunt = StuntType::None;
283}
284
285void KartHalfPipe::StuntManager::calcAngle() {
286 if (finalAngle * properties.finalAngleScalar < angle) {
287 angleDelta = std::max(properties.angleDeltaMin, angleDelta * angleDeltaFactor);
288 angleDeltaFactor = std::max(properties.angleDeltaFactorMin,
289 angleDeltaFactor - properties.angleDeltaFactorDecr);
290 }
291
292 angle = std::min(finalAngle, angle + angleDelta);
293}
294
295void KartHalfPipe::StuntManager::setProperties(size_t idx) {
296 static constexpr std::array<StuntProperties, 6> STUNT_PROPERTIES = {{
297 {6.0f, 2.5f, 0.955f, 0.01f, 0.7f, 360.0f},
298 {7.0f, 3.0f, 0.955f, 0.01f, 0.7f, 360.0f},
299 {7.0f, 3.0f, 0.95f, 0.01f, 0.7f, 360.0f},
300 {12.0f, 2.5f, 0.955f, 0.01f, 0.0f, 360.0f},
301 {4.0f, 4.0f, 0.98f, 0.01f, 0.0f, 360.0f},
302 {9.0f, 3.0f, 0.92f, 0.01f, 0.8f, 720.0f},
303 }};
304
305 ASSERT(idx < STUNT_PROPERTIES.size());
306
307 properties = STUNT_PROPERTIES[idx];
308 finalAngle = properties.finalAngle;
309 angleDelta = properties.angleDelta;
310 angleDeltaFactorDecr = properties.angleDeltaFactorDecr;
311 angle = 0.0f;
312 angleDeltaFactor = 1.0f;
313}
314
315} // namespace Kart
#define KCL_TYPE_DRIVER_WALL
0xC010B000
@ COL_TYPE_HALFPIPE_INVISIBLE_WALL
Invisible wall after a half-pipe jump, like in BC.
#define KCL_TYPE_DRIVER_FLOOR
0x20E80DFF - Any KCL that the player can drive on.
#define KCL_TYPE_BIT(x)
#define KCL_TYPE_ANY_INVISIBLE_WALL
0x90002000
s32 m_attemptedTrickTimer
When attempting a trick, tracks how long the animation would be.
EGG::Vector3f bodyUp() const
Returns the second column of the rotation matrix, which is the "up" direction.
Pertains to kart-related functionality.
A quaternion, used to represent 3D rotation.
Definition Quat.hh:12
Vector3f rotateVector(const Vector3f &vec) const
Rotates a vector based on the quat.
Definition Quat.cc:50
Quatf slerpTo(const Quatf &q2, f32 t) const
Performs spherical linear interpolation.
Definition Quat.cc:79
void setAxisRotation(f32 angle, const Vector3f &axis)
Set the quat given angle and axis.
Definition Quat.cc:106
Vector3f rotateVectorInv(const Vector3f &vec) const
Rotates a vector on the inverse quat.
Definition Quat.cc:64
void setRPY(const Vector3f &rpy)
Sets roll, pitch, and yaw.
Definition Quat.cc:7
void makeVectorRotation(const Vector3f &from, const Vector3f &to)
Captures rotation between two vectors.
Definition Quat.cc:35
A 3D float vector.
Definition Vector.hh:83
f32 normalise()
Normalizes the vector and returns the original length.
Definition Vector.cc:44
f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
Definition Vector.hh:182
Vector3f perpInPlane(const EGG::Vector3f &rhs, bool normalise) const
Calculates the orthogonal vector, based on the plane defined by this vector and rhs.
Definition Vector.cc:96