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() : m_prevPos(
EGG::Vector3f::zero) {}
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 auto &status = KartObjectProxy::status();
33 if (state()->airtime() > 15 && status.onBit(eStatus::OverZipper)) {
34 m_timer = LANDING_BOOST_DELAY;
37 bool isLanding = status.onBit(eStatus::HalfPipeRamp) && m_timer <= 0;
41 if (status.offBit(eStatus::InAction) &&
42 collide()->surfaceFlags().offBit(KartCollide::eSurfaceFlags::StopHalfPipeState)) {
43 if (m_touchingZipper && status.onBit(eStatus::AirStart)) {
44 dynamics()->setExtVel(EGG::Vector3f::zero);
45 status.setBit(eStatus::OverZipper);
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;
57 EGG::Vector3f rot = dynamics()->mainRot().rotateVectorInv(velNorm);
59 m_rot.makeVectorRotation(rot, EGG::Vector3f::ez);
60 m_prevPos = prevPos();
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 if (status.onBit(eStatus::OverZipper)) {
67 dynamics()->setGravity(-1.3f);
69 EGG::Vector3f side = mainRot().rotateVector(EGG::Vector3f::ez);
75 sideRot = sideRot.multSwap(mainRot()).multSwap(m_rot);
77 f32 t = move()->calcSlerpRate(DEG2RAD360, mainRot(), sideRot);
79 dynamics()->setFullRot(slerp);
80 dynamics()->setMainRot(slerp);
82 --m_attemptedTrickTimer;
86 }
else if (status.onBit(eStatus::HalfPipeRamp)) {
89 status.resetBit(eStatus::HalfpipeMidair);
93 m_timer = std::max(0, m_timer - 1);
94 m_touchingZipper = isLanding;
98void KartHalfPipe::calcTrick() {
99 constexpr s16 TRICK_COOLDOWN = 10;
101 auto &trick = inputs()->currentState().trick;
103 if (trick != System::Trick::None) {
104 m_nextTimer = TRICK_COOLDOWN;
108 const auto &status = KartObjectProxy::status();
110 if (status.onBit(eStatus::OverZipper)) {
111 if (status.offBit(eStatus::ZipperTrick) && m_nextTimer > 0 && state()->airtime() > 3 &&
112 state()->airtime() < 10) {
113 activateTrick(m_attemptedTrickTimer, m_trick);
117 m_nextTimer = std::max(0, m_nextTimer - 1);
121void KartHalfPipe::calcRot() {
122 if (m_stunt == StuntType::None) {
126 m_stuntManager.calcAngle();
128 f32 angle = m_rotSign * (DEG2RAD * m_stuntManager.angle);
131 case StuntType::Side360:
132 case StuntType::Side720:
133 m_stuntRot.setRPY(0.0f, angle, 0.0f);
135 case StuntType::Backside: {
136 auto rpy = EGG::Quatf::FromRPY(0.0f, DEG2RAD * (0.25f * -m_rotSign * m_stuntManager.angle),
139 m_stuntRot.setAxisRotation(angle, rot);
141 case StuntType::Frontside: {
142 EGG::Quatf rpy = EGG::Quatf::FromRPY(0.0f, 0.0f,
143 DEG2RAD * (0.2f * -m_rotSign * m_stuntManager.angle));
145 m_stuntRot.setAxisRotation(angle, rot);
147 case StuntType::Frontflip:
148 m_stuntRot.setRPY(m_rotSign * angle, 0.0f, 0.0f);
150 case StuntType::Backflip:
151 m_stuntRot.setRPY(-m_rotSign * angle, 0.0f, 0.0f);
157 physics()->composeStuntRot(m_stuntRot);
161void KartHalfPipe::calcLanding(
bool notAirborne) {
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;
167 constexpr f32 COS_PI_OVER_4 = 0.707f;
171 Field::KCLTypeMask maskOut;
175 auto &status = KartObjectProxy::status();
177 bool overZipper = status.onBit(eStatus::OverZipper);
179 if (notAirborne && velocity().y < 0.0f) {
186 status.resetBit(eStatus::HalfpipeMidair);
188 EGG::Vector3f prevPos = m_prevPos + EGG::Vector3f::ey * PREVIOUS_RADIUS;
190 bool hasDriverFloorCollision = move()->calcZipperCollision(LANDING_RADIUS, bsp().initialYPos,
193 prevPos = hasDriverFloorCollision ? EGG::Vector3f::inf : prevPos;
196 if (!move()->calcZipperCollision(MIDAIR_RADIUS, bsp().initialYPos, pos, upLocal, prevPos,
197 &colInfo2, &maskOut, mask)) {
202 if (move()->calcZipperCollision(WALL_RADIUS, bsp().initialYPos, pos, upLocal, prevPos,
203 &colInfo2, &maskOut, mask)) {
205 status.setBit(eStatus::HalfpipeMidair);
209 move()->setUp(up + (colInfo2.wallNrm - up) * 0.2f);
210 move()->setSmoothedUp(move()->up());
212 f32 yScale = bsp().initialYPos * scale().y;
214 pos + colInfo2.tangentOff + -WALL_RADIUS * colInfo2.wallNrm + yScale * upLocal;
215 newPos.y += move()->hopPosY();
217 dynamics()->setPos(newPos);
218 move()->setDir(move()->dir().perpInPlane(move()->up(),
true));
219 move()->setVel1Dir(move()->dir());
222 status.setBit(eStatus::ZipperStick);
228 status.resetBit(eStatus::ZipperStick);
232 if (!hasDriverFloorCollision || status.onBit(eStatus::HalfpipeMidair) ||
233 state()->airtime() <= 5) {
237 if (colInfo.floorNrm.
dot(EGG::Vector3f::ey) <= COS_PI_OVER_4) {
241 if (status.onBit(eStatus::OverZipper)) {
242 status.resetBit(eStatus::ZipperStick);
247void KartHalfPipe::activateTrick(s32 duration, System::Trick trick) {
248 if (duration < 51 || trick == System::Trick::None) {
249 m_stunt = StuntType::None;
251 m_rotSign = m_nextSign;
252 bool timerThreshold = duration > 70;
255 case System::Trick::Up:
256 m_stunt = timerThreshold ? StuntType::Backside : StuntType::Backflip;
258 case System::Trick::Down:
259 m_stunt = timerThreshold ? StuntType::Frontside : StuntType::Frontflip;
261 case System::Trick::Left:
262 case System::Trick::Right:
263 m_stunt = timerThreshold ? StuntType::Side720 : StuntType::Side360;
264 m_rotSign = trick == System::Trick::Left ? 1.0f : -1.0f;
270 m_stuntManager.setProperties(
static_cast<size_t>(m_stunt));
272 status().setBit(eStatus::ZipperTrick);
275 m_stuntRot = EGG::Quatf::ident;
279void KartHalfPipe::end(
bool boost) {
280 auto &status = KartObjectProxy::status();
281 bool overZipper = status.onBit(eStatus::OverZipper);
283 if (overZipper && state()->airtime() > 5 && boost) {
284 move()->activateZipperBoost();
287 if (status.onBit(eStatus::ZipperTrick)) {
288 physics()->composeDecayingStuntRot(m_stuntRot);
292 move()->setDir(mainRot().rotateVector(EGG::Vector3f::ez));
293 move()->setVel1Dir(move()->dir());
296 status.resetBit(eStatus::OverZipper, eStatus::ZipperTrick, eStatus::ZipperStick,
297 eStatus::HalfpipeMidair);
299 m_stunt = StuntType::None;
302void KartHalfPipe::StuntManager::calcAngle() {
303 if (finalAngle * properties.finalAngleScalar < angle) {
304 angleDelta = std::max(properties.angleDeltaMin, angleDelta * angleDeltaFactor);
305 angleDeltaFactor = std::max(properties.angleDeltaFactorMin,
306 angleDeltaFactor - properties.angleDeltaFactorDecr);
309 angle = std::min(finalAngle, angle + angleDelta);
312void KartHalfPipe::StuntManager::setProperties(
size_t idx) {
313 static constexpr std::array<StuntProperties, 6> STUNT_PROPERTIES = {{
314 {6.0f, 2.5f, 0.955f, 0.01f, 0.7f, 360.0f},
315 {7.0f, 3.0f, 0.955f, 0.01f, 0.7f, 360.0f},
316 {7.0f, 3.0f, 0.95f, 0.01f, 0.7f, 360.0f},
317 {12.0f, 2.5f, 0.955f, 0.01f, 0.0f, 360.0f},
318 {4.0f, 4.0f, 0.98f, 0.01f, 0.0f, 360.0f},
319 {9.0f, 3.0f, 0.92f, 0.01f, 0.8f, 720.0f},
322 ASSERT(idx < STUNT_PROPERTIES.size());
324 properties = STUNT_PROPERTIES[idx];
325 finalAngle = properties.finalAngle;
326 angleDelta = properties.angleDelta;
327 angleDeltaFactorDecr = properties.angleDeltaFactorDecr;
329 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
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 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.