15 auto addOffset = [](
const void *file,
u32 offset) ->
const void * {
16 return reinterpret_cast<const void *
>(
reinterpret_cast<const u8 *
>(file) + offset);
249 offset = parse<u32>(*
reinterpret_cast<const u32 *
>(curBlock + index));
252 if ((offset & 0x80000000) != 0) {
261 u32 x_shift = ((1 * ((
u32)x >> shift)) & 1);
262 u32 y_shift = ((2 * ((
u32)y >> shift)) & 2);
263 u32 z_shift = ((4 * ((
u32)z >> shift)) & 4);
265 index = 4 * (x_shift | y_shift | z_shift);
269 return reinterpret_cast<const u16 *
>(curBlock + (offset & ~0x80000000));
286 cross *= (height / dp);
288 return cross + vertex1;
323 for (
size_t i = 1; i < prismCount; ++i) {
334 geom.
vtx = readVec(posStream, pos_i);
335 geom.
fnrm = readVec(nrmStream, fnrm_i);
336 geom.
enrm1 = readVec(nrmStream, enrm1_i);
337 geom.
enrm2 = readVec(nrmStream, enrm2_i);
338 geom.
enrm3 = readVec(nrmStream, enrm3_i);
348template <KColData::CollisionCheckType Type>
354 auto out = [&](
f32 dist) {
359 *fnrmOut = geom.
fnrm;
362 *flagsOut = attribute;
379 f32 plane_dist = relativePos.
ps_dot(fnrm);
381 if (dist_in_plane <= 0.0f) {
409 if (dist_in_plane >= typeDistance) {
421 if (dist_ab <= 0.0f && dist_bc <= 0.0f && dist_ca <= 0.0f) {
425 if (plane_dist < 0.0f && lastPos.
ps_dot(fnrm) < 0.0f) {
429 return out(dist_in_plane);
433 f32 edge_dist, other_edge_dist;
435 bool swapNorms =
false;
437 if (dist_ab >= dist_ca && dist_ab > dist_bc) {
441 if (dist_ca >= dist_bc) {
443 other_edge_nor = enrm1;
444 other_edge_dist = dist_ca;
448 other_edge_nor = enrm3;
449 other_edge_dist = dist_bc;
452 }
else if (dist_bc >= dist_ca) {
456 if (dist_ab >= dist_ca) {
458 other_edge_nor = enrm2;
459 other_edge_dist = dist_ab;
463 other_edge_nor = enrm1;
464 other_edge_dist = dist_ca;
471 if (dist_bc >= dist_ab) {
473 other_edge_nor = enrm3;
474 other_edge_dist = dist_bc;
478 other_edge_nor = enrm2;
479 other_edge_dist = dist_ab;
484 f32 cos = edge_nor.
ps_dot(other_edge_nor);
486 if (cos * edge_dist > other_edge_dist) {
488 if (edge_dist > plane_dist) {
494 f32 sq_sin = cos * cos - 1.0f;
497 std::swap(edge_dist, other_edge_dist);
501 std::swap(edge_nor, other_edge_nor);
504 f32 t = (cos * edge_dist - other_edge_dist) / sq_sin;
505 f32 s = edge_dist - t * cos;
506 const EGG::Vector3f corner_pos = edge_nor * t + other_edge_nor * s;
510 if (cornerDot > plane_dist * plane_dist) {
518 if (sq_dist < plane_dist * plane_dist || sq_dist <= 0.0f) {
522 f32 dist = EGG::Mathf::sqrt(sq_dist) - plane_dist;
530 if (lastPos.
ps_dot(fnrm) < 0.0f) {
542 u16 *flagsOut,
bool movement) {
555 f32 plane_dist = relativePos.
ps_dot(fnrm);
556 f32 dist_in_plane = 0.01f - plane_dist;
557 if (dist_in_plane <= 0.0f) {
563 if (dist_ca >= 0.01f) {
569 if (dist_ab >= 0.01f) {
575 if (dist_bc >= 0.01f) {
588 *distOut = dist_in_plane;
596 *flagsOut = attribute;
#define KCL_ATTRIBUTE_TYPE_BIT(x)
Given the full 2 byte KCL flag for a triangle, extracts the "Base Type" portion of the flag.
#define KCL_TYPE_DIRECTIONAL
0x05070000
A stream of data stored in memory.
bool checkSphereMovement(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *attributeOut)
Iterates the local data block to check for directional collision.
void narrowScopeLocal(const EGG::Vector3f &pos, f32 radius, KCLTypeMask mask)
bool checkPoint(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *attributeOut)
const u16 * searchBlock(const EGG::Vector3f &pos)
Finds the data block corresponding to the provided position.
std::array< u16, 256 > m_prismCache
bool checkSphereCollision(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut)
owning_span< KCollisionPrismGeometry > m_prismGeometry
One-indexed array of each prism's geometry.
bool checkCollision(u16 prismIdx, f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut)
This is a combination of the three collision checks in the base game.
bool checkSphereSingle(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut)
void lookupPoint(const EGG::Vector3f &pos, const EGG::Vector3f &prevPos, KCLTypeMask typeMask)
Sets members in preparation of a subsequent point collision check call.
owning_span< u16 > m_prismAttributes
One-indexed array of each prism's KCL attribute.
KColData(const void *file)
void preloadPrismGeometry()
Resolves each prism's vertex, height, and normal indices into a contiguous struct.
u32 m_areaXBlocksShift
Used to initialize octree navigation.
bool checkPointMovement(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *attributeOut)
void computeBBox()
Calculates a EGG::BoundBox3f that describes the boundary of the track's KCL.
u32 m_blockWidthShift
Used to initialize octree navigation.
u32 m_areaXWidthMask
The x dimension of the octree's bounding box.
void lookupSphereCached(const EGG::Vector3f &p1, const EGG::Vector3f &p2, u32 typeMask, f32 radius)
void narrowPolygon_EachBlock(const u16 *prismArray)
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.
u32 m_areaXYBlocksShift
Used to initialize octree navigation.
f32 m_sphereRadius
Clamps the sphere we check collision against.
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.
EGG::Vector3f m_cachedPos
bool checkPointCollision(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut)
u32 m_areaYWidthMask
The y dimension of the octree's bounding box.
EGG::Vector3f m_areaMinPos
u32 m_areaZWidthMask
The z dimension of the octree's bounding box.
bool checkSphere(f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut)
Iterates the list of looked-up triangles to see if we are colliding.
owning_span()
Uninitialized buffer.
Represents a sphere in 3D space.
constexpr bool isInsideOtherSphere(const Sphere3f &rhs) const
Not actually part of EGG. Included here for compartmentalization.
constexpr f32 ps_dot() const
Paired-singles dot product implementation.
void read(Stream &stream)
Initializes a Vector3f by reading 12 bytes from the stream.
constexpr void set(f32 val)
constexpr f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
constexpr Vector3f cross(const Vector3f &rhs) const
constexpr Vector3f minimize(const Vector3f &rhs) const
Returns a vector whose elements are the min of the elements of both vectors.
constexpr Vector3f maximize(const Vector3f &rhs) const
Returns a vector whose elements are the max of the elements of both vectors.
constexpr f32 ps_squareMag() const
Differs from ps_dot due to variation in which operands are fused.
Stores the vertex, height and normals of a collision prism.
f32 height
The height of the tri.
EGG::Vector3f enrm2
The second edge's normal.
EGG::Vector3f vtx
The first vertex of the tri.
EGG::Vector3f enrm1
The first edge's normal.
EGG::Vector3f fnrm
The face normal.
EGG::Vector3f enrm3
The third edge's normal.