A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
ObjectCollisionKart.cc
1#include "ObjectCollisionKart.hh"
2
3#include "game/field/ObjectDirector.hh"
4
5#include "game/kart/KartObjectManager.hh"
6
7#include "game/system/RaceConfig.hh"
8
9namespace Kinoko::Field {
10
12ObjectCollisionKart::ObjectCollisionKart() : m_kartObject(nullptr) {}
13
15ObjectCollisionKart::~ObjectCollisionKart() {
16 EGG::egg_delete(m_hull);
17}
18
20void ObjectCollisionKart::init(u32 idx) {
21 if (m_kartObject) {
22 return;
23 }
24
25 m_kartObject = Kart::KartObjectManager::Instance()->object(idx);
26 m_playerIdx = idx;
27
28 auto vehicle = System::RaceConfig::Instance()->raceScenario().players[idx].vehicle;
29 m_hull = EGG::egg_new<ObjectCollisionConvexHull>(GetVehicleVertices(vehicle));
30}
31
33size_t ObjectCollisionKart::checkCollision(const EGG::Matrix34f &mat, const EGG::Vector3f &v) {
34 if (!m_hull) {
35 return 0;
36 }
37
38 // Performance improvement for Kinoko: the hull is only used to check collision against nearby
39 // objects. If there are none, then skip the transform since there are no collisions.
40 if (!BoxColManager::Instance()->hasNextObject()) {
41 return 0;
42 }
43
44 const EGG::Vector3f &scale = m_kartObject->scale();
45 m_hull->transform(mat, scale, v);
46 m_hull->setBoundingRadius(scale.x * m_hull->initRadius());
47
48 return ObjectDirector::Instance()->checkKartObjectCollision(m_kartObject, m_hull);
49}
50
52EGG::Vector3f ObjectCollisionKart::GetHitDirection(u16 objKartHit) {
53 EGG::Vector3f hitDepth = ObjectDirector::Instance()->hitDepth(objKartHit);
54 hitDepth.normalise();
55 return hitDepth;
56}
57
59constexpr std::span<const EGG::Vector3f> ObjectCollisionKart::GetVehicleVertices(Vehicle vehicle) {
60 static constexpr std::array<EGG::Vector3f, 2> VERT_STANDARD_KART_S = {{
61 {0.0f, 35.0f, -40.0f},
62 {0.0f, 35.0f, 25.0f},
63 }};
64
65 static constexpr std::array<EGG::Vector3f, 4> VERT_STANDARD_KART_M = {{
66 {0.0, 60.0, 10.0},
67 {10.0, 35.0, -50.0},
68 {-10.0, 35.0, -50.0},
69 {0.0, 35.0, 65.0},
70 }};
71
72 static constexpr std::array<EGG::Vector3f, 4> VERT_STANDARD_KART_L = {{
73 {0.0, 135.0, 30.0},
74 {25.0, 35.0, -95.0},
75 {-25.0, 35.0, -95.0},
76 {0.0, 35.0, 105.0},
77 }};
78
79 static constexpr std::array<EGG::Vector3f, 4> VERT_BOOSTER_SEAT = {{
80 {0.0, 120.0, 0.0},
81 {0.0, 95.0, -55.0},
82 {0.0, 10.0, -15.0},
83 {0.0, 10.0, 20.0},
84 }};
85
86 static constexpr std::array<EGG::Vector3f, 5> VERT_CLASSIC_DRAGSTER = {{
87 {0.0, 55.0, -25.0},
88 {20.0, -5.0, -85.0},
89 {-20.0, -5.0, -85.0},
90 {20.0, -5.0, 60.0},
91 {-20.0, -5.0, 60.0},
92 }};
93
94 static constexpr std::array<EGG::Vector3f, 5> VERT_OFFROADER = {{
95 {0.0, 85.0, 30.0},
96 {60.0, 5.0, -95.0},
97 {-60.0, 5.0, -95.0},
98 {50.0, 5.0, 130.0},
99 {-50.0, 5.0, 130.0},
100 }};
101
102 static constexpr std::array<EGG::Vector3f, 3> VERT_MINI_BEAST = {{
103 {0.0, 25.0, -10.0},
104 {0.0, 5.0, -60.0},
105 {0.0, 5.0, 40.0},
106 }};
107
108 static constexpr std::array<EGG::Vector3f, 4> VERT_WILD_WING = {{
109 {0.0, 60.0, -10.0},
110 {15.0, 25.0, -80.0},
111 {-15.0, 25.0, -80.0},
112 {0.0, 25.0, 80.0},
113 }};
114
115 static constexpr std::array<EGG::Vector3f, 5> VERT_FLAME_FLYER = {{
116 {0.0, 80.0, 0.0},
117 {30.0, 0.0, -105.0},
118 {-30.0, 0.0, -105.0},
119 {25.0, 0.0, 105.0},
120 {-25.0, 0.0, 105.0},
121 }};
122
123 static constexpr std::array<EGG::Vector3f, 3> VERT_CHEEP_CHARGER = {{
124 {0.0, 40.0, 0.0},
125 {0.0, 25.0, -10.0},
126 {0.0, 25.0, 10.0},
127 }};
128
129 static constexpr std::array<EGG::Vector3f, 4> VERT_SUPER_BLOOPER = {{
130 {0.0, 50.0, 0.0},
131 {0.0, 25.0, 50.0},
132 {-25.0, 25.0, -20.0},
133 {25.0, 25.0, -20.0},
134 }};
135
136 static constexpr std::array<EGG::Vector3f, 5> VERT_PIRANHA_PROWLER = {{
137 {0.0, 95.0, -10.0},
138 {35.0, -25.0, -115.0},
139 {-35.0, -25.0, -115.0},
140 {35.0, -25.0, 85.0},
141 {-35.0, -25.0, 85.0},
142 }};
143
144 static constexpr std::array<EGG::Vector3f, 5> VERT_TINY_TITAN = {{
145 {0.0, 55.0, -10.0},
146 {25.0, -10.0, -40.0},
147 {-25.0, -10.0, -40.0},
148 {-25.0, -10.0, 30.0},
149 {25.0, -10.0, 30.0},
150 }};
151
152 static constexpr std::array<EGG::Vector3f, 5> VERT_DAYTRIPPER = {{
153 {0.0, 60.0, 0.0},
154 {12.0, 5.0, -55.0},
155 {-12.0, 5.0, -55.0},
156 {8.0, 5.0, 35.0},
157 {-8.0, 5.0, 35.0},
158 }};
159
160 static constexpr std::array<EGG::Vector3f, 5> VERT_JETSETTER = {{
161 {0.0, 120.0, 30.0},
162 {25.0, 30.0, -75.0},
163 {-25.0, 30.0, -75.0},
164 {20.0, 30.0, 115.0},
165 {-20.0, 30.0, 115.0},
166 }};
167
168 static constexpr std::array<EGG::Vector3f, 3> VERT_BLUE_FALCON = {{
169 {20.0, 20.0, -30.0},
170 {-20.0, 20.0, -30.0},
171 {0.0, 20.0, 80.0},
172 }};
173
174 static constexpr std::array<EGG::Vector3f, 5> VERT_SPRINTER = {{
175 {0.0, 60.0, 0.0},
176 {25.0, 20.0, -60.0},
177 {-25.0, 20.0, -60.0},
178 {20.0, 20.0, 75.0},
179 {-20.0, 20.0, 75.0},
180 }};
181
182 static constexpr std::array<EGG::Vector3f, 7> VERT_HONEYCOUPE = {{
183 {0.0, 100.0, 50.0},
184 {60.0, 20.0, -110.0},
185 {-60.0, 20.0, -110.0},
186 {60.0, -40.0, -110.0},
187 {-60.0, -40.0, -110.0},
188 {50.0, 0.0, 130.0},
189 {-50.0, 0.0, 130.0},
190 }};
191
192 static constexpr std::array<EGG::Vector3f, 3> VERT_STANDARD_BIKE_S = {{
193 {0.0, 40.0, 0.0},
194 {0.0, 5.0, -15.0},
195 {0.0, 5.0, 20.0},
196 }};
197
198 static constexpr std::array<EGG::Vector3f, 3> VERT_STANDARD_BIKE_M = {{
199 {0.0, 75.0, 10.0},
200 {0.0, 10.0, -20.0},
201 {0.0, 10.0, 20.0},
202 }};
203
204 static constexpr std::array<EGG::Vector3f, 3> VERT_STANDARD_BIKE_L = {{
205 {0.0, 110.0, -15.0},
206 {0.0, -5.0, -35.0},
207 {0.0, -5.0, 40.0},
208 }};
209
210 static constexpr std::array<EGG::Vector3f, 3> VERT_BULLET_BIKE = {{
211 {0.0, 25.0, 10.0},
212 {0.0, -10.0, -45.0},
213 {0.0, -10.0, 50.0},
214 }};
215
216 static constexpr std::array<EGG::Vector3f, 3> VERT_MACH_BIKE = {{
217 {0.0, 50.0, 15.0},
218 {0.0, 5.0, -30.0},
219 {0.0, 5.0, 35.0},
220 }};
221
222 static constexpr std::array<EGG::Vector3f, 3> VERT_FLAME_RUNNER = {{
223 {0.0, 105.0, -10.0},
224 {0.0, -5.0, -50.0},
225 {0.0, -5.0, 45.0},
226 }};
227
228 static constexpr std::array<EGG::Vector3f, 3> VERT_BIT_BIKE = {{
229 {0.0, 40.0, -10.0},
230 {0.0, 15.0, -15.0},
231 {0.0, 15.0, 5.0},
232 }};
233
234 static constexpr std::array<EGG::Vector3f, 3> VERT_SUGARSCOOT = {{
235 {0.0, 70.0, 10.0},
236 {0.0, 10.0, -40.0},
237 {0.0, 10.0, 15.0},
238 }};
239
240 static constexpr std::array<EGG::Vector3f, 4> VERT_WARIO_BIKE = {{
241 {0.0, 110.0, 35.0},
242 {0.0, 70.0, -65.0},
243 {0.0, -10.0, -75.0},
244 {0.0, -5.0, 85.0},
245 }};
246
247 static constexpr std::array<EGG::Vector3f, 3> VERT_QUACKER = {{
248 {0.0, 40.0, 0.0},
249 {0.0, 10.0, 20.0},
250 {0.0, 10.0, -5.0},
251 }};
252
253 static constexpr std::array<EGG::Vector3f, 3> VERT_ZIP_ZIP = {{
254 {0.0, 75.0, 10.0},
255 {0.0, 10.0, -25.0},
256 {0.0, 10.0, 20.0},
257 }};
258
259 static constexpr std::array<EGG::Vector3f, 3> VERT_SHOOTING_STAR = {{
260 {0.0, 120.0, 35.0},
261 {0.0, -15.0, -45.0},
262 {0.0, -15.0, 100.0},
263 }};
264
265 static constexpr std::array<EGG::Vector3f, 3> VERT_MAGIKRUISER = {{
266 {0.0, 35.0, -20.0},
267 {0.0, 0.0, -70.0},
268 {0.0, 0.0, 20.0},
269 }};
270
271 static constexpr std::array<EGG::Vector3f, 3> VERT_SNEAKSTER = {{
272 {0.0, 50.0, 20.0},
273 {0.0, 15.0, -50.0},
274 {0.0, 15.0, 30.0},
275 }};
276
277 static constexpr std::array<EGG::Vector3f, 3> VERT_SPEAR = {{
278 {0.0, 105.0, -20.0},
279 {0.0, -15.0, -60.0},
280 {0.0, -15.0, 75.0},
281 }};
282
283 static constexpr std::array<EGG::Vector3f, 3> VERT_JET_BUBBLE = {{
284 {0.0, 35.0, 10.0},
285 {0.0, -5.0, -45.0},
286 {0.0, -5.0, 50.0},
287 }};
288
289 static constexpr std::array<EGG::Vector3f, 3> VERT_DOLPHIN_DASHER = {{
290 {0.0, 50.0, 20.0},
291 {0.0, -5.0, -35.0},
292 {0.0, -5.0, 42.0},
293 }};
294
295 static constexpr std::array<EGG::Vector3f, 4> VERT_PHANTOM = {{
296 {0.0, 120.0, 35.0},
297 {20.0, -5.0, -85.0},
298 {-20.0, -5.0, -85.0},
299 {0.0, -5.0, 100.0},
300 }};
301
302 static constexpr std::array<EGG::Vector3f, 7> VERT_DEFAULT = {{
303 {0.0, 140.0, 10.0},
304 {-60.0, 70.0, 40.0},
305 {60.0, 70.0, 40.0},
306 {0.0, -40.0, -160.0},
307 {-60.0, -40.0, 50.0},
308 {60.0, -40.0, 50.0},
309 {0.0, -40.0, 160.0},
310 }};
311
312 switch (vehicle) {
313 case Vehicle::Standard_Kart_S:
314 return VERT_STANDARD_KART_S;
315 case Vehicle::Standard_Kart_M:
316 return VERT_STANDARD_KART_M;
317 case Vehicle::Standard_Kart_L:
318 return VERT_STANDARD_KART_L;
319 case Vehicle::Baby_Booster:
320 return VERT_BOOSTER_SEAT;
321 case Vehicle::Classic_Dragster:
322 return VERT_CLASSIC_DRAGSTER;
323 case Vehicle::Offroader:
324 return VERT_OFFROADER;
325 case Vehicle::Mini_Beast:
326 return VERT_MINI_BEAST;
327 case Vehicle::Wild_Wing:
328 return VERT_WILD_WING;
329 case Vehicle::Flame_Flyer:
330 return VERT_FLAME_FLYER;
331 case Vehicle::Cheep_Charger:
332 return VERT_CHEEP_CHARGER;
333 case Vehicle::Super_Blooper:
334 return VERT_SUPER_BLOOPER;
335 case Vehicle::Piranha_Prowler:
336 return VERT_PIRANHA_PROWLER;
337 case Vehicle::Tiny_Titan:
338 return VERT_TINY_TITAN;
339 case Vehicle::Daytripper:
340 return VERT_DAYTRIPPER;
341 case Vehicle::Jetsetter:
342 return VERT_JETSETTER;
343 case Vehicle::Blue_Falcon:
344 return VERT_BLUE_FALCON;
345 case Vehicle::Sprinter:
346 return VERT_SPRINTER;
347 case Vehicle::Honeycoupe:
348 return VERT_HONEYCOUPE;
349 case Vehicle::Standard_Bike_S:
350 return VERT_STANDARD_BIKE_S;
351 case Vehicle::Standard_Bike_M:
352 return VERT_STANDARD_BIKE_M;
353 case Vehicle::Standard_Bike_L:
354 return VERT_STANDARD_BIKE_L;
355 case Vehicle::Bullet_Bike:
356 return VERT_BULLET_BIKE;
357 case Vehicle::Mach_Bike:
358 return VERT_MACH_BIKE;
359 case Vehicle::Flame_Runner:
360 return VERT_FLAME_RUNNER;
361 case Vehicle::Bit_Bike:
362 return VERT_BIT_BIKE;
363 case Vehicle::Sugarscoot:
364 return VERT_SUGARSCOOT;
365 case Vehicle::Wario_Bike:
366 return VERT_WARIO_BIKE;
367 case Vehicle::Quacker:
368 return VERT_QUACKER;
369 case Vehicle::Zip_Zip:
370 return VERT_ZIP_ZIP;
371 case Vehicle::Shooting_Star:
372 return VERT_SHOOTING_STAR;
373 case Vehicle::Magikruiser:
374 return VERT_MAGIKRUISER;
375 case Vehicle::Sneakster:
376 return VERT_SNEAKSTER;
377 case Vehicle::Spear:
378 return VERT_SPEAR;
379 case Vehicle::Jet_Bubble:
380 return VERT_JET_BUBBLE;
381 case Vehicle::Dolphin_Dasher:
382 return VERT_DOLPHIN_DASHER;
383 case Vehicle::Phantom:
384 return VERT_PHANTOM;
385 default:
386 return VERT_DEFAULT;
387 }
388}
389
391const EGG::Vector3f &ObjectCollisionKart::translation(size_t idx) {
392 const auto *objCol = ObjectDirector::Instance()->collidingObject(idx)->collision();
393 return objCol ? objCol->translation() : EGG::Vector3f::zero;
394}
395
396} // namespace Kinoko::Field
static constexpr std::span< const EGG::Vector3f > GetVehicleVertices(Vehicle vehicle)
Helper function to map between a vehicle and its set of convex hull vertices.
Pertains to collision.
A 3D float vector.
Definition Vector.hh:107