A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
ObjectPress.cc
1#include "ObjectPress.hh"
2
3#include "game/field/CollisionDirector.hh"
4#include "game/field/ObjectDirector.hh"
5
6#include "game/kart/KartObject.hh"
7
8namespace Field {
9
11ObjectPress::ObjectPress(const System::MapdataGeoObj &params)
12 : ObjectCollidable(params), m_loweringVelocity(0.0f) {}
13
15ObjectPress::~ObjectPress() = default;
16
18void ObjectPress::init() {
19 constexpr f32 FLOOR_CHECK_SPEED = 100.0f;
20 constexpr f32 HEIGHT = 20.0f;
21 constexpr EGG::Vector3f HITBOX_OFFSET = EGG::Vector3f(0.0f, HEIGHT, 0.0f);
22
23 m_state = State::Raised;
24 m_startingRise = false;
25 m_anmDuration = 0;
26 m_raisedTimer = static_cast<u32>(m_mapObj->setting(1));
27 m_windUpTimer = 0;
28 m_raisedHeight = m_pos.y;
29
30 bool hasCol = false;
31 CollisionInfo info;
32 auto *colDir = CollisionDirector::Instance();
33
34 // The game performs floor collision checks here to make sure that if a stomper is positioned
35 // above a sloped floor, it does not partially clip through the floor when it stomps down.
36 // If there is no floor below the stomper, then we get stuck in an infinite loop here.
37 while (!hasCol) {
38 m_flags.setBit(eFlags::Position);
39 m_pos.y -= FLOOR_CHECK_SPEED;
40
41 hasCol = colDir->checkSphereFull(HEIGHT, m_pos + HITBOX_OFFSET, EGG::Vector3f::inf,
42 KCL_TYPE_FLOOR, &info, nullptr, 0);
43 }
44
45 EGG::Vector3f loweredPos = m_pos + info.tangentOff;
46 m_pos.x = loweredPos.x;
47 m_pos.z = loweredPos.z;
48 m_loweredHeight = loweredPos.y;
49 m_flags.setBit(eFlags::Position);
50 m_pos.y = m_raisedHeight;
51 m_startedLowered = false;
52}
53
55void ObjectPress::calc() {
56 m_startedLowered = false;
57
58 switch (m_state) {
59 case State::Raised:
60 calcRaised();
61 break;
62 case State::WindUp:
63 calcWindUp();
64 break;
65 case State::Lowering:
66 calcLowering();
67 break;
68 case State::Lowered:
69 calcLowered();
70 break;
71 case State::Raising:
72 calcRaising();
73 break;
74 }
75}
76
78void ObjectPress::loadAnims() {
79 std::array<const char *, 1> names = {{
80 "Press",
81 }};
82
83 std::array<Render::AnmType, 1> types = {{
84 Render::AnmType::Chr,
85 }};
86
87 linkAnims(names, types);
88}
89
91void ObjectPress::createCollision() {
92 constexpr f32 POINT_SCALE = 10.0f;
93 constexpr std::array<EGG::Vector3f, 16> POINTS = {{
94 EGG::Vector3f(37.5f, 0.0f, 43.5f) * POINT_SCALE,
95 EGG::Vector3f(43.0f, 0.0f, 38.0f) * POINT_SCALE,
96 EGG::Vector3f(37.5f, 0.0f, -43.5f) * POINT_SCALE,
97 EGG::Vector3f(43.0f, 0.0f, 38.0f) * POINT_SCALE,
98 EGG::Vector3f(-37.5f, 0.0f, -43.5f) * POINT_SCALE,
99 EGG::Vector3f(-43.0f, 0.0f, -38.0f) * POINT_SCALE,
100 EGG::Vector3f(-37.5f, 0.0f, 43.5f) * POINT_SCALE,
101 EGG::Vector3f(-43.0f, 0.0f, 38.0f) * POINT_SCALE,
102 EGG::Vector3f(40.0f, 143.8f, 46.0f) * POINT_SCALE,
103 EGG::Vector3f(45.5f, 143.8f, 40.5f) * POINT_SCALE,
104 EGG::Vector3f(40.0f, 143.8f, -46.0f) * POINT_SCALE,
105 EGG::Vector3f(45.5f, 143.8f, -40.5f) * POINT_SCALE,
106 EGG::Vector3f(-40.0f, 143.8f, -46.0f) * POINT_SCALE,
107 EGG::Vector3f(-45.5f, 143.8f, -40.5f) * POINT_SCALE,
108 EGG::Vector3f(-40.0f, 143.8f, 46.0f) * POINT_SCALE,
109 EGG::Vector3f(-45.5f, 143.8f, 40.5f) * POINT_SCALE,
110 }};
111
112 m_collision = new ObjectCollisionConvexHull(POINTS);
113}
114
116Kart::Reaction ObjectPress::onCollision(Kart::KartObject *kartObj, Kart::Reaction reactionOnKart,
117 Kart::Reaction /*reactionOnObj*/, EGG::Vector3f & /*hitDepth*/) {
118 constexpr f32 CRUSH_THRESHOLD = 430.0f;
119
120 EGG::Vector3f diff = kartObj->pos() - m_pos;
121 bool close =
122 EGG::Mathf::abs(diff.x) < CRUSH_THRESHOLD && EGG::Mathf::abs(diff.z) < CRUSH_THRESHOLD;
123
124 // ObjectId::Press corresponds to a WallAllSpeed reaction, so when the stomper is coming down,
125 // the base game instead uses ObjectId::PressSoko to induce LongCrushLoseItem.
126 if (close && (m_state == State::Lowering || m_startedLowered)) {
127 const auto &hitTable = ObjectDirector::Instance()->hitTableKart();
128 return hitTable.reaction(hitTable.slot(ObjectId::PressSoko));
129 }
130
131 return reactionOnKart;
132}
133
135void ObjectPress::calcRaised() {
136 constexpr u32 WINDUP_FRAMES = 10;
137
138 if (--m_raisedTimer == 0) {
139 m_state = State::WindUp;
140 m_windUpTimer = WINDUP_FRAMES;
141 }
142}
143
144void ObjectPress::calcWindUp() {
145 constexpr f32 SPEED = 10.0f;
146
147 m_flags.setBit(eFlags::Position);
148 m_pos.y += SPEED;
149
150 if (--m_windUpTimer == 0) {
151 m_state = State::Lowering;
152 }
153}
154
156void ObjectPress::calcLowering() {
157 constexpr f32 ACCEL = 3.0f;
158
159 m_loweringVelocity -= ACCEL;
160 m_flags.setBit(eFlags::Position);
161 m_pos.y += m_loweringVelocity;
162
163 checkCollisionLowering();
164}
165
166void ObjectPress::calcLowered() {
167 if (--m_anmDuration > 0) {
168 return;
169 }
170
171 if (m_startingRise) {
172 m_state = State::Raising;
173 } else {
174 auto *anmMgr = m_drawMdl->anmMgr();
175 f32 frameCount = anmMgr->activeAnim(Render::AnmType::Chr)->frameCount();
176 anmMgr->playAnim(frameCount, -ANM_RATE, 0);
177
178 m_startingRise = true;
179 m_anmDuration = frameCount / ANM_RATE;
180 }
181}
182
183void ObjectPress::calcRaising() {
184 constexpr f32 SPEED = 10.0f;
185
186 f32 height = m_pos.y + SPEED;
187
188 if (height < m_raisedHeight) {
189 m_pos.y = height;
190 } else {
191 m_pos.y = m_raisedHeight;
192 m_state = State::Raised;
193 m_raisedTimer = static_cast<u32>(m_mapObj->setting(2));
194 }
195
196 m_flags.setBit(eFlags::Position);
197}
198
200void ObjectPress::checkCollisionLowering() {
201 constexpr f32 RADIUS = 10.0f;
202 constexpr EGG::Vector3f HITBOX_OFFSET = EGG::Vector3f(0.0f, RADIUS, 0.0f);
203
204 CollisionInfo info;
205
206 if (!CollisionDirector::Instance()->checkSphereFull(RADIUS, m_pos + HITBOX_OFFSET,
207 EGG::Vector3f::inf, KCL_TYPE_FLOOR, &info, nullptr, 0)) {
208 return;
209 }
210
211 m_loweringVelocity = 0.0f;
212 m_pos = m_pos + info.tangentOff;
213 m_flags.setBit(eFlags::Position);
214
215 auto *anmMgr = m_drawMdl->anmMgr();
216 anmMgr->playAnim(0.0f, ANM_RATE, 0);
217
218 m_state = State::Lowered;
219 m_startingRise = false;
220 m_anmDuration = anmMgr->activeAnim(Render::AnmType::Chr)->frameCount() / ANM_RATE;
221 m_startedLowered = true;
222}
223
225ObjectPressSenko::ObjectPressSenko(const System::MapdataGeoObj &params)
226 : ObjectPress(params), m_startingWindup(false) {}
227
229ObjectPressSenko::~ObjectPressSenko() = default;
230
232void ObjectPressSenko::calcRaised() {
233 if (m_startingWindup) {
234 startWindup();
235 m_startingWindup = false;
236 }
237}
238
240void ObjectPressSenko::startWindup() {
241 constexpr u32 WINDUP_DURATION = 10;
242
243 m_state = State::WindUp;
244 m_windUpTimer = WINDUP_DURATION;
245}
246
247} // namespace Field
#define KCL_TYPE_FLOOR
0x20E80FFF - Any KCL that the player or items can drive/land on.
The highest level abstraction for a kart.
Definition KartObject.hh:11
Pertains to collision.
A 3D float vector.
Definition Vector.hh:87