persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
CameraSystem.cpp
Go to the documentation of this file.
1#include "CameraSystem.hpp"
2#include "core/globals.hpp"
3
5{
6 active = true;
7 mode = config.mode;
8 currentPos = config.eye;
9 targetPos = config.target;
10 angle = config.initialAngle;
12 distance = config.distance;
13 height = config.height;
14 lookAhead = config.lookAhead;
16}
17
19{
20 mode = msg.mode;
21 if (msg.mode == CameraMode::Path)
22 {
23 pathFrame = 0;
24 pathKeyIndex = 0;
25 pathDone = false;
26 }
27 if (msg.mode == CameraMode::Free)
28 {
29 freeInitialised = false;
30 }
31}
32
34{
35 path = msg.path;
36 pathFrame = 0;
37 pathKeyIndex = 0;
38 pathDone = false;
39}
40
45
47{
48 active = true;
49}
50
52{
53 active = false;
54}
55
57{
58 switch (mode)
59 {
62 return MathManager::getInstance().atan2(ae::q20_12_t{currentPos.x - charPos.x},
63 ae::q20_12_t{charPos.z - currentPos.z});
64 default:
65 return angle;
66 }
67}
68
70{
71 active = false;
72}
73
75{
76 active = false;
77}
78
79void CameraSystem::update(ae::q20_12_t)
80{
81 camPos.up.y = ae::q20_12_t{1};
82
83 switch (mode)
84 {
86 {
87 camPos.eye.x = currentPos.x;
88 camPos.eye.y = currentPos.y;
89 camPos.eye.z = currentPos.z;
90 camPos.target.x = targetPos.x;
91 camPos.target.y = targetPos.y;
92 camPos.target.z = targetPos.z;
93 break;
94 }
95
97 {
98 camPos.eye.x = currentPos.x;
99 camPos.eye.y = currentPos.y;
100 camPos.eye.z = currentPos.z;
101 camPos.target.x = charPos.x;
102 camPos.target.y = charPos.y;
103 camPos.target.z = charPos.z;
104 break;
105 }
106
108 {
109 if (!isRotationLocked && (systemKeysHeld & KEY_L))
110 {
112 }
113
114 if (!isRotationLocked && (systemKeysHeld & KEY_R))
115 {
117 }
118
119 camPos.eye.x = charPos.x + ae::q20_12_t{math.sin(angle)} * distance;
120 camPos.eye.y = charPos.y + height;
121 camPos.eye.z = charPos.z - ae::q20_12_t{math.cos(angle)} * distance;
122
123 camPos.target.x = charPos.x - ae::q20_12_t{math.sin(angle)} * lookAhead;
124 camPos.target.y = charPos.y + ae::q20_12_t{0.1};
125 camPos.target.z = charPos.z + ae::q20_12_t{math.cos(angle)} * lookAhead;
126 break;
127 }
128
129 case CameraMode::Free:
130 {
131 if (!freeInitialised)
132 {
133 currentPos.x = charPos.x;
134 currentPos.y = charPos.y + height;
135 currentPos.z = charPos.z;
136 freeInitialised = true;
137 }
138
139 if (!isRotationLocked && (systemKeysHeld & KEY_L))
140 {
142 }
143
144 if (!isRotationLocked && (systemKeysHeld & KEY_R))
145 {
147 }
148
149 const ae::q20_12_t fwdX = -ae::q20_12_t{math.sin(angle)} * freeCameraSpeed;
150 const ae::q20_12_t fwdZ = ae::q20_12_t{math.cos(angle)} * freeCameraSpeed;
151
152 if (systemKeysHeld & KEY_UP)
153 {
154 currentPos.x += fwdX;
155 currentPos.z += fwdZ;
156 }
157 if (systemKeysHeld & KEY_DOWN)
158 {
159 currentPos.x -= fwdX;
160 currentPos.z -= fwdZ;
161 }
162 if (systemKeysHeld & KEY_RIGHT)
163 {
164 currentPos.x -= fwdZ;
165 currentPos.z += fwdX;
166 }
167 if (systemKeysHeld & KEY_LEFT)
168 {
169 currentPos.x += fwdZ;
170 currentPos.z -= fwdX;
171 }
172
173 camPos.eye.x = currentPos.x;
174 camPos.eye.y = currentPos.y;
175 camPos.eye.z = currentPos.z;
176 camPos.target.x = currentPos.x - ae::q20_12_t{math.sin(angle)};
177 camPos.target.y = currentPos.y;
178 camPos.target.z = currentPos.z + ae::q20_12_t{math.cos(angle)};
179
180 break;
181 }
182
183 case CameraMode::Path:
184 {
185 if (!path || path->keyframes.size() < 2)
186 break;
187
188 pathFrame++;
189
190 while (pathKeyIndex + 2 < static_cast<int>(path->keyframes.size()) &&
191 pathFrame >= path->keyframes[pathKeyIndex + 1].time)
192 {
193 pathKeyIndex++;
194 }
195
196 const CameraKeyframe& kf0 = path->keyframes[pathKeyIndex];
197 const CameraKeyframe& kf1 = path->keyframes[pathKeyIndex + 1];
198
199 if (pathFrame >= kf1.time && pathKeyIndex + 2 >= static_cast<int>(path->keyframes.size()))
200 {
201 pathDone = true;
203 camPos.eye.x = kf1.eye.x;
204 camPos.eye.y = kf1.eye.y;
205 camPos.eye.z = kf1.eye.z;
206 camPos.target.x = kf1.target.x;
207 camPos.target.y = kf1.target.y;
208 camPos.target.z = kf1.target.z;
209 break;
210 }
211
212 int span = kf1.time - kf0.time;
213 ae::q20_12_t t =
214 (span > 0) ? math.div(ae::q20_12_t{pathFrame - kf0.time}, ae::q20_12_t{span}) : ae::q20_12_t{1};
215
216 camPos.eye.x = kf0.eye.x + (kf1.eye.x - kf0.eye.x) * t;
217 camPos.eye.y = kf0.eye.y + (kf1.eye.y - kf0.eye.y) * t;
218 camPos.eye.z = kf0.eye.z + (kf1.eye.z - kf0.eye.z) * t;
219 camPos.target.x = kf0.target.x + (kf1.target.x - kf0.target.x) * t;
220 camPos.target.y = kf0.target.y + (kf1.target.y - kf0.target.y) * t;
221 camPos.target.z = kf0.target.z + (kf1.target.z - kf0.target.z) * t;
222 break;
223 }
224
225 default:
226 break;
227 }
228
229 ae::broadcastEvent(camPos);
230}
Manages the camera for a 3D environment view.
ae::q20_12_t getMovementAngle() const
Returns the angle that maps the UP key to "move away from camera".
ae::q20_12_t angleIncrement
ae::q20_12_t lookAhead
CameraMode mode
void on_receive(const Event::ConfigureCamera &config)
ETL message handler to configure the camera settings.
ae::q20_12_t angle
const CameraPath * path
ae::q20_12_t height
Vec3< ae::q20_12_t > targetPos
void shutdown() override
MathManager & math
void init() override
ae::q20_12_t freeCameraSpeed
Vec3< ae::q20_12_t > currentPos
void update(ae::q20_12_t) override
Core update loop that advances camera state and broadcasts the resulting gluLookAt arguments.
Event::CameraPosition camPos
ae::q20_12_t distance
CharacterPosition charPos
Holds global variables used game-wide.
volatile u32 systemKeysHeld
Definition main.cpp:37
A single keyframe in a camera path.
Vec3< ae::q20_12_t > target
Look-at position.
int time
Frame index at which this keyframe is reached.
Vec3< ae::q20_12_t > eye
Camera eye position.
All parameters needed to configure a CameraSystem in one call.
Vec3< ae::q20_12_t > eye
ae::q20_12_t lookAhead
Distance ahead of the character for the look-at point.
ae::q20_12_t distance
Distance from character to camera eye.
ae::q20_12_t angleIncrement
Radians rotated per frame on L/R input.
ae::q20_12_t initialAngle
Starting orbit angle in radians.
bool isRotationLocked
Disable camera angle rotation on L/R input.
Vec3< ae::q20_12_t > target
Look-at point. Used by Static only.
ae::q20_12_t height
Eye height above the character origin.
Set the CameraMode.
Sets the pointer to a CameraPath.
Event payload to set the character position data.
Activates the CameraSystem update loop.
Deactivates the CameraSystem update loop.