A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Kinoko::Field::KColData Class Reference

#include <KColData.hh>

Description

Performs lookups for KCL triangles.

Definition at line 68 of file KColData.hh.

Classes

struct  KCollisionPrism
struct  KCollisionPrismGeometry
 Stores the vertex, height and normals of a collision prism. More...

Public Types

enum class  CollisionCheckType {
  Edge ,
  Plane ,
  Movement
}

Public Member Functions

 STATIC_ASSERT (sizeof(KCollisionPrism)==0x10)
 STATIC_ASSERT (sizeof(KCollisionPrismGeometry)==0x40)
 KColData (const void *file)
void narrowScopeLocal (const EGG::Vector3f &pos, f32 radius, KCLTypeMask mask)
void narrowPolygon_EachBlock (const u16 *prismArray)
void computeBBox ()
 Calculates a EGG::BoundBox3f that describes the boundary of the track's KCL.
bool checkPointCollision (f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut)
bool checkSphereCollision (f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut)
bool checkSphere (f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut)
 Iterates the list of looked-up triangles to see if we are colliding.
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.
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.
void lookupSphereCached (const EGG::Vector3f &p1, const EGG::Vector3f &p2, u32 typeMask, f32 radius)
const u16 * searchBlock (const EGG::Vector3f &pos)
 Finds the data block corresponding to the provided position.

Static Public Member Functions

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.

Getters

const EGG::BoundBox3f & bbox () const
u16 prismCache (u32 idx) const

Private Member Functions

void preloadPrismGeometry ()
 Resolves each prism's vertex, height, and normal indices into a contiguous struct.
template<CollisionCheckType Type>
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 checkPointCollision (u16 prismIdx, f32 *distOut, EGG::Vector3f *fnrmOut, u16 *flagsOut, bool movement)
 This is a combination of two point collision check functions. They only vary based on whether we are checking movement.
bool checkSphereMovement (f32 *distOut, EGG::Vector3f *fnrmOut, u16 *attributeOut)
 Iterates the local data block to check for directional collision.
bool checkPointMovement (f32 *distOut, EGG::Vector3f *fnrmOut, u16 *attributeOut)
bool checkPoint (f32 *distOut, EGG::Vector3f *fnrmOut, u16 *attributeOut)

Private Attributes

const void * m_posData
const void * m_nrmData
const void * m_prismData
const void * m_blockData
f32 m_prismThickness
EGG::Vector3f m_areaMinPos
u32 m_areaXWidthMask
 The x dimension of the octree's bounding box.
u32 m_areaYWidthMask
 The y dimension of the octree's bounding box.
u32 m_areaZWidthMask
 The z dimension of the octree's bounding box.
u32 m_blockWidthShift
 Used to initialize octree navigation.
u32 m_areaXBlocksShift
 Used to initialize octree navigation.
u32 m_areaXYBlocksShift
 Used to initialize octree navigation.
f32 m_sphereRadius
 Clamps the sphere we check collision against.
EGG::Vector3f m_pos
EGG::Vector3f m_prevPos
EGG::Vector3f m_movement
f32 m_radius
KCLTypeMask m_typeMask
const u16 * m_prismIter
EGG::BoundBox3f m_bbox
std::array< u16, 256 > m_prismCache
u16 * m_prismCacheTop
u16 * m_cachedPrismArray
EGG::Vector3f m_cachedPos
f32 m_cachedRadius
owning_span< KCollisionPrismGeometry > m_prismGeometry
 One-indexed array of each prism's geometry.
owning_span< u16 > m_prismAttributes
 One-indexed array of each prism's KCL attribute.

Member Enumeration Documentation

◆ CollisionCheckType

enum class Kinoko::Field::KColData::CollisionCheckType
strong

Definition at line 70 of file KColData.hh.

Constructor & Destructor Documentation

◆ KColData()

Kinoko::Field::KColData::KColData ( const void * file)
0x807BDC5C

Definition at line 14 of file KColData.cc.

Member Function Documentation

◆ bbox()

const EGG::BoundBox3f & Kinoko::Field::KColData::bbox ( ) const
inlinenodiscard

