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;
37}
38
40Hitbox::Hitbox() : m_bspHitbox(nullptr), m_ownsBSP(false) {}
41
43Hitbox::~Hitbox() {
44 if (m_ownsBSP) {
45 delete m_bspHitbox;
46 }
47}
48
51void Hitbox::calc(f32 totalScale, f32 sinkDepth, const EGG::Vector3f &scale, const EGG::Quatf &rot,
52 const EGG::Vector3f &pos) {
53 f32 fVar1 = 0.0f;
54 if (scale.y < totalScale) {
55 fVar1 = (totalScale - scale.y) * m_bspHitbox->radius;
56 }
57
58 EGG::Vector3f scaledPos = m_bspHitbox->position * scale;
59 scaledPos.y = (m_bspHitbox->position.y + sinkDepth) * scale.y + fVar1;
60
61 m_relPos = rot.rotateVector(scaledPos);
62 m_worldPos = m_relPos + pos;
63}
64
66void Hitbox::reset() {
67 m_worldPos.setZero();
68 m_lastPos.setZero();
69 m_relPos.setZero();
70}
71
73void Hitbox::setLastPos(const EGG::Vector3f &scale, const EGG::Matrix34f &pose) {
74 f32 yScaleFactor = scale.y;
75 EGG::Vector3f scaledPos = m_bspHitbox->position;
76 scaledPos.x *= scale.x;
77 scaledPos.z *= scale.z;
78
79 if (scale.y != scale.z && scale.y < 1.0f) {
80 scaledPos.y += (1.0f - scale.y) * m_radius;
81 yScaleFactor = scale.z;
82 }
83
84 scaledPos.y *= yScaleFactor;
85 m_lastPos = pose.ps_multVector(scaledPos);
86}
87
89CollisionGroup::CollisionGroup() : m_hitboxScale(1.0f) {
90 m_collisionData.reset();
91}
92
93CollisionGroup::~CollisionGroup() {
94 delete[] m_hitboxes.data();
95}
96
104f32 CollisionGroup::initHitboxes(const std::array<BSP::Hitbox, 16> &hitboxes) {
105 u16 bspHitboxCount = 0;
106
107 for (const auto &hitbox : hitboxes) {
108 if (parse<u16>(hitbox.enable)) {
109 ++bspHitboxCount;
110 }
111 }
112
113 m_hitboxes = std::span<Hitbox>(new Hitbox[bspHitboxCount], bspHitboxCount);
114 u16 hitboxIdx = 0;
115
116 for (const auto &bspHitbox : hitboxes) {
117 if (parse<u16>(bspHitbox.enable)) {
118 m_hitboxes[hitboxIdx++].setBspHitbox(&bspHitbox);
119 }
120 }
121
122 return computeCollisionLimits();
123}
124
129 EGG::Vector3f max = EGG::Vector3f::zero;
130
131 for (const auto &hitbox : m_hitboxes) {
132 const BSP::Hitbox *bspHitbox = hitbox.bspHitbox();
133
134 if (bspHitbox->enable == 0) {
135 continue;
136 }
137
138 max = max.maximize(bspHitbox->position.abs() + bspHitbox->radius);
139 }
140
141 // Get largest component of the vector
142 f32 maxComponent = max.z;
143
144 if (max.x <= max.y) {
145 if (max.z < max.y) {
146 maxComponent = max.y;
147 }
148 } else if (max.z < max.x) {
149 maxComponent = max.x;
150 }
151
152 m_boundingRadius = maxComponent;
153
154 return max.z * 0.5f;
155}
156
160void CollisionGroup::createSingleHitbox(f32 radius, const EGG::Vector3f &relPos) {
161 m_hitboxes = std::span<Hitbox>(new Hitbox[1], 1);
162
163 // TODO: Do we need for loop if this is just one?
164 // And how exactly will we identify to free the BSP::Hitbox on destruction?
165 for (auto &hitbox : m_hitboxes) {
166 hitbox.reset();
167 BSP::Hitbox *bspHitbox = new BSP::Hitbox;
168 hitbox.setBspHitbox(bspHitbox, true);
169 bspHitbox->position = relPos;
170 bspHitbox->radius = radius;
171 hitbox.setRadius(radius);
172 }
173 m_boundingRadius = radius;
174}
175
177void CollisionGroup::reset() {
178 m_collisionData.reset();
179
180 for (auto &hitbox : m_hitboxes) {
181 hitbox.reset();
182 hitbox.setRadius(hitbox.bspHitbox()->radius * m_hitboxScale);
183 }
184}
185
186void CollisionGroup::resetCollision() {
187 m_collisionData.reset();
188}
189
191void CollisionGroup::setHitboxScale(f32 scale) {
192 m_hitboxScale = scale;
193
194 for (auto &hitbox : m_hitboxes) {
195 hitbox.setRadius(hitbox.bspHitbox()->radius * m_hitboxScale);
196 }
197}
198
199} // 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.
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".