A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
ObjectBase.hh
Go to the documentation of this file.
1#pragma once
2
4
5#include <egg/core/BitFlag.hh>
6#include <egg/math/Matrix.hh>
7
8namespace Kinoko {
9
10namespace Abstract::g3d {
11
12class ResFile;
13
14} // namespace Abstract::g3d
15
16namespace Render {
17
18enum class AnmType;
19class DrawMdl;
20
21} // namespace Render
22
23namespace System {
24
25class MapdataGeoObj;
26
27} // namespace System
28
29namespace Field {
30
31class BoxColUnit;
32class RailInterpolator;
33
46public:
48 enum class eFlags {
49 Position = 0,
50 Rotation = 1,
51 Matrix = 2,
52 Scale = 3,
53 };
54
57
61 enum class eLoadFlags {
62 Calc = 0,
63 Draw = 1,
64 AlwaysRecalc = 2,
65 };
66
69
70 ObjectBase(const System::MapdataGeoObj &params);
71 ObjectBase(const char *name, const EGG::Vector3f &pos, const EGG::Vector3f &rot,
72 const EGG::Vector3f &scale);
73 virtual ~ObjectBase();
74
75 virtual void init();
76 virtual void calc();
77 virtual void calcModel();
78 virtual void loadGraphics();
79 virtual void loadAnims();
80 virtual void loadRail();
81 [[nodiscard]] virtual const char *getResources() const;
82 [[nodiscard]] virtual const char *getName() const;
83 [[nodiscard]] virtual LoadFlags loadFlags() const;
84 [[nodiscard]] virtual const char *getKclName() const;
85 [[nodiscard]] virtual const EGG::Vector3f &getPosition() const;
86 [[nodiscard]] virtual f32 getCollisionRadius() const;
87 [[nodiscard]] virtual ObjectId id() const;
88
90 virtual void load() = 0;
91
93 virtual void createCollision() = 0;
94
96 virtual void calcCollisionTransform() = 0;
97
98 void resize(f32 radius, f32 maxSpeed);
100 void disableCollision() const;
101 void enableCollision() const;
102
104
107 void setPos(const EGG::Vector3f &pos) {
109 m_pos = pos;
110 }
111
114 void addPos(const EGG::Vector3f &v) {
116 m_pos += v;
117 }
118
121 void subPos(const EGG::Vector3f &v) {
123 m_pos -= v;
124 }
125
132
139
142 void setRot(const EGG::Vector3f &rot) {
144 m_rot = rot;
145 }
146
150 m_rot = rot;
151 }
152
155 void addRot(const EGG::Vector3f &v) {
156 m_rotUpdated = true;
158 m_rot += v;
159 }
160
163 void subRot(const EGG::Vector3f &v) {
164 m_rotUpdated = true;
166 m_rot -= v;
167 }
168
172 void setTransform(const EGG::Matrix34f &mat) {
173 m_rotUpdated = false;
176 m_transform = mat;
177 m_pos = mat.base(3);
178 }
179
181
183
186 [[nodiscard]] const RailInterpolator *railInterpolator() const {
187 return m_railInterpolator;
188 }
189
192 [[nodiscard]] const EGG::Vector3f &pos() const {
193 return m_pos;
194 }
195
198 [[nodiscard]] const EGG::Vector3f &scale() const {
199 return m_scale;
200 }
201
204 [[nodiscard]] const EGG::Vector3f &rot() const {
205 return m_rot;
206 }
207
210 [[nodiscard]] const EGG::Matrix34f &transform() {
212 return m_transform;
213 }
214
220 [[nodiscard]] const BoxColUnit *unit() const {
221 return m_boxColUnit;
222 }
223
225
226protected:
227 void calcTransform();
228 void linkAnims(const std::span<const char *> &names, const std::span<Render::AnmType> types);
229 void calcRotLock();
230 void setMatrixTangentTo(const EGG::Vector3f &up, const EGG::Vector3f &tangent);
232
233 static void SetRotTangentHorizontal(EGG::Matrix34f &mat, const EGG::Vector3f &up,
234 const EGG::Vector3f &tangent);
235 [[nodiscard]] static f32 CheckPointAgainstLineSegment(const EGG::Vector3f &point,
236 const EGG::Vector3f &a, const EGG::Vector3f &b);
237 [[nodiscard]] static EGG::Vector3f RotateXZByYaw(f32 angle, const EGG::Vector3f &v);
238 [[nodiscard]] static EGG::Vector3f RotateAxisAngle(f32 angle, const EGG::Vector3f &axis,
239 const EGG::Vector3f &v1);
240 [[nodiscard]] static EGG::Matrix34f OrthonormalBasis(const EGG::Vector3f &v);
241 [[nodiscard]] static EGG::Vector3f AdjustVecForward(f32 sidewaysScalar, f32 forwardScalar,
242 f32 minSpeed, const EGG::Vector3f &src, EGG::Vector3f forward);
243 [[nodiscard]] static f32 CalcParabolicDisplacement(f32 initVel, f32 accel, u32 frame);
244
251
252private:
257 enum class PendingRotation {
259 TangentTo,
261 };
262
264
271
275};
276
277} // namespace Field
278
279} // namespace Kinoko
Represents a binary resource file which contains object models, textures, and animations.
Definition ResFile.hh:14
A 3 x 4 matrix.
Definition Matrix.hh:10
constexpr Vector3f base(size_t col) const
Get a particular column from a matrix.
Definition Matrix.hh:408
virtual void init()
Runs once during race initialization.
Definition ObjectBase.cc:87
void setScale(f32 scale)
Sets the uniform scale of the object and updates the scale flag.
static f32 CalcParabolicDisplacement(f32 initVel, f32 accel, u32 frame)
Solves the standard kinematic equation .
eLoadFlags
Describes whether the object needs to be updated every frame.
Definition ObjectBase.hh:61
EGG::TBitFlag< u8, eFlags > Flags
A bitfield of eFlags that represents the state of the object.
Definition ObjectBase.hh:56
EGG::Vector3f m_scale
The scale of the object.
void resize(f32 radius, f32 maxSpeed)
Resizes the BoxColUnit associated with the object.
static EGG::Vector3f RotateXZByYaw(f32 angle, const EGG::Vector3f &v)
Rotates a vector around the Y-axis and returns the XZ-plane portion of the vector.
Render::DrawMdl * m_drawMdl
The draw model associated with the object.
void subPos(const EGG::Vector3f &v)
Subtracts a vector from the object's position and updates the position matrix flag.
void addRot(const EGG::Vector3f &v)
Adds a vector to the object's rotation and updates the rotation flag.
virtual void load()=0
Loads resources, collision, and registers the object to the ObjectDirector.
void calcTransform()
Updates the transform matrix of the object based on its position and rotation.
Abstract::g3d::ResFile * m_resFile
The resource file associated with the object.
virtual void calc()
Runs once per frame if the Calc flag is set in loadFlags().
Definition ObjectBase.cc:91
static EGG::Vector3f AdjustVecForward(f32 sidewaysScalar, f32 forwardScalar, f32 minSpeed, const EGG::Vector3f &src, EGG::Vector3f forward)
Adjusts a vector based on sideways and forward scalars, ensuring a minimum magnitude.
static EGG::Vector3f RotateAxisAngle(f32 angle, const EGG::Vector3f &axis, const EGG::Vector3f &v1)
Rotates a vector around an arbitrary axis by a given angle.
void setTransform(const EGG::Matrix34f &mat)
Sets the transformation matrix of the object and updates the matrix flag.
static f32 CheckPointAgainstLineSegment(const EGG::Vector3f &point, const EGG::Vector3f &a, const EGG::Vector3f &b)
Calculates on what side of line segment ab point lies.
static void SetRotTangentHorizontal(EGG::Matrix34f &mat, const EGG::Vector3f &up, const EGG::Vector3f &tangent)
Sets the rotation of a matrix based on an up vector and a tangent vector.
void enableCollision() const
Re-enables collision checks for this object in the BoxColManager.
EGG::Vector3f m_pos
The position of the object.
void setScale(const EGG::Vector3f &scale)
Sets the scale of the object and updates the scale flag.
const EGG::Vector3f & pos() const
Gets the position of the object in world space.
virtual void loadRail()
Loads the rail interpolator for the object.
EGG::Vector3f m_pendingWork0
Up/forward vector depending on the PendingRotation type.
bool m_rotUpdated
Whether m_rot is up-to-date.
virtual void createCollision()=0
Creates a collision object that inherits from ObjectCollisionBase.
const BoxColUnit * unit() const
Fetches the BoxColUnit associated with the object.
const EGG::Matrix34f & transform()
Gets the transformation matrix of the object.
virtual const char * getName() const
Fetches the name of the object from the ObjectFlowTable.
const System::MapdataGeoObj *const m_mapObj
The KMP object associated with the object.
EGG::Vector3f m_pendingWork1
Tangent vector of the PendingRotation.
virtual const char * getResources() const
Fetches the name of the brres resource file associated with the object, if any.
void setMatrixFromOrthonormalBasisAndPos(const EGG::Vector3f &v)
Sets the transformation matrix based on an orthonormal basis and the object's position.
const EGG::Vector3f & scale() const
Gets the scale of the object.
PendingRotation m_pendingRot
Rotation of m_transform yet to be computed.
ObjectBase(const System::MapdataGeoObj &params)
Constructor.
Definition ObjectBase.cc:29
void unregisterCollision()
Unregisters the BoxColUnit associated with the object from the BoxColManager.
void setPos(const EGG::Vector3f &pos)
Sets the position of the object in world space and updates the position matrix flag.
const ObjectId m_id
The ID of the object.
void disableCollision() const
Disables collision checks for this object in the BoxColManager.
void addPos(const EGG::Vector3f &v)
Adds a vector to the object's position and updates the position matrix flag.
virtual const char * getKclName() const
Fetches the name of the KCL resource associated with the object from the ObjectFlowTable.
virtual void calcModel()
Updates the object's model.
Definition ObjectBase.cc:96
void setRot(const EGG::Vector3f &rot)
Sets the rotation of the object and updates the rotation flag.
virtual void loadAnims()
Links associated animations from the object's resource file to the DrawMdl, if any.
void linkAnims(const std::span< const char * > &names, const std::span< Render::AnmType > types)
Links animations from the object's resource file to the DrawMdl.
void setMatrixTangentTo(const EGG::Vector3f &up, const EGG::Vector3f &tangent)
Sets the object's transformation matrix based on an up vector and a tangent vector.
eFlags
Tracks what properties have changed and require recalculation in the transform matrix.
Definition ObjectBase.hh:48
@ Position
The object's position has changed.
Definition ObjectBase.hh:49
@ Scale
The object's scale has changed.
Definition ObjectBase.hh:52
@ Rotation
The object's rotation has changed.
Definition ObjectBase.hh:50
@ Matrix
The object's transformation matrix has changed.
Definition ObjectBase.hh:51
void setRotNoFlag(const EGG::Vector3f &rot)
Sets the rotation of the object without updating the rotation flag.
virtual void calcCollisionTransform()=0
Updates the position, rotation, and scale of the collision object.
virtual void loadGraphics()
Loads the resources associated with the object, if any.
EGG::Matrix34f m_transform
The 3x4 matrix representing the object's position and rotation.
EGG::TBitFlag< u8, eLoadFlags > LoadFlags
A bitfield of eLoadFlags that represents the update requirements of the object.
Definition ObjectBase.hh:68
void resolvePendingRotation()
Computes the rotation columns of m_transform from the PendingRotation, if any.
RailInterpolator * m_railInterpolator
The rail interpolator associated with the object.
virtual ObjectId id() const
Fetches the unique identifier of the object.
const RailInterpolator * railInterpolator() const
Gets the RailInterpolator associated with the object.
Flags m_flags
The properties of the transform matrix to be recalculated.
void calcRotLock()
Refreshes the object's rotation vector based on its transform matrix.
void subRot(const EGG::Vector3f &v)
Subtracts a vector from the object's rotation and updates the rotation flag.
const EGG::Vector3f & rot() const
Gets the rotation of the object.
virtual LoadFlags loadFlags() const
Returns a bitmask indicating which lifecycle hooks should be called for the object.
PendingRotation
The source of a rotation for m_transform which has not been computed yet.
EGG::Vector3f m_rot
The rotation of the object (in radians).
static EGG::Matrix34f OrthonormalBasis(const EGG::Vector3f &v)
Creates an orthonormal basis from a given vector.
virtual f32 getCollisionRadius() const
Collision radius to use for GJK checks.
virtual const EGG::Vector3f & getPosition() const
The position of the object in world space.
BoxColUnit * m_boxColUnit
The box collision unit associated with the object.
Base class for interpolating along a rail. This class is responsible for calculating the current posi...
Pertains to collision.
ObjectId
Unique identifiers for each object.
Definition ObjectId.hh:10
@ None
Represents the absence of an object.
Definition ObjectId.hh:11
Pertains to rendering the kart model.
Definition ObjectBase.hh:16
High-level handling for generic system operations, such as input reading, race configuration,...
Definition ObjectBase.hh:23
float f32
Definition Memory.hh:21
uint32_t u32
Definition Memory.hh:18
Wrapper around an integral type with an enum corresponding to its bits.
Definition BitFlag.hh:23
constexpr TBitFlag< T, E > & setBit(Es... es)
Sets the corresponding bits for the provided enum values.
Definition BitFlag.hh:74
A 3D float vector.
Definition Vector.hh:108
constexpr void set(f32 val)
Definition Vector.hh:126
A representation of the boundaries of an entity that has dynamic collision.