A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
CollisionGroup.cc
1#include "CollisionGroup.hh"
2
3namespace Kart {
4
6void CollisionData::reset() {
7 tangentOff.setZero();
8 floorNrm.setZero();
9 wallNrm.setZero();
10 vel.setZero();
11 relPos.setZero();
12 movement.setZero();
13 roadVelocity.setZero();
14 speedFactor = 1.0f;
15 rotFactor = 0.0f;
17 closestFloorSettings = 0xffffffff;
19 closestFloorSettings = 0xffffffff;
20 intensity = 0.0f;
21 colPerpendicularity = 0.0f;
22
23 bFloor = false;
24 bWall = false;
25 bInvisibleWall = false;
26 bTrickable = false;
28 bWall3 = false;
29 bInvisibleWallOnly = false;
31 bSoftWall = false;
34 bHasRoadVel = false;
35 bWallAtLeftCloser = false;
36 bWallAtRightCloser = false;
38}
39
41Hitbox::Hitbox() : m_bspHitbox(nullptr), m_ownsBSP(false) {}
42
44Hitbox::~Hitbox() {
45 if (m_ownsBSP) {
46 delete m_bspHitbox;
47 }
48}
49
52void Hitbox::calc(f32 totalScale, f32 sinkDepth, const EGG::Vector3f &scale, const EGG::Quatf &rot,
53 const EGG::Vector3f &pos) {
54 f32 fVar1 = 0.0f;
55 if (scale.y < totalScale) {
56 fVar1 = (totalScale - scale.y) * m_bspHitbox->radius;
57 }
58
59 EGG::Vector3f scaledPos = m_bspHitbox->position * scale;
60 scaledPos.y = (m_bspHitbox->position.y + sinkDepth) * scale.y + fVar1;
61
62 m_relPos = rot.rotateVector(scaledPos);
63 m_worldPos = m_relPos + pos;
64}
65
67void Hitbox::reset() {
68 m_worldPos.setZero();
69 m_lastPos.setZero();
70 m_relPos.setZero();
71}
72
74void Hitbox::setLastPos(const EGG::Vector3f &scale, const EGG::Matrix34f &pose) {
75 f32 yScaleFactor = scale.y;
76 EGG::Vector3f scaledPos = m_bspHitbox->position;
77 scaledPos.x *= scale.x;
78 scaledPos.z *= scale.z;
79
80 if (scale.y != scale.z && scale.y < 1.0f) {
81 scaledPos.y += (1.0f - scale.y) * m_radius;
82 yScaleFactor = scale.z;
83 }
84
85 scaledPos.y *= yScaleFactor;
86 m_lastPos = pose.ps_multVector(scaledPos);
87}
88
90CollisionGroup::CollisionGroup() : m_hitboxScale(1.0f) {
91 m_collisionData.reset();
92}
93
94CollisionGroup::~CollisionGroup() {
95 delete[] m_hitboxes.data();
96}
97
105f32 CollisionGroup::initHitboxes(const std::array<BSP::Hitbox, 16> &hitboxes) {
106 u16 bspHitboxCount = 0;
107
108 for (const auto &hitbox : hitboxes) {
109 if (parse<u16>(hitbox.enable)) {
110 ++bspHitboxCount;
111 }
112 }
113
114 m_hitboxes = std::span<Hitbox>(new Hitbox[bspHitboxCount], bspHitboxCount);
115 u16 hitboxIdx = 0;
116
117 for (const auto &bspHitbox : hitboxes) {
118 if (parse<u16>(bspHitbox.enable)) {
119 m_hitboxes[hitboxIdx++].setBspHitbox(&bspHitbox);
120 }
121 }
122
123 return computeCollisionLimits();
124}
125
130 EGG::Vector3f max = EGG::Vector3f::zero;
131
132 for (const auto &hitbox : m_hitboxes) {
133 const BSP::Hitbox *bspHitbox = hitbox.bspHitbox();
134
135 if (bspHitbox->enable == 0) {
136 continue;
137 }
138
139 max = max.maximize(bspHitbox->position.abs() + bspHitbox->radius);
140 }
141
142 // Get largest component of the vector
143 f32 maxComponent = max.z;
144
145 if (max.x <= max.y) {
146 if (max.z < max.y) {
147 maxComponent = max.y;
148 }
149 } else if (max.z < max.x) {
150 maxComponent = max.x;
151 }
152
153 m_boundingRadius = maxComponent;
154
155 return max.z * 0.5f;
156}
157
161void CollisionGroup::createSingleHitbox(f32 radius, const EGG::Vector3f &relPos) {
162 m_hitboxes = std::span<Hitbox>(new Hitbox[1], 1);
163
164 // TODO: Do we need for loop if this is just one?
165 // And how exactly will we identify to free the BSP::Hitbox on destruction?
166 for (auto &hitbox : m_hitboxes) {
167 hitbox.reset();
168 BSP::Hitbox *bspHitbox = new BSP::Hitbox;
169 hitbox.setBspHitbox(bspHitbox, true);
170 bspHitbox->position = relPos;
171 bspHitbox->radius = radius;
172 hitbox.setRadius(radius);
173 }
174 m_boundingRadius = radius;
175}
176
178void CollisionGroup::reset() {
179 m_collisionData.reset();
180
181 for (auto &hitbox : m_hitboxes) {
182 hitbox.reset();
183 hitbox.setRadius(hitbox.bspHitbox()->radius * m_hitboxScale);
184 }
185}
186
187void CollisionGroup::resetCollision() {
188 m_collisionData.reset();
189}
190
192void CollisionGroup::setHitboxScale(f32 scale) {
193 m_hitboxScale = scale;
194
195 for (auto &hitbox : m_hitboxes) {
196 hitbox.setRadius(hitbox.bspHitbox()->radius * m_hitboxScale);
197 }
198}
199
200} // namespace Kart
A 3 x 4 matrix.
Definition Matrix.hh:8
Vector3f ps_multVector(const Vector3f &vec) const
Paired-singles impl. of multVector.
Definition Matrix.cc:237
void createSingleHitbox(f32 radius, const EGG::Vector3f &relPos)
Creates a hitbox to represent a tire.
f32 computeCollisionLimits()
Sets the bounding radius.
f32 initHitboxes(const std::array< BSP::Hitbox, 16 > &hitboxes)
Initializes the hitbox array based on the KartParam's BSP hitboxes.
Represents a hitbox for the kart body or a wheel.
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:55
A 3D float vector.
Definition Vector.hh:88
Vector3f abs() const
Returns the absolute value of each element of the vector.
Definition Vector.hh:220
Vector3f maximize(const Vector3f &rhs) const
Returns a vector whose elements are the max of the elements of both vectors.
Definition Vector.cc:83
Represents one of the many hitboxes that make up a vehicle.
Definition KartParam.hh:10
u16 enable
Specifies if this is an active hitbox (since BSP always has 16).
Definition KartParam.hh:11
EGG::Vector3f position
The relative position of the hitbox.
Definition KartParam.hh:12
Field::KCLTypeMask closestWallFlags
The colliding wall KCL flag's KColType.
bool bMovingWaterVertical
KC last turn vertical water.
u32 closestFloorSettings
The colliding floor KCL flag's "variant".
bool bMovingWaterMomentum
Player will maintain speed for a bit after leaving KCL.
bool bMovingWaterDecaySpeed
Player speed will drop if not in mushroom.
bool bFloor
Set if colliding with KCL which satisfies KCL_TYPE_FLOOR.
bool bMovingWaterDisableAccel
KC last turn prevents mini-turbo acceleration.
bool bWall3
Set if colliding with COL_TYPE_WALL_2.
bool bWall
Set if colliding with KCL which satisfies KCL_TYPE_WALL.
Field::KCLTypeMask closestFloorFlags
The colliding floor KCL flag's KColType.
bool bMovingWaterStickyRoad
KC pipe vertical water section.
s32 intensity
The KCL flag's "wheel depth".