Definition at line 126 of file KColData.hh.

◆ checkCollision()

template<KColData::CollisionCheckType Type>
bool Kinoko::Field::KColData::checkCollision ( u16 prismIdx,
f32 * distOut,
EGG::Vector3f * fnrmOut,
u16 * flagsOut )
nodiscardprivate

This is a combination of the three collision checks in the base game.

Parameters
prismIdxThe index of the prism to check for collision against

The checks vary only by a few if-statements, related to whether we are checking for:

  1. A collision with at least the triangle edge (0x807C0F00)
  2. A collision with the triangle plane (0x807C1514)
  3. A collision such that we are inside the triangle (0x807C0884)

Definition at line 349 of file KColData.cc.

◆ checkPoint()

bool Kinoko::Field::KColData::checkPoint ( f32 * distOut,
EGG::Vector3f * fnrmOut,
u16 * attributeOut )
nodiscardprivate0x807C21F4

Definition at line 628 of file KColData.cc.

◆ checkPointCollision() [1/2]

bool Kinoko::Field::KColData::checkPointCollision ( f32 * distOut,
EGG::Vector3f * fnrmOut,
u16 * flagsOut )
nodiscard0x807C1F80

Definition at line 111 of file KColData.cc.

◆ checkPointCollision() [2/2]

bool Kinoko::Field::KColData::checkPointCollision ( u16 prismIdx,
f32 * distOut,
EGG::Vector3f * fnrmOut,
u16 * flagsOut,
bool movement )
nodiscardprivate

This is a combination of two point collision check functions. They only vary based on whether we are checking movement.

Parameters
prismIdxThe index of the collision prism to check against

Definition at line 541 of file KColData.cc.

◆ checkPointMovement()

bool Kinoko::Field::KColData::checkPointMovement ( f32 * distOut,
EGG::Vector3f * fnrmOut,
u16 * attributeOut )
nodiscardprivate0x807C1F80

Definition at line 647 of file KColData.cc.

◆ checkSphere()

bool Kinoko::Field::KColData::checkSphere ( f32 * distOut,
EGG::Vector3f * fnrmOut,
u16 * flagsOut )
nodiscard0x807C1514

Iterates the list of looked-up triangles to see if we are colliding.

Parameters
distOutIf colliding, returns the distance between the player and the triangle
fnrmOutIf colliding, returns the floor normal of the triangle
flagsOutIf colliding, returns the KCL attributes for that triangle
Returns
whether or not the player is colliding with the triangle

Definition at line 128 of file KColData.cc.

◆ checkSphereCollision()

bool Kinoko::Field::KColData::checkSphereCollision ( f32 * distOut,
EGG::Vector3f * fnrmOut,
u16 * flagsOut )
nodiscard0x807C2410

Definition at line 117 of file KColData.cc.

◆ checkSphereMovement()

bool Kinoko::Field::KColData::checkSphereMovement ( f32 * distOut,
EGG::Vector3f * fnrmOut,
u16 * attributeOut )
nodiscardprivate0x807C0884

Iterates the local data block to check for directional collision.

Parameters
distOutIf colliding, returns the distance between the player and the tri
fnrmOutIf colliding, returns the floor normal of the triangle
attributeOutIf colliding, returns the KCL attributes for that triangle
Returns
Whether or not a collision has occurred

Definition at line 608 of file KColData.cc.

◆ checkSphereSingle()

bool Kinoko::Field::KColData::checkSphereSingle ( f32 * distOut,
EGG::Vector3f * fnrmOut,
u16 * flagsOut )
nodiscard0x807C0F00

Definition at line 148 of file KColData.cc.

◆ computeBBox()

void Kinoko::Field::KColData::computeBBox ( )
0x807BDDFC

Calculates a EGG::BoundBox3f that describes the boundary of the track's KCL.

Definition at line 90 of file KColData.cc.

◆ GetVertex()

EGG::Vector3f Kinoko::Field::KColData::GetVertex ( f32 height,
const EGG::Vector3f & vertex1,
const EGG::Vector3f & fnrm,
const EGG::Vector3f & enrm3,
const EGG::Vector3f & enrm )
staticnodiscard0x807BDF54

