A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
ObjectChoropu.cc
1#include "ObjectChoropu.hh"
2
3#include "game/field/ObjectDirector.hh"
4#include "game/field/RailManager.hh"
5
6#include "game/system/RaceManager.hh"
7
8namespace Field {
9
11ObjectChoropu::ObjectChoropu(const System::MapdataGeoObj &params)
12 : ObjectCollidable(params), StateManager(this) {
13 constexpr f32 MAX_SPEED = 20.0f;
14
15 m_startFrameOffset = static_cast<s16>(params.setting(1));
16 m_idleDuration = params.setting(0);
17 m_isStationary = strcmp(getName(), "choropu") != 0;
18
19 s16 railIdx = params.pathId();
20 if (railIdx != -1) {
21 auto *rail = RailManager::Instance()->rail(railIdx);
22 rail->checkSphereFull();
23 }
24
25 // If the mole moves around, then we need to create the dirt trail.
26 if (!m_isStationary) {
27 const auto &flowTable = ObjectDirector::Instance()->flowTable();
28 const auto *collisionSet =
29 flowTable.set(flowTable.slot(flowTable.getIdFromName("choropu_ground")));
30 ASSERT(collisionSet);
31
32 s16 height = parse<s16>(collisionSet->params.cylinder.height);
33 size_t groundCount = static_cast<size_t>(MAX_GROUND_LEN / EGG::Mathf::abs(height * 2)) + 1;
34 m_groundObjs = std::span<ObjectChoropuGround *>(new ObjectChoropuGround *[groundCount],
35 groundCount);
36
37 for (auto *&obj : m_groundObjs) {
38 obj = new ObjectChoropuGround(m_pos, m_rot, m_scale);
39 obj->load();
40 obj->resize(RADIUS, MAX_SPEED);
41 }
42
43 m_groundHeight = m_groundObjs.front()->height();
44 }
45
46 m_objHoll = new ObjectChoropuHoll(params);
47 m_objHoll->load();
48}
49
51ObjectChoropu::~ObjectChoropu() {
52 delete[] m_groundObjs.data();
53}
54
56void ObjectChoropu::init() {
57 if (m_isStationary) {
58 disableCollision();
59
60 m_scale.x = 1.0f;
61 m_objHoll->setScale(EGG::Vector3f(1.0f, m_objHoll->scale().y, 1.0f));
62 m_nextStateId = 0;
63 m_groundLength = 0.0f;
64
65 calcTransform();
66 m_transMat = m_transform;
67 } else {
68 if (m_mapObj->pathId() == -1) {
69 return;
70 }
71
72 m_railInterpolator->init(0.0f, 0);
73 m_railInterpolator->setPerPointVelocities(true);
74
75 m_railMat = RailOrthonormalBasis(*m_railInterpolator);
76 m_pos = m_railMat.base(3);
77 m_flags.setBit(eFlags::Position);
78
79 disableCollision();
80 m_objHoll->disableCollision();
81
82 m_nextStateId = 0;
83 }
84}
85
87void ObjectChoropu::calc() {
88 constexpr u32 START_DELAY = 300;
89
90 // Nothing to do if the mole hasn't spawned yet
91 u32 t = System::RaceManager::Instance()->timer();
92 if (t < m_startFrameOffset + START_DELAY) {
93 return;
94 }
95
96 if (!m_isStationary) {
97 if (m_mapObj->pathId() == -1) {
98 return;
99 }
100
101 m_railMat = RailOrthonormalBasis(*m_railInterpolator);
102 }
103
104 StateManager::calc();
105
106 m_objHoll->setScale(EGG::Vector3f(1.0f, m_objHoll->scale().y, 1.0f));
107}
108
110Kart::Reaction ObjectChoropu::onCollision(Kart::KartObject * /*kartObj*/,
111 Kart::Reaction reactionOnKart, Kart::Reaction /*reactionOnObj*/,
112 EGG::Vector3f & /*hitDepth*/) {
113 return m_currentStateId == 1 ? Kart::Reaction::SmallBump : reactionOnKart;
114}
115
116void ObjectChoropu::enterStateStub() {}
117
119void ObjectChoropu::enterDigging() {
120 if (m_isStationary) {
121 disableCollision();
122 } else {
123 for (auto *&obj : m_groundObjs) {
124 obj->enableCollision();
125 }
126
127 disableCollision();
128 m_objHoll->disableCollision();
129 m_groundLength = 0.0f;
130 }
131}
132
134void ObjectChoropu::enterPeeking() {
135 if (m_isStationary) {
136 m_pos = m_transMat.base(3);
137 m_flags.setBit(eFlags::Position, eFlags::Rotation);
138 m_rot.z = 0.0f;
139
140 enableCollision();
141 } else {
142 m_pos = m_railMat.base(3);
143 m_flags.setBit(eFlags::Position, eFlags::Rotation);
144 m_rot.z = 0.0f;
145 m_rot.y = 0.0f;
146
147 enableCollision();
148
149 const auto &curTanDir = m_railInterpolator->curTangentDir();
150 s16 curPointIdx = m_railInterpolator->curPointIdx();
151 EGG::Matrix34f mat;
152 SetRotTangentHorizontal(mat, m_railInterpolator->floorNrm(curPointIdx), curTanDir);
153 mat.setBase(3, m_railInterpolator->curPos());
154
155 m_objHoll->setTransform(mat);
156 m_objHoll->setPos(mat.base(3));
157 m_objHoll->enableCollision();
158 }
159}
160
162void ObjectChoropu::enterJumping() {
163 enableCollision();
164
165 m_pos = m_isStationary ? m_transMat.base(3) : m_railMat.base(3);
166 m_flags.setBit(eFlags::Position, eFlags::Rotation);
167 m_rot.z = 0.0f;
168}
169
170void ObjectChoropu::calcStateStub() {}
171
173void ObjectChoropu::calcDigging() {
174 if (m_isStationary) {
175 if (m_currentFrame > m_idleDuration) {
176 m_nextStateId = 1;
177 }
178
179 return;
180 }
181
182 m_pos = m_railInterpolator->curPos();
183 m_flags.setBit(eFlags::Position);
184
185 if (m_railInterpolator->calc() == RailInterpolator::Status::SegmentEnd) {
186 if (m_railInterpolator->curPoint().setting[1] == 1) {
187 m_nextStateId = 1;
188 } else {
189 calcGround();
190 }
191 } else {
192 bool skipGroundCalc = false;
193
194 if (m_railInterpolator->nextPoint().setting[1] == 1) {
195 f32 invT = 1.0f - m_railInterpolator->segmentT();
196 if (invT * m_railInterpolator->getCurrSegmentLength() < 250.0f) {
197 skipGroundCalc = true;
198 }
199 }
200
201 if (!skipGroundCalc) {
202 calcGround();
203 }
204 }
205}
206
208void ObjectChoropu::calcPeeking() {
209 constexpr s16 PEEK_DURATION = 40;
210 constexpr s16 STATE_DURATION = 100;
211
212 if (!m_isStationary) {
213 m_groundLength = std::max(0.0f, m_groundLength - m_railInterpolator->speed());
214
215 calcGroundObjs();
216 }
217
218 if (m_currentFrame > STATE_DURATION) {
219 m_nextStateId = 3;
220 }
221
222 if (m_currentFrame > PEEK_DURATION) {
223 disableCollision();
224 }
225}
226
228void ObjectChoropu::calcJumping() {
229 constexpr f32 JUMP_LINEAR_COEFFICIENT = 65.0f;
230 constexpr f32 JUMP_QUADRATIC_COEFFICIENT = 2.7f;
231
232 if (!m_isStationary) {
233 m_groundLength = std::max(0.0f, m_groundLength - m_railInterpolator->speed());
234 calcGroundObjs();
235 }
236
237 // y = -1.35t^2 + 65.0t
238 f32 pos = JUMP_LINEAR_COEFFICIENT * static_cast<f32>(m_currentFrame) -
239 static_cast<f32>(m_currentFrame) * 0.5f * JUMP_QUADRATIC_COEFFICIENT *
240 static_cast<f32>(m_currentFrame);
241
242 if (pos < 0.0f) {
243 m_nextStateId = 0;
244 }
245
246 m_pos.y = pos + (m_isStationary ? m_transMat.base(3).y : m_railInterpolator->curPos().y);
247 m_flags.setBit(eFlags::Position);
248}
249
251void ObjectChoropu::calcGround() {
252 m_groundLength += m_railInterpolator->getCurrVel();
253 if (m_groundLength > MAX_GROUND_LEN) {
254 m_groundLength = MAX_GROUND_LEN - 1.0f;
255 }
256
257 calcGroundObjs();
258}
259
261void ObjectChoropu::calcGroundObjs() {
262 size_t idx =
263 std::min(static_cast<size_t>(m_groundLength / m_groundHeight) + 1, m_groundObjs.size());
264
265 for (auto *&obj : m_groundObjs) {
266 obj->enableCollision();
267 }
268
269 for (size_t i = idx; i < m_groundObjs.size(); ++i) {
270 m_groundObjs[i]->disableCollision();
271 }
272
273 if (m_groundLength > RADIUS) {
274 f32 height = std::min(m_groundHeight, m_groundLength) - RADIUS;
275 m_groundObjs[0]->calcPosAndMat(height, calcInterpolatedPose(RADIUS + 0.5f * height));
276 }
277
278 for (size_t i = 1; i < idx - 1; ++i) {
279 f32 height = 0.5f * m_groundHeight + m_groundHeight * static_cast<f32>(i);
280 m_groundObjs[i]->calcPosAndMat(m_groundHeight, calcInterpolatedPose(height));
281 }
282
283 f32 height = m_groundLength - m_groundHeight * static_cast<f32>(idx - 1);
284 EGG::Matrix34f mat =
285 calcInterpolatedPose(0.5f * height + m_groundHeight * static_cast<f32>(idx - 1));
286 m_groundObjs[idx - 1]->calcPosAndMat(height, mat);
287}
288
290EGG::Matrix34f ObjectChoropu::calcInterpolatedPose(f32 t) const {
291 EGG::Vector3f curDir;
292 EGG::Vector3f curTanDir;
293 m_railInterpolator->evalCubicBezierOnPath(t, curDir, curTanDir);
294 EGG::Matrix34f mat = OrthonormalBasis(curTanDir);
295 mat.setBase(3, curDir);
296 return mat;
297}
298
300ObjectChoropuGround::ObjectChoropuGround(const EGG::Vector3f &pos, const EGG::Vector3f &rot,
301 const EGG::Vector3f &scale)
302 : ObjectCollidable("choropu_ground", pos, rot, scale) {
303 const auto &flowTable = ObjectDirector::Instance()->flowTable();
304 const auto *collisionSet =
305 flowTable.set(flowTable.slot(flowTable.getIdFromName("choropu_ground")));
306 ASSERT(collisionSet);
307
308 s16 height = parse<s16>(collisionSet->params.cylinder.height);
309 m_height = 2.0f * EGG::Mathf::abs(static_cast<f32>(height));
310}
311
313ObjectChoropuGround::~ObjectChoropuGround() = default;
314
316void ObjectChoropuGround::calcPosAndMat(f32 height, const EGG::Matrix34f &mat) {
317 EGG::Matrix34f matTemp;
318 SetRotTangentHorizontal(matTemp, mat.base(2), EGG::Vector3f::ey);
319 matTemp.setBase(1, matTemp.base(1) * (height / m_height));
320 matTemp.setBase(3, mat.base(3));
321 m_flags.setBit(eFlags::Matrix);
322 m_transform = matTemp;
323 m_pos = matTemp.base(3);
324}
325
327ObjectChoropuHoll::ObjectChoropuHoll(const System::MapdataGeoObj &params)
328 : ObjectCollidable(params) {}
329
331ObjectChoropuHoll::~ObjectChoropuHoll() = default;
332
333} // namespace Field
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:181
Vector3f base(size_t col) const
Get a particular column from a matrix.
Definition Matrix.hh:70
The highest level abstraction for a kart.
Definition KartObject.hh:11
Pertains to collision.
A 3D float vector.
Definition Vector.hh:88