DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
bound.cpp
1#include "bound.hpp"
2
3#include "frame.hpp"
4#ifndef PORT
10static float vuabs(float *vector) {
11 asm {
12 lqc2 $vf4, 0x0($4)
13 vmul.xyz $vf4, $vf4, $vf4
14 vmr32.xy $vf5, $vf4
15 vmr32.x $vf6, $vf5
16 vadd.x $vf7, $vf4, $vf5
17 vadd.x $vf5, $vf6, $vf7
18 vsqrt $Q, $vf5x
19 vwaitq
20 cfc2.ni $2, $vi22
21 mtc1 $2, $f0
22 }
23}
24#endif
25int CBound::InCheck(float *point, float *result) {
26 float length;
27
28 result[0] = point[0] - position[0];
29 result[1] = point[1] - position[1];
30 result[2] = point[2] - position[2];
31 result[3] = 0.0f;
32 sceVu0ApplyMatrix(result, inverse, result);
33
34 if (result[0] > extent[0]) {
35 return 0;
36 }
37
38 if (result[1] > extent[1]) {
39 return 0;
40 }
41
42 if (result[2] > extent[2]) {
43 return 0;
44 }
45
46 if (result[0] < -extent[0]) {
47 return 0;
48 }
49
50 if (result[1] < -extent[1]) {
51 return 0;
52 }
53
54 if (result[2] < -extent[2]) {
55 return 0;
56 }
57
58 result[0] = result[0] * reciprocal[0];
59 result[1] = result[1] * reciprocal[1];
60 result[2] = result[2] * reciprocal[2];
61 length = vuabs(result);
62
63 if (length < 1.0f) {
64 if (length == 0.0f) {
65 result[0] = extent[0];
66 result[1] = extent[1];
67 result[2] = extent[2];
68 } else {
69 result[0] = result[0] * extent[0] / length;
70 result[1] = result[1] * extent[1] / length;
71 result[2] = result[2] * extent[2] / length;
72 }
73
74 result[3] = 0.0f;
75 sceVu0ApplyMatrix(result, matrix, result);
76 result[0] += position[0];
77 result[1] += position[1];
78 result[2] += position[2];
79 result[3] = 1.0f;
80 return 1;
81 }
82
83 return 0;
84}
85
86void CBound::SetDir(CFrame *frame, float *from_position, float *to_position, float *up_direction, float half_width, float half_height) {
87 follow_mode = BOUND_FOLLOW_FRAME;
88 from_frame = frame;
89 to_frame = NULL;
90 ChangeDir(from_position, to_position, up_direction);
91
92 // The depth is whatever the box already had; only the two across the
93 // span are given here.
94 float half_depth = extent[2];
95 extent[0] = half_width;
96 extent[1] = half_height;
97 extent[2] = half_depth;
98
99 if (!(extent[0] <= 0.0f)) {
100 reciprocal[0] = 1.0f / half_width;
101 }
102
103 if (!(extent[1] <= 0.0f)) {
104 reciprocal[1] = 1.0f / half_height;
105 }
106
107 if (!(extent[2] <= 0.0f)) {
108 reciprocal[2] = 1.0f / half_depth;
109 }
110}
111
112void CBound::ChangeDir(float *from_position, float *to_position, float *up_direction) {
113 sceVu0CopyVector(from, from_position);
114 sceVu0CopyVector(to, to_position);
115 sceVu0CopyVector(up, up_direction);
116}
117
119 sceVu0FVECTOR span;
120 sceVu0FVECTOR world_from;
121 sceVu0FVECTOR world_to;
122 sceVu0FVECTOR world_up;
123 sceVu0FMATRIX frame_matrix;
124
125 if (from_frame != NULL) {
126 from_frame->GetLWMatrix(frame_matrix);
127 sceVu0ApplyMatrix(world_from, frame_matrix, from);
128 sceVu0ApplyMatrix(world_to, frame_matrix, to);
129 up[3] = 0.0f;
130 sceVu0ApplyMatrix(world_up, frame_matrix, up);
131 sceVu0SubVector(span, world_from, world_to);
132 position[0] = (world_from[0] + world_to[0]) / 2.0f;
133 position[1] = (world_from[1] + world_to[1]) / 2.0f;
134 position[2] = (world_from[2] + world_to[2]) / 2.0f;
135 } else {
136 sceVu0CopyVector(world_up, up);
137 sceVu0SubVector(span, from, to);
138 position[0] = (from[0] + to[0]) / 2.0f;
139 position[1] = (from[1] + to[1]) / 2.0f;
140 position[2] = (from[2] + to[2]) / 2.0f;
141 }
142
143 SetDir(span, world_up);
144 float half_width;
145 float half_height;
146 float half_depth;
147
148 half_depth = extent[2];
149 half_height = extent[1];
150 half_width = extent[0];
151 extent[0] = half_width;
152 extent[1] = half_height;
153 extent[2] = half_depth;
154
155 if (!(extent[0] <= 0.0f)) {
156 reciprocal[0] = 1.0f / half_width;
157 }
158
159 if (!(extent[1] <= 0.0f)) {
160 reciprocal[1] = 1.0f / half_height;
161 }
162
163 if (!(extent[2] <= 0.0f)) {
164 reciprocal[2] = 1.0f / half_depth;
165 }
166}
167
168void CBound::SetDir(float *direction, float *up_direction) {
169 sceVu0FVECTOR origin = {0.0f, 0.0f, 0.0f, 1.0f};
170
171 sceVu0CameraMatrix(inverse, origin, direction, up_direction);
172 sceVu0TransposeMatrix(matrix, inverse);
173}
174
175void CBound::SetDir(float *new_direction) {
176 sceVu0Normalize(direction, new_direction);
177 float length = vuabs(direction);
178 float saved_y = direction[1];
179 direction[1] = 0.0f;
180 float horizontal_length = vuabs(direction);
181 direction[1] = saved_y;
182
183 float horizontal_sine = 0.0f;
184 float horizontal_cosine;
185
186 if (horizontal_length == 0.0f) {
187 horizontal_cosine = 1.0f;
188 } else {
189 horizontal_cosine = direction[2] / horizontal_length;
190 horizontal_sine = -direction[0] / horizontal_length;
191 }
192
193 if (length == 0.0f) {
194 length = 1.0f;
195 }
196
197 float horizontal_ratio = horizontal_length / length;
198 float vertical_ratio = direction[1] / length;
199
200 sceVu0UnitMatrix(inverse);
201 inverse[0][0] = horizontal_cosine;
202 inverse[0][1] = -horizontal_sine * vertical_ratio;
203 inverse[0][2] = horizontal_sine * horizontal_ratio;
204 inverse[0][3] = 0.0f;
205 inverse[1][0] = 0.0f;
206 inverse[1][1] = -horizontal_ratio;
207 inverse[1][2] = -vertical_ratio;
208 inverse[1][3] = 0.0f;
209 inverse[2][0] = horizontal_sine;
210 inverse[2][1] = horizontal_cosine * vertical_ratio;
211 inverse[2][2] = -horizontal_cosine * horizontal_ratio;
212 inverse[2][3] = 0.0f;
213 sceVu0TransposeMatrix(matrix, inverse);
214}
215
217 sceVu0FMATRIX frame_matrix;
218 sceVu0FVECTOR world_from;
219 sceVu0FVECTOR world_to;
220 sceVu0FVECTOR span;
221 sceVu0FVECTOR end_extension;
222 sceVu0FVECTOR start_extension;
223
224 if (from_frame != NULL) {
225 from[3] = 1.0f;
226 from_frame->GetLWMatrix(frame_matrix);
227 sceVu0ApplyMatrix(world_from, frame_matrix, from);
228 } else {
229 sceVu0CopyVector(world_from, from);
230 }
231
232 if (to_frame != NULL) {
233 to[3] = 1.0f;
234 to_frame->GetLWMatrix(frame_matrix);
235 sceVu0ApplyMatrix(world_to, frame_matrix, to);
236 } else {
237 sceVu0CopyVector(world_to, to);
238 }
239
240 position[0] = (world_from[0] + world_to[0]) * 0.5f;
241 position[1] = (world_from[1] + world_to[1]) * 0.5f;
242 position[2] = (world_from[2] + world_to[2]) * 0.5f;
243 sceVu0SubVector(span, world_to, world_from);
244 sceVu0ScaleVector(end_extension, span, (to_stretch - 1.0f) * 0.5f);
245 sceVu0ScaleVector(start_extension, span, (from_stretch - 1.0f) * 0.5f);
246 sceVu0AddVector(world_to, world_to, end_extension);
247 sceVu0SubVector(world_from, world_from, start_extension);
248 sceVu0SubVector(span, world_to, world_from);
249
250 float half_width;
251 float half_height;
252 float half_depth;
253
254 half_depth = extent[2];
255 half_height = extent[1];
256 half_width = extent[0];
257 extent[0] = half_width;
258 extent[1] = half_height;
259 extent[2] = half_depth;
260
261 if (!(extent[0] <= 0.0f)) {
262 reciprocal[0] = 1.0f / half_width;
263 }
264
265 if (!(extent[1] <= 0.0f)) {
266 reciprocal[1] = 1.0f / half_height;
267 }
268
269 if (!(extent[2] <= 0.0f)) {
270 reciprocal[2] = 1.0f / half_depth;
271 }
272
273 SetDir(span);
274}
275
277 switch (follow_mode) {
278 case BOUND_FOLLOW_FRAME:
279 UpDateDir();
280 break;
281 case BOUND_FOLLOW_SPAN:
282 UpDateDirPos();
283 break;
284 }
285
286 if (next) {
287 next->UpDate();
288 }
289}
290
292 position[0] = 0.0f;
293 position[1] = 0.0f;
294 position[2] = 0.0f;
295 position[3] = 1.0f;
296 next = 0;
297 mask_bit = 0;
298 friction = 0.5f;
299 from_frame = to_frame = 0;
300 sceVu0CopyVector(from, position);
301 sceVu0CopyVector(to, position);
302 to_stretch = from_stretch = 1.0f;
303}
304
305CBound::CBound(float half_width, float half_height, float half_depth) {
306 InitParam();
307 extent[0] = half_width;
308 extent[1] = half_height;
309 extent[2] = half_depth;
310
311 if (!(extent[0] <= 0.0f)) {
312 reciprocal[0] = 1.0f / half_width;
313 }
314
315 if (!(extent[1] <= 0.0f)) {
316 reciprocal[1] = 1.0f / half_height;
317 }
318
319 if (!(extent[2] <= 0.0f)) {
320 reciprocal[2] = 1.0f / half_depth;
321 }
322
323 sceVu0FVECTOR forward = {0.0f, 0.0f, 1.0f, 0.0f};
324 SetDir(forward);
325}
sceVu0FMATRIX inverse
Definition bound.hpp:37
sceVu0FVECTOR direction
Definition bound.hpp:39
CFrame * from_frame
Definition bound.hpp:41
void UpDateDir()
Definition bound.cpp:118
sceVu0FVECTOR to
Definition bound.hpp:45
CBound(float half_width, float half_height, float half_depth)
Definition bound.cpp:305
s32 follow_mode
Definition bound.hpp:40
void ChangeDir(float *from_position, float *to_position, float *up_direction)
Definition bound.cpp:112
void InitParam()
Definition bound.cpp:291
CBound * next
Definition bound.hpp:29
float to_stretch
Definition bound.hpp:47
void UpDateDirPos()
Definition bound.cpp:216
CFrame * to_frame
Definition bound.hpp:42
sceVu0FVECTOR extent
Definition bound.hpp:32
void UpDate()
Definition bound.cpp:276
sceVu0FVECTOR position
Definition bound.hpp:38
virtual int InCheck(float *point, float *result)
Definition bound.cpp:25
sceVu0FVECTOR from
Definition bound.hpp:44
float friction
Definition bound.hpp:34
void SetDir(CFrame *frame, float *from_position, float *to_position, float *up_direction, float half_width, float half_height)
Definition bound.cpp:86
float from_stretch
Definition bound.hpp:48
s32 mask_bit
Definition bound.hpp:30
sceVu0FVECTOR reciprocal
Definition bound.hpp:33
sceVu0FVECTOR up
Definition bound.hpp:46
sceVu0FMATRIX matrix
Definition bound.hpp:36