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;
18 intensity = 0.0f;
19 colPerpendicularity = 0.0f;
20
21 bFloor = false;
22 bWall = false;
23 bInvisibleWall = false;
24 bWall3 = false;
25 bInvisibleWallOnly = false;
26 bSoftWall = false;
27 bTrickable = false;
28 bHasRoadVel = false;
29 bWallAtLeftCloser = false;
30 bWallAtRightCloser = false;
31}
32
34Hitbox::Hitbox() : m_bspHitbox(nullptr), m_ownsBSP(false) {}
35
37Hitbox::~Hitbox() {
38 if (m_ownsBSP) {
39 delete m_bspHitbox;
40 }
41}
42
45void Hitbox::calc(f32 totalScale, f32 sinkDepth, const EGG::Vector3f &scale, const EGG::Quatf &rot,
46 const EGG::Vector3f &pos) {
47 f32 fVar1 = 0.0f;
48 if (scale.y < totalScale) {
49 fVar1 = (totalScale - scale.y) * m_bspHitbox->radius;
50 }
51
52 EGG::Vector3f scaledPos = m_bspHitbox->position * scale;
53 scaledPos.y = (m_bspHitbox->position.y + sinkDepth) * scale.y + fVar1;
54
55 m_relPos = rot.rotateVector(scaledPos);
56 m_worldPos = m_relPos + pos;
57}
58
60void Hitbox::reset() {
61 m_worldPos.setZero();
62 m_lastPos.setZero();
63 m_relPos.setZero();
64}
65
67void Hitbox::setLastPos(const EGG::Vector3f &scale, const EGG::Matrix34f &pose) {
68 f32 yScaleFactor = scale.y;
69 EGG::Vector3f scaledPos = m_bspHitbox->position;
70 scaledPos.x *= scale.x;
71 scaledPos.z *= scale.z;
72
73 if (scale.y != scale.z && scale.y < 1.0f) {
74 scaledPos.y += (1.0f - scale.y) * m_radius;
75 yScaleFactor = scale.z;
76 }
77
78 scaledPos.y *= yScaleFactor;
79 m_lastPos = pose.ps_multVector(scaledPos);
80}
81
83CollisionGroup::CollisionGroup() : m_hitboxScale(1.0f) {
84 m_collisionData.reset();
85}
86
87CollisionGroup::~CollisionGroup() {
88 delete[] m_hitboxes.data();
89}
90
98f32 CollisionGroup::initHitboxes(const std::array<BSP::Hitbox, 16> &hitboxes) {
99 u16 bspHitboxCount = 0;
100
101 for (const auto &hitbox : hitboxes) {
102 if (parse<u16>(hitbox.enable)) {
103 ++bspHitboxCount;
104 }
105 }
106
107 m_hitboxes = std::span<Hitbox>(new Hitbox[bspHitboxCount], bspHitboxCount);
108 u16 hitboxIdx = 0;
109
110 for (const auto &bspHitbox : hitboxes) {
111 if (parse<u16>(bspHitbox.enable)) {
112 m_hitboxes[hitboxIdx++].setBspHitbox(&bspHitbox);
113 }
114 }
115
116 return computeCollisionLimits();
117}
118
123 EGG::Vector3f max = EGG::Vector3f::zero;
124
125 for (const auto &hitbox : m_hitboxes) {
126 const BSP::Hitbox *bspHitbox = hitbox.bspHitbox();
127
128 if (bspHitbox->enable == 0) {
129 continue;
130 }
131
132 max = max.maximize(bspHitbox->position.abs() + bspHitbox->radius);
133 }
134
135 // Get largest component of the vector
136 f32 maxComponent = max.z;
137
138 if (max.x <= max.y) {
139 if (max.z < max.y) {
140 maxComponent = max.y;
141 }
142 } else if (max.z < max.x) {
143 maxComponent = max.x;
144 }
145
146 m_boundingRadius = maxComponent;
147
148 return max.z * 0.5f;
149}
150
154void CollisionGroup::createSingleHitbox(f32 radius, const EGG::Vector3f &relPos) {
155 m_hitboxes = std::span<Hitbox>(new Hitbox[1], 1);
156
157 // TODO: Do we need for loop if this is just one?
158 // And how exactly will we identify to free the BSP::Hitbox on destruction?
159 for (auto &hitbox : m_hitboxes) {
160 hitbox.reset();
161 BSP::Hitbox *bspHitbox = new BSP::Hitbox;
162 hitbox.setBspHitbox(bspHitbox, true);
163 bspHitbox->position = relPos;
164 bspHitbox->radius = radius;
165 hitbox.setRadius(radius);
166 }
167 m_boundingRadius = radius;
168}
169
171void CollisionGroup::reset() {
172 m_collisionData.reset();
173
174 for (auto &hitbox : m_hitboxes) {
175 hitbox.reset();
176 hitbox.setRadius(hitbox.bspHitbox()->radius * m_hitboxScale);
177 }
178}
179
180void CollisionGroup::resetCollision() {
181 m_collisionData.reset();
182}
183
185void CollisionGroup::setHitboxScale(f32 scale) {
186 m_hitboxScale = scale;
187
188 for (auto &hitbox : m_hitboxes) {
189 hitbox.setRadius(hitbox.bspHitbox()->radius * m_hitboxScale);
190 }
191}
192
193} // 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:232
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:50
A 3D float vector.
Definition Vector.hh:83
Vector3f abs() const
Returns the absolute value of each element of the vector.
Definition Vector.hh:209
Vector3f maximize(const Vector3f &rhs) const
Returns a vector whose elements are the max of the elements of both vectors.
Definition Vector.cc:65
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
u32 closestFloorSettings
The KCL flag's "variant".
bool bFloor
Set if colliding with KCL which satisfies KCL_TYPE_FLOOR.
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 KCL flag's KColType.
s32 intensity
The KCL flag's "wheel depth".