A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
ObjectBase.cc
1#include "ObjectBase.hh"
2
3#include "game/field/ObjectDirector.hh"
4
5#include "game/system/CourseMap.hh"
6#include "game/system/ResourceManager.hh"
7#include "game/system/map/MapdataPointInfo.hh"
8
9#include <egg/math/Math.hh>
10
11#include <cstring>
12
13namespace Field {
14
16ObjectBase::ObjectBase(const System::MapdataGeoObj &params)
17 : m_drawMdl(nullptr), m_resFile(nullptr), m_id(static_cast<ObjectId>(params.id())),
18 m_flags(0x3), m_pos(params.pos()), m_rot(params.rot() * DEG2RAD), m_scale(params.scale()),
19 m_transform(EGG::Matrix34f::ident), m_mapObj(&params) {}
20
22ObjectBase::~ObjectBase() {
23 delete m_resFile;
24 delete m_drawMdl;
25}
26
28void ObjectBase::calcModel() {
29 calcTransform();
30}
31
33const char *ObjectBase::getResources() const {
34 const auto &flowTable = ObjectDirector::Instance()->flowTable();
35 const auto *collisionSet = flowTable.set(flowTable.slot(m_id));
36 ASSERT(collisionSet);
37 return collisionSet->resources;
38}
39
41void ObjectBase::loadGraphics() {
42 const char *name = getResources();
43 if (strcmp(name, "-") == 0) {
44 return;
45 }
46
47 char filename[128];
48 snprintf(filename, sizeof(filename), "%s.brres", name);
49
50 auto *resMgr = System::ResourceManager::Instance();
51 const void *resFile = resMgr->getFile(filename, nullptr, System::ArchiveId::Course);
52 if (resFile) {
53 m_resFile = new Abstract::g3d::ResFile(resFile);
54 m_drawMdl = new Render::DrawMdl;
55 }
56}
57
59void ObjectBase::loadRail() {
60 if (!m_mapObj) {
61 return;
62 }
63
64 s16 pathId = m_mapObj->pathId();
65
66 if (pathId == -1) {
67 return;
68 }
69
70 auto *point = System::CourseMap::Instance()->getPointInfo(pathId);
71 f32 speed = static_cast<f32>(m_mapObj->setting(0));
72
73 if (point->setting(0) == 0) {
74 m_railInterpolator = new RailLinearInterpolator(speed, pathId);
75 } else {
76 m_railInterpolator = new RailSmoothInterpolator(speed, pathId);
77 }
78}
79
81const char *ObjectBase::getKclName() const {
82 const auto &flowTable = ObjectDirector::Instance()->flowTable();
83 const auto *collisionSet = flowTable.set(flowTable.slot(m_id));
84 ASSERT(collisionSet);
85 return collisionSet->resources;
86}
87
89void ObjectBase::calcTransform() {
90 if (m_flags & 2) {
91 m_transform.makeRT(m_rot, m_pos);
92 m_flags &= ~0x3;
93 } else if (m_flags & 1) {
94 m_transform.setBase(3, m_pos);
95 }
96}
97
99void ObjectBase::linkAnims(const std::span<const char *> &names,
100 const std::span<Render::AnmType> types) {
101 if (!m_drawMdl) {
102 return;
103 }
104
105 ASSERT(names.size() == types.size());
106
107 for (size_t i = 0; i < names.size(); ++i) {
108 m_drawMdl->linkAnims(i, m_resFile, names[i], types[i]);
109 }
110}
111
113void ObjectBase::setMatrixTangentTo(const EGG::Vector3f &up, const EGG::Vector3f &tangent) {
114 m_flags |= 4;
115 SetRotTangentHorizontal(m_transform, up, tangent);
116 m_transform.setBase(3, m_pos);
117}
118
120EGG::Vector3f ObjectBase::RotateAxisAngle(f32 angle, const EGG::Vector3f &axis,
121 const EGG::Vector3f &v1) {
122 EGG::Matrix34f mat;
123 mat.setBase(3, EGG::Vector3f::zero);
124 mat.setAxisRotation(angle, axis);
125 return mat.ps_multVector(v1);
126}
127
129void ObjectBase::SetRotTangentHorizontal(EGG::Matrix34f &mat, const EGG::Vector3f &up,
130 const EGG::Vector3f &tangent) {
131 EGG::Vector3f vec = tangent - up * tangent.dot(up);
132 vec.normalise2();
133
134 mat.setBase(0, up.cross(vec));
135 mat.setBase(1, up);
136 mat.setBase(2, vec);
137}
138
139} // namespace Field
Represents a binary resource file which contains object models, textures, and animations.
Definition ResFile.hh:15
A 3 x 4 matrix.
Definition Matrix.hh:8
void setBase(size_t col, const Vector3f &base)
Sets one column of a matrix.
Definition Matrix.cc:189
void setAxisRotation(f32 angle, const Vector3f &axis)
Rotates the matrix about an axis.
Definition Matrix.cc:175
Vector3f ps_multVector(const Vector3f &vec) const
Paired-singles impl. of multVector.
Definition Matrix.cc:232
EGG core library.
Definition Archive.cc:6
Pertains to collision.
A 3D float vector.
Definition Vector.hh:83
f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
Definition Vector.hh:182