A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
KColData.hh
1#pragma once
2
4
5#include <egg/math/BoundBox.hh>
6#include <egg/math/Matrix.hh>
7
8// Credit: em-eight/mkw
9// Credit: stblr/Hanachan
10
11namespace Field {
12
14 EGG::BoundBox3f bbox;
15 EGG::Vector3f tangentOff;
16
17 void update(const EGG::Vector3f &offset) {
18 bbox.min = bbox.min.minimize(offset);
19 bbox.max = bbox.max.maximize(offset);
20 }
21};
22
24 EGG::BoundBox3f bbox;
25 EGG::Vector3f tangentOff;
26 EGG::Vector3f floorNrm;
27 EGG::Vector3f wallNrm;
28 EGG::Vector3f roadVelocity;
29 f32 floorDist;
30 f32 wallDist;
31 f32 movingFloorDist;
32 f32 perpendicularity;
33
34 void updateFloor(f32 dist, const EGG::Vector3f &fnrm) {
35 if (dist > floorDist) {
36 floorDist = dist;
37 floorNrm = fnrm;
38 }
39 }
40
41 void updateWall(f32 dist, const EGG::Vector3f &fnrm) {
42 if (dist > wallDist) {
43 wallDist = dist;
44 wallNrm = fnrm;
45 }
46 }
47
48 void reset() {
49 bbox.setZero();
50 movingFloorDist = -std::numeric_limits<f32>::min();
51 wallDist = -std::numeric_limits<f32>::min();
52 floorDist = -std::numeric_limits<f32>::min();
53 perpendicularity = 0.0f;
54 }
55
56 void update(f32 now_dist, const EGG::Vector3f &offset, const EGG::Vector3f &fnrm,
57 u32 kclAttributeTypeBit);
58 void transformInfo(CollisionInfo &rhs, const EGG::Matrix34f &mtx, const EGG::Vector3f &v);
59};
60
63class KColData {
64public:
65 enum class CollisionCheckType {
66 Edge,
67 Plane,
68 Movement,
69 };
70
73 KCollisionPrism(f32 height, u16 posIndex, u16 faceNormIndex, u16 edge1NormIndex,
74 u16 edge2NormIndex, u16 edge3NormIndex, u16 attribute);
75
76 f32 height;
77 u16 pos_i;
78 u16 fnrm_i;
79 u16 enrm1_i;
80 u16 enrm2_i;
81 u16 enrm3_i;
82 u16 attribute;
83 };
84 STATIC_ASSERT(sizeof(KCollisionPrism) == 0x10);
85
86 KColData(const void *file);
87 ~KColData();
88
89 void narrowScopeLocal(const EGG::Vector3f &pos, f32 radius, KCLTypeMask mask);
90 void narrowPolygon_EachBlock(const u16 *prismArray);
91
92 void computeBBox();
93 [[nodiscard]] bool checkPointCollision(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut);
94 [[nodiscard]] bool checkSphereCollision(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut);
95 [[nodiscard]] bool checkSphere(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut);
96 [[nodiscard]] bool checkSphereSingle(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut);
97
98 void lookupPoint(const EGG::Vector3f &pos, const EGG::Vector3f &prevPos, KCLTypeMask typeMask);
99 void lookupSphere(f32 radius, const EGG::Vector3f &pos, const EGG::Vector3f &prevPos,
100 KCLTypeMask typeMask);
101 void lookupSphereCached(const EGG::Vector3f &p1, const EGG::Vector3f &p2, u32 typeMask,
102 f32 radius);
103
104 [[nodiscard]] const u16 *searchBlock(const EGG::Vector3f &pos);
105
107 [[nodiscard]] const EGG::BoundBox3f &bbox() const {
108 return m_bbox;
109 }
110
111 [[nodiscard]] u16 prismCache(u32 idx) const {
112 return m_prismCache[idx];
113 }
115
116 [[nodiscard]] static EGG::Vector3f GetVertex(f32 height, const EGG::Vector3f &vertex1,
117 const EGG::Vector3f &fnrm, const EGG::Vector3f &enrm3, const EGG::Vector3f &enrm);
118
119private:
120 void preloadPrisms();
121 void preloadNormals();
122 void preloadVertices();
123
124 template <CollisionCheckType Type>
125 [[nodiscard]] bool checkCollision(const KCollisionPrism &prism, f32 *distOut,
126 EGG::Vector3f *fnrmOut, u16 *flagsOut);
127
128 [[nodiscard]] bool checkPointCollision(const KCollisionPrism &prism, f32 *distOut,
129 EGG::Vector3f *fnrmOut, u16 *flagsOut, bool movement);
130 [[nodiscard]] bool checkSphereMovement(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *attributeOut);
131 [[nodiscard]] bool checkPointMovement(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *attributeOut);
132 [[nodiscard]] bool checkPoint(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *attributeOut);
133
134 const void *m_posData;
135 const void *m_nrmData;
136 const void *m_prismData;
137 const void *m_blockData;
138 f32 m_prismThickness;
139 EGG::Vector3f m_areaMinPos;
147 EGG::Vector3f m_pos;
148 EGG::Vector3f m_prevPos;
149 EGG::Vector3f m_movement;
150 f32 m_radius;
151 KCLTypeMask m_typeMask;
152 const u16 *m_prismIter;
153 EGG::BoundBox3f m_bbox;
154 std::array<u16, 256> m_prismCache;
155 u16 *m_prismCacheTop;
156 u16 *m_cachedPrismArray;
157 EGG::Vector3f m_cachedPos;
158 f32 m_cachedRadius;
159
162 std::span<KCollisionPrism> m_prisms;
163 std::span<EGG::Vector3f> m_nrms;
164 std::span<EGG::Vector3f> m_vertices;
165};
166
167} // namespace Field
A 3 x 4 matrix.
Definition Matrix.hh:8
Performs lookups for KCL triangles.
Definition KColData.hh:63
void preloadNormals()
Creates a copy of the normals in memory.
Definition KColData.cc:331
void preloadVertices()
Creates a copy of the vertices in memory.
Definition KColData.cc:347
bool checkSphereMovement(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *attributeOut)
Iterates the local data block to check for directional collision.
Definition KColData.cc:616
u32 m_areaYWidthMask
The y dimension of the octree's bounding box.
Definition KColData.hh:141
u32 m_areaZWidthMask
The z dimension of the octree's bounding box.
Definition KColData.hh:142
bool checkCollision(const KCollisionPrism &prism, f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut)
This is a combination of the three collision checks in the base game.
Definition KColData.cc:366
f32 m_sphereRadius
Clamps the sphere we check collision against.
Definition KColData.hh:146
static EGG::Vector3f GetVertex(f32 height, const EGG::Vector3f &vertex1, const EGG::Vector3f &fnrm, const EGG::Vector3f &enrm3, const EGG::Vector3f &enrm)
Computes a prism vertex based off of the triangle's normal vectors.
Definition KColData.cc:292
void preloadPrisms()
Creates a copy of the prisms in memory.
Definition KColData.cc:304
bool checkSphere(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut)
Iterates the list of looked-up triangles to see if we are colliding.
Definition KColData.cc:138
void computeBBox()
Calculates a EGG::BoundBox3f that describes the boundary of the track's KCL.
Definition KColData.cc:96
void lookupPoint(const EGG::Vector3f &pos, const EGG::Vector3f &prevPos, KCLTypeMask typeMask)
Sets members in preparation of a subsequent point collision check call.
Definition KColData.cc:189
u32 m_areaXWidthMask
The x dimension of the octree's bounding box.
Definition KColData.hh:140
const u16 * searchBlock(const EGG::Vector3f &pos)
Finds the data block corresponding to the provided position.
Definition KColData.cc:234
u32 m_blockWidthShift
Used to initialize octree navigation.
Definition KColData.hh:143
u32 m_areaXYBlocksShift
Used to initialize octree navigation.
Definition KColData.hh:145
void narrowPolygon_EachBlock(const u16 *prismArray)
Definition KColData.cc:79
u32 m_areaXBlocksShift
Used to initialize octree navigation.
Definition KColData.hh:144
std::span< KCollisionPrism > m_prisms
Optimizes for time by avoiding unnecessary byteswapping. The Wii doesn't have this problem because bi...
Definition KColData.hh:162
void lookupSphere(f32 radius, const EGG::Vector3f &pos, const EGG::Vector3f &prevPos, KCLTypeMask typeMask)
Sets members in preparation of a subsequent sphere collision check call.
Definition KColData.cc:200
Pertains to collision.
A representation of a bounding cuboid.
Definition BoundBox.hh:30
A 3D float vector.
Definition Vector.hh:88
Vector3f maximize(const Vector3f &rhs) const
Returns a vector whose elements are the max of the elements of both vectors.
Definition Vector.cc:73
Vector3f minimize(const Vector3f &rhs) const
Returns a vector whose elements are the min of the elements of both vectors.
Definition Vector.cc:85