Computes a prism vertex based off of the triangle's normal vectors.

Triangle Vertices Formula
Given a triangle with vertices \(\vec{A}, \vec{B}, \vec{C}\), face normal \(\hat{f}\), and height \(h\), label the edge normals by: \begin{aligned}\hat{en}_1 := e_{AB}, \,\, \hat{en}_2:= e_{AC}, \,\,\hat{en}_3:=e_{BC} \end{aligned} We can recover \(\vec{B}, \vec{C}\) via: \begin{aligned} \vec{B} = \vec{A} + \dfrac{h}{(\hat{en}_2 \times \hat{f}) \cdot \hat{en}_3}\left(\hat{en}_2 \times \hat{f}\right), \, \, \vec{C} = \vec{A} + \dfrac{h}{(\hat{en}_1 \times \hat{f}) \cdot \hat{en}_3}(\hat{en}_1 \times \hat{f}) \, . \end{aligned}

Definition at line 282 of file KColData.cc.

◆ lookupPoint()

void Kinoko::Field::KColData::lookupPoint ( const EGG::Vector3f & pos,
const EGG::Vector3f & prevPos,
KCLTypeMask typeMask )
0x807C1B0C

Sets members in preparation of a subsequent point collision check call.

Definition at line 179 of file KColData.cc.

◆ lookupSphere()

void Kinoko::Field::KColData::lookupSphere ( f32 radius,
const EGG::Vector3f & pos,
const EGG::Vector3f & prevPos,
KCLTypeMask typeMask )
0x807C1BB4

Sets members in preparation of a subsequent sphere collision check call.

Definition at line 190 of file KColData.cc.

◆ lookupSphereCached()

void Kinoko::Field::KColData::lookupSphereCached ( const EGG::Vector3f & p1,
const EGG::Vector3f & p2,
u32 typeMask,
f32 radius )
0x807C1DE8

Definition at line 201 of file KColData.cc.

◆ narrowPolygon_EachBlock()

void Kinoko::Field::KColData::narrowPolygon_EachBlock ( const u16 * prismArray)
0x807C243C

We assume the cache has same endianness as the archive file, so do not parse out the prism index and directly store it in the cache.

Definition at line 73 of file KColData.cc.

◆ narrowScopeLocal()

void Kinoko::Field::KColData::narrowScopeLocal ( const EGG::Vector3f & pos,
f32 radius,
KCLTypeMask mask )
0x807C24C0

Definition at line 57 of file KColData.cc.

◆ preloadPrismGeometry()

void Kinoko::Field::KColData::preloadPrismGeometry ( )
private

Resolves each prism's vertex, height, and normal indices into a contiguous struct.

This is a performance optimization for Kinoko to avoid having to byteswap and index several different arrays to fetch a prism's geometry. Memory cost is 64 bytes per prism for the geometry plus 2 bytes per prism for the attribute, which is at worst ~690KB (for Maple Treeway).

Definition at line 295 of file KColData.cc.

◆ prismCache()

u16 Kinoko::Field::KColData::prismCache ( u32 idx) const
inlinenodiscard

Definition at line 130 of file KColData.hh.

◆ searchBlock()

const u16 * Kinoko::Field::KColData::searchBlock ( const EGG::Vector3f & point)
nodiscard0x807BE030

Finds the data block corresponding to the provided position.

Parameters
pointThe player's position
Returns
the address of the leaf node containing the input point.

Definition at line 224 of file KColData.cc.

Member Data Documentation

◆ m_areaMinPos

EGG::Vector3f Kinoko::Field::KColData::m_areaMinPos
private

Definition at line 156 of file KColData.hh.

◆ m_areaXBlocksShift

u32 Kinoko::Field::KColData::m_areaXBlocksShift
private

Used to initialize octree navigation.

See also
searchBlock.

Definition at line 161 of file KColData.hh.

◆ m_areaXWidthMask

u32 Kinoko::Field::KColData::m_areaXWidthMask
private

The x dimension of the octree's bounding box.

See also
searchBlock.

