persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
MovementComponent.cpp
Go to the documentation of this file.
2#include "core/globals.hpp"
3#include "core/logger.hpp"
4
5// TODO: remove hardcoded model references
6#include "models/makoto.hpp"
7
8// TODO: decouple camera with movement (so that movement can be used by other entities) (make a seperate component?)
9// TODO: decouple animation controller with movement (make a seperate component?)
10
12{
13 active = false;
14 walkAnim = (int)MODEL_MAKOTO_PLAYER_ROOT_MODEL_MOTION_0002;
15 idleAnim = (int)MODEL_MAKOTO_PLAYER_ROOT_MODEL_MOTION;
16}
17
19{
20 active = false;
21}
22
23void MovementComponent::update(ae::q20_12_t)
24{
25 ae::q20_12_t cameraAngle = CameraSystem::getInstance().getMovementAngle();
26 ae::q20_12_t forwardX;
27 ae::q20_12_t forwardZ;
28 ae::q20_12_t rightX;
29 ae::q20_12_t rightZ;
30
31 ae::q20_12_t deltaX{0};
32 ae::q20_12_t deltaZ{0};
33
34 ae::q20_12_t nextX;
35 ae::q20_12_t nextZ;
36
37 ae::q20_12_t angleRad;
38
39 const ae::q20_12_t sinVal = ae::q20_12_t{math.sin(cameraAngle)};
40 const ae::q20_12_t cosVal = ae::q20_12_t{math.cos(cameraAngle)};
41
42 forwardX = -sinVal * config.speed;
43 forwardZ = cosVal * config.speed;
44 rightX = cosVal * config.speed;
45 rightZ = sinVal * config.speed;
46
47 if (systemKeysHeld & KEY_UP)
48 {
49 deltaX += forwardX;
50 deltaZ += forwardZ;
51 }
52
53 if (systemKeysHeld & KEY_DOWN)
54 {
55 deltaX -= forwardX;
56 deltaZ -= forwardZ;
57 }
58
59 if (systemKeysHeld & KEY_RIGHT)
60 {
61 deltaX -= rightX;
62 deltaZ -= rightZ;
63 }
64
65 if (systemKeysHeld & KEY_LEFT)
66 {
67 deltaX += rightX;
68 deltaZ += rightZ;
69 }
70
71 if (deltaX != ae::q20_12_t{0} || deltaZ != ae::q20_12_t{0})
72 {
73 // set walking animation
74 if (Globals::enableCharacterAnim && (animationCtrl->getCurrentAnimIndex() != walkAnim))
75 {
76 animationCtrl->set(walkAnim, true);
77 }
78
79 // normalize diagonal movement to prevent faster speed
80 if (deltaX != ae::q20_12_t{0} && deltaZ != ae::q20_12_t{0})
81 {
82 const ae::q20_12_t invSqrt2{0.707106781187};
83 deltaX *= invSqrt2;
84 deltaZ *= invSqrt2;
85 }
86 }
87 else
88 {
89 // set idle animation
90 if (Globals::enableCharacterAnim && (animationCtrl->getCurrentAnimIndex() != idleAnim))
91 {
92 animationCtrl->set(idleAnim, true);
93 }
94 }
95 animationCtrl->play();
96
97 nextX = config.characterTranslate.x + deltaX;
98 nextZ = config.characterTranslate.z + deltaZ;
99
100 // try full movement first
101 if (isTileWalkable(nextX, nextZ))
102 {
103 config.characterTranslate.x = nextX;
104 config.characterTranslate.z = nextZ;
105 }
106 // if blocked, try X only (slide along Z wall)
107 else if (isTileWalkable(nextX, config.characterTranslate.z))
108 {
109 config.characterTranslate.x = nextX;
110 }
111 // if blocked, try Z only (slide along X wall)
112 else if (isTileWalkable(config.characterTranslate.x, nextZ))
113 {
114 config.characterTranslate.z = nextZ;
115 }
116
117 if (deltaX != ae::q20_12_t{0} || deltaZ != ae::q20_12_t{0})
118 {
119 angleRad = math.atan2(deltaX, deltaZ);
120 config.characterFacingAngle = angleRad * math.div(ae::q20_12_t{180}, ae::q20_12_t{3.14159265});
121 }
122 config.height = getGroundHeight(config.characterTranslate.x, config.characterTranslate.z);
123
124 ae::broadcastEvent(Event::SetCharacterPosition{isCharacterAt()});
125}
126
128{
129 this->config = config;
130}
131
133{
134 active = true;
135}
136
138{
139 active = false;
140}
141
143{
144 CharacterPosition charPos;
145
146 charPos.x = config.characterTranslate.x;
147 charPos.z = config.characterTranslate.z;
148 charPos.y = config.height;
149 charPos.facingAngle = config.characterFacingAngle;
150
151 return charPos;
152}
153
155{
156 MathManager& math = MathManager::getInstance();
157
158 int tileX = (int)(math.div(config.characterTranslate.x + config.worldOffsetX, config.tileSize));
159 int tileZ = (int)(math.div(config.characterTranslate.z + config.worldOffsetZ, config.tileSize));
160 return isTileAt(tileX, tileZ);
161}
162
164{
165 // default
166 if (config.collisionMap == nullptr || tileX < 0 || tileX >= config.mapWidth || tileZ < 0 ||
167 tileZ >= config.mapHeight)
168 {
170 }
171
172 // else use collision data
173 return (TileType)config.collisionMap[(tileZ * config.mapWidth) + tileX];
174}
175
176bool MovementComponent::isTileWalkable(ae::q20_12_t worldX, ae::q20_12_t worldZ)
177{
178 if (config.tileSize <= ae::q20_12_t{0})
179 {
180 logMessage(LogSeverity::WARNING, "Invalid tileSize");
181 return false;
182 }
183
184 const ae::q20_12_t radiusX = config.characterSize.x * ae::q20_12_t{0.5};
185 const ae::q20_12_t radiusZ = config.characterSize.z * ae::q20_12_t{0.5};
186 const ae::q20_12_t minX = worldX - radiusX + config.worldOffsetX;
187 const ae::q20_12_t maxX = worldX + radiusX + config.worldOffsetX;
188 const ae::q20_12_t minZ = worldZ - radiusZ + config.worldOffsetZ;
189 const ae::q20_12_t maxZ = worldZ + radiusZ + config.worldOffsetZ;
190 if (minX < ae::q20_12_t{0} || minZ < ae::q20_12_t{0} || maxX >= config.tileSize * ae::q20_12_t{config.mapWidth} ||
191 maxZ >= config.tileSize * ae::q20_12_t{config.mapHeight})
192 {
193 return false;
194 }
195 const int tileMinX = static_cast<int>(math.div(minX, config.tileSize));
196 const int tileMaxX = static_cast<int>(math.div(maxX, config.tileSize));
197 const int tileMinZ = static_cast<int>(math.div(minZ, config.tileSize));
198 const int tileMaxZ = static_cast<int>(math.div(maxZ, config.tileSize));
199 for (int z = tileMinZ; z <= tileMaxZ; z++)
200 {
201 for (int x = tileMinX; x <= tileMaxX; x++)
202 {
203 if (isTileAt(x, z) == TileType::COLLISION)
204 {
205 return false;
206 }
207 }
208 }
209 //until room has full 3d collision, refuse larger elevation
210 //discontinuities at the edges of stair/landing regions
211 //this is a guard, not a replacement for correctly joined regions
212 if (config.stairCount > 0 || config.landingCount > 0)
213 {
214 const ae::q20_12_t heightChange = getGroundHeight(worldX, worldZ) - config.height;
215 const ae::q20_12_t maxStep = ae::q20_12_t{0.08};
216 if (heightChange > maxStep || heightChange < -maxStep)
217 {
218 return false;
219 }
220 }
221
222 return true;
223}
224
225ae::q20_12_t MovementComponent::getGroundHeight(ae::q20_12_t x, ae::q20_12_t z) const
226{
227 for (int i = 0; i < config.stairCount; ++i)
228 {
229 const auto& s = config.stairs[i];
230
231 const ae::q20_12_t dx = s.x1 - s.x0;
232 const ae::q20_12_t dz = s.z1 - s.z0;
233 const ae::q20_12_t len2 = dx * dx + dz * dz;
234
235 if (len2 == ae::q20_12_t{0})
236 {
237 continue;
238 }
239
240 const ae::q20_12_t px = x - s.x0;
241 const ae::q20_12_t pz = z - s.z0;
242
243 //Projection onto the staircase direction.
244 const ae::q20_12_t dot = px * dx + pz * dz;
245
246 if (dot < ae::q20_12_t{0} || dot > len2)
247 {
248 continue;
249 }
250
251 //Perpendicular distance from the staircase.
252 const ae::q20_12_t cross = px * dz - pz * dx;
253
254 if (cross * cross > s.halfWidth * s.halfWidth * len2)
255 {
256 continue;
257 }
258
259 const ae::q20_12_t t = math.div(dot, len2);
260 return s.y0 + t * (s.y1 - s.y0);
261 }
262
263 for (int i = 0; i < config.landingCount; ++i)
264 {
265 const auto& f = config.landings[i];
266
267 if (x >= f.minX && x <= f.maxX && z >= f.minZ && z <= f.maxZ)
268 {
269 return f.height;
270 }
271 }
272
273 return config.baseHeight;
274}
Orchestrates the movement & view logic for a 3D environment view.
TileType
static bool enableCharacterAnim
Definition globals.hpp:44
void update(ae::q20_12_t) override
Core update loop that calculates the character & camera movement.
void start()
Start the update(ae::q20_12_t) loop by setting active to true.
TileType isTileAt()
Get the tile value at the current character position.
void configureMovement(const MovementConfig &config)
Configure the movement system.
void stop()
Stop the update(ae::q20_12_t) loop by setting active to false.
bool isTileWalkable(ae::q20_12_t worldX, ae::q20_12_t worldZ)
Checks for collision at the given position.
CharacterPosition isCharacterAt()
Get the current character position.
void destroy() override
ae::q20_12_t getGroundHeight(ae::q20_12_t x, ae::q20_12_t z) const
AnimationController * animationCtrl
Holds global variables used game-wide.
volatile u32 systemKeysHeld
Definition main.cpp:37
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Definition logger.cpp:110
Util to handle error logging.
ae::q20_12_t facingAngle
Event payload to set the character position data.
A struct that holds initial config values for the MovementComponent.