A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
KartReject.cc
Go to the documentation of this file.
1#include "KartReject.hh"
2
7
10
11#include <egg/math/Math.hh>
12#include <egg/math/Quat.hh>
13
14namespace Kinoko::Kart {
15
17KartReject::KartReject() = default;
18
20KartReject::~KartReject() = default;
21
24 m_rejectSign = 0.0f;
25}
26
30
32 return;
33 }
34
37 down = down.perpInPlane(move()->up(), true);
38 f32 cos = down.dot(move()->lastDir());
39 f32 sin = down.cross(move()->lastDir()).length();
40 f32 angle = EGG::Mathf::atan2(sin, cos);
41 angle = angle > 0.0f ? angle : -angle;
42
43 f32 minAngle = 60.0f;
44 angle *= RAD2DEG;
45 f32 dVar11 = 1.0f;
46
47 if (move()->up().dot(EGG::Vector3f::ey) < -0.7f) {
48 minAngle *= 0.5f;
49 dVar11 *= 2.0f;
50 }
51
52 if (angle > minAngle) {
53 angle = 0.05f * (angle - 60.0f);
55 (1.0f + angle * move()->speedRatio()) * dVar11 * DEG2RAD * m_rejectSign, 0.0f);
56 EGG::Quatf local_78 = mainRot().multSwap(rot);
57 local_78.normalise();
58
60 dynamics()->setFullRot(local_78);
61 dynamics()->setMainRot(local_78);
62 }
63
65
66 bool didReject = calcRejection();
67
68 if (status.offBit(eStatus::NoSparkInvisibleWall) && !didReject) {
70 }
71
72 return;
73 }
74
78 EGG::Vector3f upXZ = move()->up();
79 upXZ.y = 0.0f;
80
81 if (upXZ.length() > 0.0f && speed() > 0.0f) {
82 upXZ.normalise();
83 EGG::Vector3f local_88 = move()->lastDir().perpInPlane(upXZ, true);
84
85 if (local_88.y > 0.0f) {
86 EGG::Vector3f upCross = EGG::Vector3f::ey.cross(local_88);
87 m_rejectSign = upCross.dot(move()->up()) > 0.0f ? 1.0f : -1.0f;
88
90 }
91 }
92 }
93}
94
101 EGG::Vector3f worldUpPos = dynamics()->pos() + bodyUp() * 100.0f;
102 f32 posScalar = 100.0f;
103 f32 radius = posScalar;
104
105 for (size_t i = 0; i < 2; ++i) {
107 EGG::Vector3f worldPos = pos() + (-posScalar * move()->scale().y) * local_d0;
108
109 auto *colDir = Field::CollisionDirector::Instance();
110 if (!colDir->checkSphereFullPush(radius, worldPos, worldUpPos, KCL_TYPE_B0E82DFF, &colInfo,
111 &mask, 0)) {
112 if (i == 0) {
113 posScalar = 0.0f;
114 }
115
116 continue;
117 }
118
120
121 if (!calcCollision(colInfo, mask, tangentOff)) {
122 continue;
123 }
124
125 EGG::Vector3f tangentUp = (tangentOff - move()->up()) * 1.0f;
126 move()->setUp(move()->up() + tangentUp);
127 move()->setSmoothedUp(move()->up());
128
129 bool bVar15 = tangentOff.dot(EGG::Vector3f::ey) < -0.17f;
130 if (bVar15 || extVel().y < 0.0f || status.onBit(eStatus::NoSparkInvisibleWall)) {
131 radius = -radius;
132 colInfo.tangentOff += worldPos;
133
134 f32 yOffset = bsp().initialYPos * scale().y;
135 f32 speedScalar = bVar15 ?
136 1.0f :
137 static_cast<f32>(static_cast<f64>(EGG::Mathf::abs(speed()) * 0.01f) - 0.3);
138 speedScalar = std::clamp(speedScalar, 0.0f, 1.0f);
139
140 EGG::Vector3f posOffset =
141 colInfo.tangentOff + radius * tangentOff + yOffset * tangentOff;
142 posOffset.y += move()->hopPosY();
143 posOffset -= pos();
144 setPos(pos() + posOffset * speedScalar);
145 }
146
147 EGG::Vector3f local_13c = move()->lastDir().perpInPlane(move()->smoothedUp(), true);
148 move()->setDir(local_13c);
149 move()->setVel1Dir(local_13c);
150
151 return true;
152 }
153
154 return false;
155}
156
158 EGG::Vector3f &tangentOff) {
159 auto *colDir = Field::CollisionDirector::Instance();
160
161 if (mask & KCL_TYPE_INVISIBLE_WALL) {
162 const auto *entry = colDir->findClosestCollisionEntry(&mask, KCL_TYPE_INVISIBLE_WALL);
163 if (entry && entry->variant() == 0) {
164 tangentOff = colInfo.wallNrm;
166 return true;
167 }
168 }
169
171 if (mask & halfPipeInvisMask) {
172 if (colDir->findClosestCollisionEntry(&mask, halfPipeInvisMask)) {
173 tangentOff = colInfo.wallNrm;
175 return true;
176 }
177 }
178
179 if (mask & KCL_TYPE_DRIVER_FLOOR) {
180 const auto *entry = colDir->findClosestCollisionEntry(&mask, KCL_TYPE_DRIVER_FLOOR);
181 if (entry &&
183 tangentOff = colInfo.floorNrm;
184 return true;
185 }
186 }
187
188 return false;
189}
190
191} // namespace Kinoko::Kart
@ 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_NONE
#define KCL_TYPE_INVISIBLE_WALL
0x80002000
#define KCL_TYPE_B0E82DFF
0xB0E82DFF
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
constexpr TBitFlagExt< N, E > & resetBit(Es... es)
Resets the corresponding bits for the provided enum values.
Definition BitFlag.hh:367
constexpr TBitFlagExt< N, E > & setBit(Es... es)
Sets the corresponding bits for the provided enum values.
Definition BitFlag.hh:356
static CollisionDirector * Instance()
void setAngVel0(const EGG::Vector3f &v)
const EGG::Vector3f & pos() const
void setFullRot(const EGG::Quatf &q)
void setMainRot(const EGG::Quatf &q)
const EGG::Quatf & mainRot() const
const EGG::Vector3f & lastDir() const
Definition KartMove.hh:297
const EGG::Vector3f & scale() const
Definition KartMove.hh:269
void setUp(const EGG::Vector3f &v)
Definition KartMove.hh:192
void setSmoothedUp(const EGG::Vector3f &v)
Definition KartMove.hh:188
void setDir(const EGG::Vector3f &v)
Definition KartMove.hh:196
void setVel1Dir(const EGG::Vector3f &v)
Definition KartMove.hh:200
const EGG::Vector3f & up() const
Definition KartMove.hh:281
const EGG::Vector3f & extVel() const
const EGG::Vector3f & pos() const
EGG::Vector3f bodyUp() const
Returns the second column of the rotation matrix, which is the "up" direction.
const EGG::Vector3f & scale() const
void setPos(const EGG::Vector3f &pos)
const EGG::Quatf & mainRot() const
bool calcCollision(Field::CollisionInfo &colInfo, Field::KCLTypeMask mask, EGG::Vector3f &tangentOff)
Pertains to kart-related functionality.
@ RejectRoad
Collision which causes a change in the player's pos and rot.
Definition Status.hh:64
@ HalfPipeRamp
Set while colliding with zipper KCL.
Definition Status.hh:51
@ Hop
Set while we are in a drift hop. Clears when we land.
Definition Status.hh:31
@ RejectRoadTrigger
e.g. DK Summit ending, and Maple Treeway side walls.
Definition Status.hh:65
@ OverZipper
Set while mid-air from a zipper.
Definition Status.hh:52
@ ZipperInvisibleWall
Set when colliding with invisible wall above a zipper.
Definition Status.hh:54
float f32
Definition Memory.hh:21
double f64
Definition Memory.hh:22
A quaternion, used to represent 3D rotation.
Definition Quat.hh:12
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
static constexpr Quatf FromRPY(const EGG::Vector3f &rpy)
Definition Quat.hh:243
constexpr Quatf multSwap(const Vector3f &vec) const
Definition Quat.hh:220
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 f32 length() const
The square root of the vector's dot product.
Definition Vector.hh:212
constexpr f32 normalise()
Normalizes the vector and returns the original length.
Definition Vector.hh:279
constexpr Vector3f cross(const Vector3f &rhs) const
Definition Vector.hh:197
constexpr 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.hh:347
Stores information pertaining to a collision.
EGG::Vector3f wallNrm
Colliding wall face's normal vector.
EGG::Vector3f tangentOff
The net "push out" vector.
EGG::Vector3f floorNrm
Colliding floor's up vector.