A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
BoxColManager.hh
1#pragma once
2
3#include <egg/core/BitFlag.hh>
4#include <egg/math/Vector.hh>
5
6namespace Host {
7
8class Context;
9
10} // namespace Host
11
12namespace Kart {
13
14class KartObject;
15
16} // namespace Kart
17
18namespace Field {
19
20class ObjectCollidable;
21class ObjectDrivable;
22
26enum class eBoxColFlag {
27 Driver = 0,
28 Object = 3,
29 Drivable = 4,
30 PermRecalcAABB = 8,
31 Intangible = 9,
32 Active = 10,
33 TempRecalcAABB = 11,
34};
35typedef EGG::TBitFlag<u32, eBoxColFlag> BoxColFlag;
36
38struct BoxColUnit {
39 BoxColUnit();
41
42 void init(f32 radius, f32 maxSpeed, const EGG::Vector3f *pos, const BoxColFlag &flag,
43 void *userData);
44 void makeInactive();
45 void resize(f32 radius, f32 maxSpeed);
46 void reinsert();
47 void search(const BoxColFlag &flag);
48
49 const EGG::Vector3f *m_pos;
50 f32 m_radius;
51 f32 m_range;
52 BoxColFlag m_flag;
53 void *m_userData;
54 s16 m_highPointIdx;
55 s16 m_lowPointIdx;
56 f32 m_xMax;
57 f32 m_xMin;
58};
59
61 f32 z;
62 u8 highPoint;
63 u8 unitID;
64};
65
67 f32 z;
68 u8 lowPoint;
69 u8 minLowPoint;
70};
71
74 friend class Host::Context;
75
76public:
78 ~BoxColManager() override;
79
80 void clear();
81 void calc();
82
83 [[nodiscard]] ObjectCollidable *getNextObject();
84 [[nodiscard]] ObjectDrivable *getNextDrivable();
85
86 void resetIterators();
87
88 [[nodiscard]] BoxColUnit *insertDriver(f32 radius, f32 maxSpeed, const EGG::Vector3f *pos,
89 bool alwaysRecalc, Kart::KartObject *kartObject);
90 [[nodiscard]] BoxColUnit *insertObject(f32 radius, f32 maxSpeed, const EGG::Vector3f *pos,
91 bool alwaysRecalc, void *userData);
92 [[nodiscard]] BoxColUnit *insertDrivable(f32 radius, f32 maxSpeed, const EGG::Vector3f *pos,
93 bool alwaysRecalc, void *userData);
94
95 void reinsertUnit(BoxColUnit *unit);
96 void search(BoxColUnit *unit, const BoxColFlag &flag);
97 void search(f32 radius, const EGG::Vector3f &pos, const BoxColFlag &flag);
98
99 [[nodiscard]] bool isSphereInSpatialCache(f32 radius, const EGG::Vector3f &pos,
100 const BoxColFlag &flag) const;
101
102 static BoxColManager *CreateInstance();
103 static void DestroyInstance();
104 [[nodiscard]] static BoxColManager *Instance();
105
106private:
107 [[nodiscard]] void *getNextImpl(s32 &id, const BoxColFlag &flag);
108 void iterate(s32 &iter, const BoxColFlag &flag);
109 [[nodiscard]] BoxColUnit *insert(f32 radius, f32 maxSpeed, const EGG::Vector3f *pos,
110 const BoxColFlag &flag, void *userData);
111 void remove(BoxColUnit *&pUnit);
112 void searchImpl(BoxColUnit *unit, const BoxColFlag &flag);
113 void searchImpl(f32 radius, const EGG::Vector3f &pos, const BoxColFlag &flag);
114
115 static constexpr size_t MAX_UNIT_COUNT = 0x100;
116
117 std::array<BoxColHighPoint, MAX_UNIT_COUNT> m_highPoints;
118 std::array<BoxColLowPoint, MAX_UNIT_COUNT> m_lowPoints;
119 std::array<BoxColUnit, MAX_UNIT_COUNT> m_unitPool;
120 std::array<BoxColUnit *, MAX_UNIT_COUNT> m_units;
121
123 std::array<u32, MAX_UNIT_COUNT> m_unitIDs;
124
125 s32 m_unitCount;
126 s32 m_nextUnitID;
127 s32 m_nextObjectID;
128 s32 m_nextDrivableID;
129 s32 m_maxID;
130 BoxColUnit *m_cacheQueryUnit;
131 EGG::Vector3f m_cachePoint;
132 f32 m_cacheRadius;
133 BoxColFlag m_cacheFlag;
134
135 static BoxColManager *s_instance;
136};
137
138} // namespace Field
An interface for ensuring certain structures and classes are destroyed with the heap.
Definition Disposer.hh:11
Spatial indexing manager for entities with dynamic collision.
std::array< BoxColUnit *, MAX_UNIT_COUNT > m_units
Units within our search bounds.
void * getNextImpl(s32 &id, const BoxColFlag &flag)
Helper function since the getters share all code except the flag.
std::array< BoxColHighPoint, MAX_UNIT_COUNT > m_highPoints
A unit's rightmost Z-axis point.
BoxColManager()
Creates two intangible units to represent the spatial bounds.
std::array< BoxColLowPoint, MAX_UNIT_COUNT > m_lowPoints
A unit's leftmost Z-axis point;.
std::array< u32, MAX_UNIT_COUNT > m_unitIDs
Specifies what unit to retrieve from the pool during allocation.
std::array< BoxColUnit, MAX_UNIT_COUNT > m_unitPool
Where all the units live.
void calc()
Recalculate the bounds of all active units having PermRecalcAABB or TempRecalcAABB flag,...
Contexts can be used to restore a previous memory state for the current session.
Definition Context.hh:59
The highest level abstraction for a kart.
Definition KartObject.hh:11
Pertains to collision.
eBoxColFlag
A bitfield that represents the state and type of a given BoxColUnit.
@ TempRecalcAABB
Only recalculate once.
@ PermRecalcAABB
Recalculate this unit's spatial indexing every frame.
@ Intangible
Ignore collision with the unit.
Represents the host application.
Definition HeapCommon.hh:9
Pertains to kart-related functionality.
Wrapper around an integral type with an enum corresponding to its bits.
Definition BitFlag.hh:16
A 3D float vector.
Definition Vector.hh:87
A representation of the boundaries of an entity that has dynamic collision.