1#include "KartHalfPipe.hh"
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"
10#include <egg/math/Math.hh>
15KartHalfPipe::KartHalfPipe() =
default;
18KartHalfPipe::~KartHalfPipe() =
default;
21void KartHalfPipe::reset() {
22 m_stunt = StuntType::None;
23 m_touchingZipper =
false;
28void KartHalfPipe::calc() {
29 constexpr s16 LANDING_BOOST_DELAY = 3;
31 if (state()->airtime() > 15 && state()->isOverZipper()) {
32 m_timer = LANDING_BOOST_DELAY;
35 bool isLanding = state()->isHalfPipeRamp() && m_timer <= 0;
39 if (!state()->isInAction() &&
40 collide()->surfaceFlags().offBit(KartCollide::eSurfaceFlags::StopHalfPipeState)) {
41 if (m_touchingZipper && state()->isAirStart()) {
42 dynamics()->setExtVel(EGG::Vector3f::zero);
43 state()->setOverZipper(
true);
51 m_nextSign = local_64.
dot(EGG::Vector3f::ey) > 0.0f ? 1.0f : -1.0f;
58 m_prevPos = prevPos();
62 f32 scaledDir = std::min(65.0f, move()->dir().y * move()->speed());
64 }
else if (state()->isOverZipper()) {
65 dynamics()->setGravity(-1.3f);
73 sideRot = sideRot.multSwap(mainRot()).multSwap(m_rot);
75 f32 t = move()->calcSlerpRate(DEG2RAD360, mainRot(), sideRot);
77 dynamics()->setFullRot(slerp);
78 dynamics()->setMainRot(slerp);
84 }
else if (state()->isHalfPipeRamp()) {
89 m_timer = std::max(0, m_timer - 1);
90 m_touchingZipper = isLanding;
94void KartHalfPipe::calcTrick() {
95 constexpr s16 TRICK_COOLDOWN = 10;
97 auto &trick = inputs()->currentState().trick;
99 if (trick != System::Trick::None) {
100 m_nextTimer = TRICK_COOLDOWN;
104 if (state()->isOverZipper()) {
105 if (!state()->isZipperTrick() && m_nextTimer > 0 && state()->airtime() > 3 &&
106 state()->airtime() < 10) {
111 m_nextTimer = std::max(0, m_nextTimer - 1);
115void KartHalfPipe::calcRot() {
116 if (m_stunt == StuntType::None) {
120 m_stuntManager.calcAngle();
122 f32 angle = m_rotSign * (DEG2RAD * m_stuntManager.angle);
125 case StuntType::Side360:
126 case StuntType::Side720:
127 m_stuntRot.
setRPY(0.0f, angle, 0.0f);
129 case StuntType::Backside: {
130 auto rpy = EGG::Quatf::FromRPY(0.0f, DEG2RAD * (0.25f * -m_rotSign * m_stuntManager.angle),
135 case StuntType::Frontside: {
136 EGG::Quatf rpy = EGG::Quatf::FromRPY(0.0f, 0.0f,
137 DEG2RAD * (0.2f * -m_rotSign * m_stuntManager.angle));
141 case StuntType::Frontflip:
142 m_stuntRot.
setRPY(m_rotSign * angle, 0.0f, 0.0f);
144 case StuntType::Backflip:
145 m_stuntRot.
setRPY(-m_rotSign * angle, 0.0f, 0.0f);
151 physics()->composeStuntRot(m_stuntRot);
155void KartHalfPipe::calcLanding(
bool) {
156 constexpr f32 LANDING_RADIUS = 150.0f;
157 constexpr f32 PREVIOUS_RADIUS = 200.0f;
158 constexpr f32 MIDAIR_RADIUS = 50.0f;
159 constexpr f32 WALL_RADIUS = 100.0f;
161 constexpr f32 COS_PI_OVER_4 = 0.707f;
172 EGG::Vector3f prevPos = m_prevPos + EGG::Vector3f::ey * PREVIOUS_RADIUS;
174 bool hasDriverFloorCollision = move()->calcZipperCollision(LANDING_RADIUS, bsp().initialYPos,
177 prevPos = hasDriverFloorCollision ? EGG::Vector3f::inf : prevPos;
179 if (state()->isOverZipper()) {
180 if (!move()->calcZipperCollision(MIDAIR_RADIUS, bsp().initialYPos, pos, upLocal, prevPos,
181 &colInfo2, &maskOut, mask)) {
186 if (move()->calcZipperCollision(WALL_RADIUS, bsp().initialYPos, pos, upLocal, prevPos,
187 &colInfo2, &maskOut, mask)) {
189 move()->setUp(up + (colInfo2.wallNrm - up) * 0.2f);
190 move()->setSmoothedUp(move()->up());
192 f32 yScale = bsp().initialYPos * scale().y;
194 pos + colInfo2.tangentOff + -WALL_RADIUS * colInfo2.wallNrm + yScale * upLocal;
195 newPos.y += move()->hopPosY();
197 dynamics()->setPos(newPos);
198 move()->setDir(move()->dir().perpInPlane(move()->up(),
true));
199 move()->setVel1Dir(move()->dir());
201 if (state()->isOverZipper()) {
202 state()->setZipperStick(
true);
207 if (state()->isOverZipper()) {
208 state()->setZipperStick(
false);
212 if (!hasDriverFloorCollision || state()->airtime() <= 5) {
216 if (colInfo.floorNrm.
dot(EGG::Vector3f::ey) <= COS_PI_OVER_4) {
220 if (state()->isOverZipper()) {
221 state()->setZipperStick(
false);
226void KartHalfPipe::activateTrick(s32 duration, System::Trick trick) {
227 if (duration < 51 || trick == System::Trick::None) {
228 m_stunt = StuntType::None;
230 m_rotSign = m_nextSign;
231 bool timerThreshold = duration > 70;
234 case System::Trick::Up:
235 m_stunt = timerThreshold ? StuntType::Backside : StuntType::Backflip;
237 case System::Trick::Down:
238 m_stunt = timerThreshold ? StuntType::Frontside : StuntType::Frontflip;
240 case System::Trick::Left:
241 case System::Trick::Right:
242 m_stunt = timerThreshold ? StuntType::Side720 : StuntType::Side360;
243 m_rotSign = trick == System::Trick::Left ? 1.0f : -1.0f;
249 m_stuntManager.setProperties(
static_cast<size_t>(m_stunt));
251 state()->setZipperTrick(
true);
254 m_stuntRot = EGG::Quatf::ident;
258void KartHalfPipe::end(
bool boost) {
259 if (state()->isOverZipper() && state()->airtime() > 5 && boost) {
260 move()->activateZipperBoost();
263 if (state()->isZipperTrick()) {
264 physics()->composeDecayingStuntRot(m_stuntRot);
267 if (state()->isOverZipper()) {
268 move()->setDir(mainRot().rotateVector(EGG::Vector3f::ez));
269 move()->setVel1Dir(move()->dir());
272 state()->setOverZipper(
false);
273 state()->setZipperTrick(
false);
274 state()->setZipperStick(
false);
276 m_stunt = StuntType::None;
279void KartHalfPipe::StuntManager::calcAngle() {
280 if (finalAngle * properties.finalAngleScalar < angle) {
281 angleDelta = std::max(properties.angleDeltaMin, angleDelta * angleDeltaFactor);
282 angleDeltaFactor = std::max(properties.angleDeltaFactorMin,
283 angleDeltaFactor - properties.angleDeltaFactorDecr);
286 angle = std::min(finalAngle, angle + angleDelta);
289void KartHalfPipe::StuntManager::setProperties(
size_t idx) {
290 static constexpr std::array<StuntProperties, 6> STUNT_PROPERTIES = {{
291 {6.0f, 2.5f, 0.955f, 0.01f, 0.7f, 360.0f},
292 {7.0f, 3.0f, 0.955f, 0.01f, 0.7f, 360.0f},
293 {7.0f, 3.0f, 0.95f, 0.01f, 0.7f, 360.0f},
294 {12.0f, 2.5f, 0.955f, 0.01f, 0.0f, 360.0f},
295 {4.0f, 4.0f, 0.98f, 0.01f, 0.0f, 360.0f},
296 {9.0f, 3.0f, 0.92f, 0.01f, 0.8f, 720.0f},
299 ASSERT(idx < STUNT_PROPERTIES.size());
301 properties = STUNT_PROPERTIES[idx];
302 finalAngle = properties.finalAngle;
303 angleDelta = properties.angleDelta;
304 angleDeltaFactorDecr = properties.angleDeltaFactorDecr;
306 angleDeltaFactor = 1.0f;
#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_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.
Vector3f rotateVector(const Vector3f &vec) const
Rotates a vector based on the quat.
Quatf slerpTo(const Quatf &q2, f32 t) const
Performs spherical linear interpolation.
void setAxisRotation(f32 angle, const Vector3f &axis)
Set the quat given angle and axis.
Vector3f rotateVectorInv(const Vector3f &vec) const
Rotates a vector on the inverse quat.
void setRPY(const Vector3f &rpy)
Sets roll, pitch, and yaw.
void makeVectorRotation(const Vector3f &from, const Vector3f &to)
Captures rotation between two vectors.
f32 normalise()
Normalizes the vector and returns the original length.
f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
Vector3f perpInPlane(const EGG::Vector3f &rhs, bool normalise) const
Calculates the orthogonal vector, based on the plane defined by this vector and rhs.