Definition at line 157 of file KColData.hh.

◆ m_areaXYBlocksShift

u32 Kinoko::Field::KColData::m_areaXYBlocksShift
private

Used to initialize octree navigation.

See also
searchBlock.

Definition at line 162 of file KColData.hh.

◆ m_areaYWidthMask

u32 Kinoko::Field::KColData::m_areaYWidthMask
private

The y dimension of the octree's bounding box.

See also
searchBlock.

Definition at line 158 of file KColData.hh.

◆ m_areaZWidthMask

u32 Kinoko::Field::KColData::m_areaZWidthMask
private

The z dimension of the octree's bounding box.

See also
searchBlock.

Definition at line 159 of file KColData.hh.

◆ m_bbox

EGG::BoundBox3f Kinoko::Field::KColData::m_bbox
private

Definition at line 170 of file KColData.hh.

◆ m_blockData

const void* Kinoko::Field::KColData::m_blockData
private

Definition at line 154 of file KColData.hh.

◆ m_blockWidthShift

u32 Kinoko::Field::KColData::m_blockWidthShift
private

Used to initialize octree navigation.

See also
searchBlock.

Definition at line 160 of file KColData.hh.

◆ m_cachedPos

EGG::Vector3f Kinoko::Field::KColData::m_cachedPos
private

Definition at line 174 of file KColData.hh.

◆ m_cachedPrismArray

u16* Kinoko::Field::KColData::m_cachedPrismArray
private

Definition at line 173 of file KColData.hh.

◆ m_cachedRadius

f32 Kinoko::Field::KColData::m_cachedRadius
private

Definition at line 175 of file KColData.hh.

◆ m_movement

EGG::Vector3f Kinoko::Field::KColData::m_movement
private

Definition at line 166 of file KColData.hh.

◆ m_nrmData

const void* Kinoko::Field::KColData::m_nrmData
private

Definition at line 152 of file KColData.hh.

◆ m_pos

EGG::Vector3f Kinoko::Field::KColData::m_pos
private

Definition at line 164 of file KColData.hh.

◆ m_posData

const void* Kinoko::Field::KColData::m_posData
private

Definition at line 151 of file KColData.hh.

◆ m_prevPos

EGG::Vector3f Kinoko::Field::KColData::m_prevPos
private

Definition at line 165 of file KColData.hh.

◆ m_prismAttributes

owning_span<u16> Kinoko::Field::KColData::m_prismAttributes
private

One-indexed array of each prism's KCL attribute.

Kept separate from m_prismGeometry so that KCollisionPrismGeometry can have better cache alignment.

Definition at line 184 of file KColData.hh.

◆ m_prismCache

std::array<u16, 256> Kinoko::Field::KColData::m_prismCache
private

Definition at line 171 of file KColData.hh.

◆ m_prismCacheTop

u16* Kinoko::Field::KColData::m_prismCacheTop
private

Definition at line 172 of file KColData.hh.

◆ m_prismData

const void* Kinoko::Field::KColData::m_prismData
private

Definition at line 153 of file KColData.hh.

◆ m_prismGeometry

owning_span<KCollisionPrismGeometry> Kinoko::Field::KColData::m_prismGeometry
private

One-indexed array of each prism's geometry.

Kinoko-specific optimization for cache locality. See KCollisionPrismGeometry.

Definition at line 179 of file KColData.hh.

◆ m_prismIter

const u16* Kinoko::Field::KColData::m_prismIter
private

Definition at line 169 of file KColData.hh.

◆ m_prismThickness

f32 Kinoko::Field::KColData::m_prismThickness
private

Definition at line 155 of file KColData.hh.

◆ m_radius

f32 Kinoko::Field::KColData::m_radius
private

Definition at line 167 of file KColData.hh.

◆ m_sphereRadius

f32 Kinoko::Field::KColData::m_sphereRadius
private

Clamps the sphere we check collision against.

See also
searchBlock.

Definition at line 163 of file KColData.hh.

◆ m_typeMask

KCLTypeMask Kinoko::Field::KColData::m_typeMask
private

Definition at line 168 of file KColData.hh.