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 Kinoko {
7
8namespace Host {
9
10class Context;
11
12} // namespace Host
13
14namespace Kart {
15
16class KartObject;
17
18} // namespace Kart
19
20namespace Field {
21
22class ObjectCollidable;
23class ObjectDrivable;
24
28enum class eBoxColFlag {
29 Driver = 0,
30 Object = 3,
31 Drivable = 4,
32 PermRecalcAABB = 8,
33 Intangible = 9,
34 Active = 10,
35 TempRecalcAABB = 11,
36};
37typedef EGG::TBitFlag<u32, eBoxColFlag> BoxColFlag;
38
40struct BoxColUnit {
41 BoxColUnit();
43
44 void init(f32 radius, f32 maxSpeed, const EGG::Vector3f *pos, const BoxColFlag &flag,
45 void *userData);
46 void makeInactive();
47 void resize(f32 radius, f32 maxSpeed);
48 void reinsert();
49 void search(const BoxColFlag &flag);
50
51 const EGG::Vector3f *m_pos;
52 f32 m_radius;
53 f32 m_range;
54 BoxColFlag m_flag;
55 void *m_userData;
56 s16 m_highPointIdx;
57 s16 m_lowPointIdx;
58 f32 m_xMax;
59 f32 m_xMin;
60};
61
63 f32 z;
64 u8 highPoint;
65 u8 unitID;
66};
67
69 f32 z;
70 u8 lowPoint;
71 u8 minLowPoint;
72};
73
76 friend class Host::Context;
77
78public:
80 ~BoxColManager() override;
81
82 void clear();
83 void calc();
84
85 [[nodiscard]] ObjectCollidable *getNextObject();
86 [[nodiscard]] ObjectDrivable *getNextDrivable();
87
88 void resetIterators();
89
90 [[nodiscard]] BoxColUnit *insertDriver(f32 radius, f32 maxSpeed, const EGG::Vector3f *pos,
91 bool alwaysRecalc, Kart::KartObject *kartObject);
92 [[nodiscard]] BoxColUnit *insertObject(f32 radius, f32 maxSpeed, const EGG::Vector3f *pos,
93 bool alwaysRecalc, void *userData);
94 [[nodiscard]] BoxColUnit *insertDrivable(f32 radius, f32 maxSpeed, const EGG::Vector3f *pos,
95 bool alwaysRecalc, void *userData);
96
97 void reinsertUnit(BoxColUnit *unit);
98 void remove(BoxColUnit *&unit);
99 void search(BoxColUnit *unit, const BoxColFlag &flag);
100 void search(f32 radius, const EGG::Vector3f &pos, const BoxColFlag &flag);
101
102 [[nodiscard]] bool isSphereInSpatialCache(f32 radius, const EGG::Vector3f &pos,
103 const BoxColFlag &flag) const;
104
105 static BoxColManager *CreateInstance();
106 static void DestroyInstance();
107 [[nodiscard]] static BoxColManager *Instance();
108
109private:
110 [[nodiscard]] void *getNextImpl(s32 &id, const BoxColFlag &flag);
111 void iterate(s32 &iter, const BoxColFlag &flag);
112 [[nodiscard]] BoxColUnit *insert(f32 radius, f32 maxSpeed, const EGG::Vector3f *pos,
113 const BoxColFlag &flag, void *userData);
114 void searchImpl(BoxColUnit *unit, const BoxColFlag &flag);
115 void searchImpl(f32 radius, const EGG::Vector3f &pos, const BoxColFlag &flag);
116
117 static constexpr size_t MAX_UNIT_COUNT = 0x100;
118
119 std::array<BoxColHighPoint, MAX_UNIT_COUNT> m_highPoints;
120 std::array<BoxColLowPoint, MAX_UNIT_COUNT> m_lowPoints;
121 std::array<BoxColUnit, MAX_UNIT_COUNT> m_unitPool;
122 std::array<BoxColUnit *, MAX_UNIT_COUNT> m_units;
123
125 std::array<u32, MAX_UNIT_COUNT> m_unitIDs;
126
127 s32 m_unitCount;
128 s32 m_nextUnitID;
129 s32 m_nextObjectID;
130 s32 m_nextDrivableID;
131 s32 m_maxID;
132 BoxColUnit *m_cacheQueryUnit;
133 EGG::Vector3f m_cachePoint;
134 f32 m_cacheRadius;
135 BoxColFlag m_cacheFlag;
136
137 static BoxColManager *s_instance;
138};
139
140} // namespace Field
141
142} // namespace Kinoko
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.
void * getNextImpl(s32 &id, const BoxColFlag &flag)
Helper function since the getters share all code except the flag.
std::array< BoxColUnit *, MAX_UNIT_COUNT > m_units
Units within our search bounds.
std::array< BoxColUnit, MAX_UNIT_COUNT > m_unitPool
Where all the units live.
std::array< BoxColHighPoint, MAX_UNIT_COUNT > m_highPoints
A unit's rightmost Z-axis point.
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.
BoxColManager()
Creates two intangible units to represent the spatial bounds.
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:71
The highest level abstraction for a kart.
Definition KartObject.hh:11
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.
Wrapper around an integral type with an enum corresponding to its bits.
Definition BitFlag.hh:23
A 3D float vector.
Definition Vector.hh:107
A representation of the boundaries of an entity that has dynamic collision.