102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
ground.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
3#include "pcdogs/game/enums.h"
13#include "pcdogs/game/globals.h"
17#include "pcdogs/math/types.h"
21#include <math.h>
22#include <stdarg.h>
23#include <string.h>
24
25// Original pcdogs.exe: no direct counterpart (decomp helper)
26// Divides the signed high/low word pair by a signed divisor with x86 IDIV. Division by zero and quotient overflow retain their native traps.
27static inline int32_t Math_DivideSignedWordPair(int32_t high, uint32_t low, int32_t divisor)
28{
29 int32_t quotient, remainder;
30
31 __asm__("idivl %4" : "=a"(quotient), "=d"(remainder)
32 : "a"(low), "d"(high), "rm"(divisor) : "cc");
33
34 return quotient;
35}
36
37// Original pcdogs.exe: no direct counterpart (decomp helper)
38// Divides a signed value shifted left by 12 by the divisor with x86 IDIV. Division by zero and quotient overflow retain their native traps.
39static int32_t Math_DivideQ12WithOverflowTrap(int32_t value, int32_t divisor)
40{
41 /* IDIV also preserves the original divide-by-zero and quotient-overflow traps. */
42 int32_t quotient, remainder;
43
44 __asm__("idivl %4" : "=a"(quotient), "=d"(remainder)
45 : "a"((uint32_t)value << MATH_Q12_SHIFT), "d"(value >> 20), "rm"(divisor) : "cc");
46
47 return quotient;
48}
49
50// Original pcdogs.exe: no direct counterpart (decomp helper)
51// Reads an indexed collision vertex, applies the node's Q12 rotation and truncates each result to a signed 16-bit coordinate.
52static struct Math_Vec3I16 Collision_TransformIndexedVertexPosition(const struct Collision_Node *node, uint16_t index)
53{
54 struct Math_Vec3I16 p;
55
56 memcpy(&p, &node->vertices[index].pos, sizeof(p));
57
58 const struct Math_Matrix3x3I16 *m = &node->transform_matrix;
59 int32_t x = p.x, y = p.y, z = p.z;
60
61 return (struct Math_Vec3I16){
62 (int16_t)((x * m->m00 + y * m->m01 + z * m->m02) >> MATH_Q12_SHIFT),
63 (int16_t)((x * m->m10 + y * m->m11 + z * m->m12) >> MATH_Q12_SHIFT),
64 (int16_t)((x * m->m20 + y * m->m21 + z * m->m22) >> MATH_Q12_SHIFT)};
65}
66
67// Original pcdogs.exe: no direct counterpart (decomp helper)
68// Copies an indexed polygon vertex to three signed coordinates, applying the node's Q12 rotation when requested.
69static void Collision_ReadPolygonVertexPosition(const struct Collision_Node *node, uint16_t index, int transformed, int16_t out[3])
70{
71 const struct Collision_Vertex *vertex = &node->vertices[index];
72 int32_t x = vertex->pos.x, y = vertex->pos.y, z = vertex->pos.z;
73
74 if (transformed)
75 {
76 const struct Math_Matrix3x3I16 *m = (const void *)&node->transform_matrix;
77
78 out[0] = (int16_t)((m->m00 * x + m->m01 * y + m->m02 * z) >> MATH_Q12_SHIFT);
79 out[1] = (int16_t)((m->m10 * x + m->m11 * y + m->m12 * z) >> MATH_Q12_SHIFT);
80 out[2] = (int16_t)((m->m20 * x + m->m21 * y + m->m22 * z) >> MATH_Q12_SHIFT);
81 }
82 else
83 {
84 out[0] = x;
85 out[1] = y;
86 out[2] = z;
87 }
88}
89
90// Original pcdogs.exe: no direct counterpart (decomp helper)
91// Rotates a collision vertex by a Q12 matrix and truncates the three output coordinates to signed 16-bit values.
93 const struct Collision_Vertex *v)
94{
95 struct Collision_Vertex r;
96 int32_t x = v->pos.x, y = v->pos.y, z = v->pos.z;
97
98 r.pos.x = (int16_t)((x * m->m00 + y * m->m01 + z * m->m02) >> MATH_Q12_SHIFT);
99 r.pos.y = (int16_t)((x * m->m10 + y * m->m11 + z * m->m12) >> MATH_Q12_SHIFT);
100 r.pos.z = (int16_t)((x * m->m20 + y * m->m21 + z * m->m22) >> MATH_Q12_SHIFT);
101
102 return r;
103}
104
105// Original pcdogs.exe: 0x4251f0
106// Updates the actor's triangle selector and Q12 ground barycentric weights, then reconstructs its world position. The cached node and polygon must be valid, and degenerate determinants or overflowing quotients retain native division traps.
108{
109 const struct Collision_Node *node = actor->ground_collision_node;
110 const struct Collision_Polygon *poly = actor->ground_contact_polygon;
111 struct Math_Vec3I16 vertices[4];
112 int16_t px = (int16_t)((actor->position.x - node->origin.x) >> 6);
113 int16_t pz = (int16_t)((actor->position.z - node->origin.z) >> 6);
114 int triangle = poly->flags & COLLISION_POLYGON_TRIANGLE;
115
116 for (int i = 3 - triangle; i >= 0; --i)
117 vertices[i] = Collision_TransformIndexedVertexPosition(node, poly->vertex_idx[i]);
118
119 int16_t dx = (int16_t)(px - vertices[1].x), dz = (int16_t)(pz - vertices[1].z);
120 int16_t selector = triangle, vertex_z;
121
122 if (triangle)
123 {
124 vertices[3] = vertices[0];
125 vertex_z = vertices[2].z;
126 }
127 else
128 {
129 int32_t ex = (int16_t)(vertices[3].x - vertices[1].x);
130 int32_t ez = (int16_t)(vertices[3].z - vertices[1].z);
131 int32_t side = ((int32_t)vertices[0].x - vertices[1].x) * ez -
132 ((int32_t)vertices[0].z - vertices[1].z) * ex;
133
134 if ((side ^ (dx * ez - dz * ex)) >= 0)
135 {
136 vertices[2].x = vertices[0].x;
137 vertices[2].y = vertices[0].y;
138 vertex_z = vertices[0].z;
139 selector = 0;
140 }
141 else
142 {
143 vertex_z = vertices[2].z;
144 selector = 1;
145 }
146 }
147
148 int32_t a = (int16_t)(vertices[1].x - vertices[2].x);
149 int32_t b = (int16_t)(vertex_z - vertices[1].z);
150 int32_t determinant = ((int32_t)vertices[3].x - vertices[1].x) * b +
151 ((int32_t)vertices[3].z - vertices[1].z) * a;
152 int16_t *weights = (int16_t *)&actor->attach_offset;
153
154 weights[0] = selector;
155 weights[1] = (int16_t)Math_DivideQ12WithOverflowTrap(dx * b + dz * a, determinant);
156
157 int32_t numerator = (int16_t)(vertices[1].z - vertices[3].z) * (int32_t)dx +
158 (int16_t)(vertices[3].x - vertices[1].x) * (int32_t)dz;
159
160 weights[2] = (int16_t)Math_DivideQ12WithOverflowTrap(numerator, determinant);
161 weights[3] = (int16_t)(MATH_Q12_ONE - weights[1] - weights[2]);
163}
164
165// Original pcdogs.exe: 0x425490
166// Reconstructs a Q6 world position from the actor's signed Q12 ground weights and node transform. Selector zero uses vertices 3,0,1; a nonzero selector uses 3,2,1.
167void Collision_TransformGroundBarycentricToWorld(struct Actor_State const *actor_state, struct Math_Vec3I32 *out_world_pos)
168{
169 struct Collision_Node const *collision_node = actor_state->ground_collision_node;
170 struct Collision_Polygon const *contact_poly = actor_state->ground_contact_polygon;
171 const struct Actor_GroundBarycentricQ12 *barycentric_storage =
172 (const struct Actor_GroundBarycentricQ12 *)&actor_state->attach_offset;
173 struct Collision_Vertex const *node_vertices = collision_node->vertices;
174 int16_t weighted_local_x;
175 int16_t weighted_local_y;
176 int32_t weighted_z_first_term;
177 struct Collision_Vertex const *tri_vertex_a;
178 int32_t weighted_z_second_term;
179 int32_t weight_c_q12;
180
181 if (barycentric_storage->triangleSelector)
182 {
183 int32_t triangle_vertex_one_index = (uint16_t)(contact_poly->vertex_idx[1]);
184
185 tri_vertex_a = &node_vertices[triangle_vertex_one_index];
186 weight_c_q12 = (int32_t)barycentric_storage->weightC_q12;
187
188 int32_t triangle_vertex_two_index = (uint16_t)(contact_poly->vertex_idx[2]);
189 struct Collision_Vertex const *triangle_vertex_two = &node_vertices[triangle_vertex_two_index];
190 int32_t triangle_weight_two = (int32_t)barycentric_storage->weightB_q12;
191 int32_t triangle_vertex_three_index = (uint16_t)(contact_poly->vertex_idx[3]);
192 struct Collision_Vertex const *triangle_vertex_three = &node_vertices[triangle_vertex_three_index];
193 int32_t triangle_weight_three = (int32_t)barycentric_storage->weightA_q12;
194
195 weighted_z_first_term = (int32_t)triangle_vertex_two->pos.z * triangle_weight_two;
196 weighted_local_x = (int16_t)(((int32_t)triangle_vertex_three->pos.x * triangle_weight_three + (int32_t)tri_vertex_a->pos.x * weight_c_q12 + (int32_t)triangle_vertex_two->pos.x * triangle_weight_two) >> MATH_Q12_SHIFT);
197 weighted_local_y = (int16_t)(((int32_t)triangle_vertex_two->pos.y * triangle_weight_two + (int32_t)triangle_vertex_three->pos.y * triangle_weight_three + (int32_t)tri_vertex_a->pos.y * weight_c_q12) >> MATH_Q12_SHIFT);
198 weighted_z_second_term = (int32_t)triangle_vertex_three->pos.z * triangle_weight_three;
199 }
200 else
201 {
202 int32_t quad_vertex_one_index = (uint16_t)(contact_poly->vertex_idx[1]);
203
204 tri_vertex_a = &node_vertices[quad_vertex_one_index];
205 weight_c_q12 = (int32_t)barycentric_storage->weightC_q12;
206
207 int32_t quad_vertex_zero_index = (uint16_t)(contact_poly->vertex_idx[0]);
208 struct Collision_Vertex const *tri_vertex_b = &node_vertices[quad_vertex_zero_index];
209 int32_t quad_weight_zero = (int32_t)barycentric_storage->weightB_q12;
210 int32_t quad_vertex_three_index = (uint16_t)(contact_poly->vertex_idx[3]);
211 struct Collision_Vertex const *tri_vertex_c = &node_vertices[quad_vertex_three_index];
212 int32_t quad_weight_three = (int32_t)barycentric_storage->weightA_q12;
213
214 weighted_local_x = (int16_t)(((int32_t)tri_vertex_c->pos.x * quad_weight_three + (int32_t)tri_vertex_a->pos.x * weight_c_q12 + (int32_t)tri_vertex_b->pos.x * quad_weight_zero) >> MATH_Q12_SHIFT);
215 weighted_z_first_term = (int32_t)tri_vertex_c->pos.z * quad_weight_three;
216 weighted_local_y = (int16_t)(((int32_t)tri_vertex_c->pos.y * quad_weight_three + (int32_t)tri_vertex_b->pos.y * quad_weight_zero + (int32_t)tri_vertex_a->pos.y * weight_c_q12) >> MATH_Q12_SHIFT);
217 weighted_z_second_term = (int32_t)tri_vertex_b->pos.z * quad_weight_zero;
218 }
219
220 int32_t local_y = (int32_t)weighted_local_y;
221 int32_t local_x = (int32_t)weighted_local_x;
222 int32_t local_z = (int32_t)(int16_t)((weighted_z_first_term + weighted_z_second_term + (int32_t)tri_vertex_a->pos.z * weight_c_q12) >> MATH_Q12_SHIFT);
223 int32_t rotated_local_y = (int32_t)(int16_t)(((int32_t)collision_node->transform_matrix.m10 * local_x + (int32_t)collision_node->transform_matrix.m11 * local_y + (int32_t)collision_node->transform_matrix.m12 * local_z) >> MATH_Q12_SHIFT);
224 int32_t rotated_local_z_sum = (int32_t)collision_node->transform_matrix.m20 * local_x + (int32_t)collision_node->transform_matrix.m21 * local_y + (int32_t)collision_node->transform_matrix.m22 * local_z;
225
226 out_world_pos->x = ((int32_t)(int16_t)(((int32_t)collision_node->transform_matrix.m00 * local_x + (int32_t)collision_node->transform_matrix.m01 * local_y + (int32_t)collision_node->transform_matrix.m02 * local_z) >> MATH_Q12_SHIFT) << 6) + collision_node->origin.x;
227 out_world_pos->y = (rotated_local_y << 6) + collision_node->origin.y;
228 out_world_pos->z = ((int32_t)(int16_t)(rotated_local_z_sum >> MATH_Q12_SHIFT) << 6) + collision_node->origin.z;
229}
230
231// Original pcdogs.exe: 0x425690
232// Projects a world point onto a collision polygon while preserving world XZ, using plane evaluation or triangle interpolation as appropriate. A null node leaves the output untouched.
234 const struct Collision_Polygon *polygon, const struct Math_Vec3I32 *world_pos,
235 struct Math_Vec3I32 *out_world_pos)
236{
237 if (!node)
238 return;
239
240 out_world_pos->x = world_pos->x - node->origin.x;
241 out_world_pos->y = world_pos->y - node->origin.y;
242 out_world_pos->z = world_pos->z - node->origin.z;
243
244 int32_t nx = polygon->face_plane->normal.x;
245 int32_t ny = polygon->face_plane->normal.y;
246 int32_t nz = polygon->face_plane->normal.z;
248
249 if (transformed)
250 {
251 const struct Math_Matrix3x3I16 *m = &node->transform_matrix;
252 int32_t tx = (nx * m->m00 + ny * m->m01 + nz * m->m02) >> MATH_Q12_SHIFT;
253 int32_t ty = (nx * m->m10 + ny * m->m11 + nz * m->m12) >> MATH_Q12_SHIFT;
254 int32_t tz = (nx * m->m20 + ny * m->m21 + nz * m->m22) >> MATH_Q12_SHIFT;
255
256 nx = tx;
257 ny = ty;
258 nz = tz;
259 }
260
261 int32_t height;
262
264 {
265 int32_t sum = (polygon->face_plane->plane_distance >> 4) + (int32_t)(((int64_t)out_world_pos->x * nx) >> MATH_Q12_SHIFT) + (int32_t)(((int64_t)out_world_pos->z * nz) >> MATH_Q12_SHIFT);
266
267 height = (int32_t)(((int64_t)sum * MATH_Q12_ONE) / -ny);
268 }
269 else
270 {
271 const struct Collision_Vertex *v0 = &node->vertices[polygon->vertex_idx[0]];
272 const struct Collision_Vertex *v1 = &node->vertices[polygon->vertex_idx[1]];
273 const struct Collision_Vertex *v2 = &node->vertices[polygon->vertex_idx[2]];
274 const struct Collision_Vertex *v3 = &node->vertices[polygon->vertex_idx[3]];
275 int32_t side = ((out_world_pos->x >> 6) - v1->pos.x) * (v3->pos.z - v1->pos.z) - ((out_world_pos->z >> 6) - v1->pos.z) * (v3->pos.x - v1->pos.x);
276 const struct Collision_Vertex *a = side <= 0 ? v1 : v0;
277 const struct Collision_Vertex *b = side <= 0 ? v2 : v3;
278 const struct Collision_Vertex *c = side <= 0 ? v3 : v1;
279 struct Collision_Vertex local[3];
280
281 if (transformed)
282 {
283 local[0] = Collision_TransformVertex(&node->transform_matrix, a);
284 local[1] = Collision_TransformVertex(&node->transform_matrix, b);
285 local[2] = Collision_TransformVertex(&node->transform_matrix, c);
286 a = &local[0];
287 b = &local[1];
288 c = &local[2];
289 }
290
291 height = Math_CalculateTriangleHeight(out_world_pos->x, out_world_pos->z, a, b, c);
292 }
293
294 out_world_pos->x += node->origin.x;
295 out_world_pos->y = height + node->origin.y;
296 out_world_pos->z += node->origin.z;
297}
298
299// Original pcdogs.exe: 0x425de0
300// Decodes the neighboring polygon and reverse edge from packed adjacency. Zero adjacency writes edge -1 and returns null; other targets are returned only when their collision-active flag 0x04 is set.
301struct Collision_Polygon *Collision_GetAdjacentPolygon(struct Collision_Node const *collision_node, struct Collision_Polygon const *polygon, int32_t *edge_index_in_out)
302{
303 int32_t packed_adjacency = (uint16_t)(polygon->face_plane->adj_edges[*(uint32_t *)edge_index_in_out]);
304
305 if (!packed_adjacency)
306 {
307 *(uint32_t *)edge_index_in_out = 0xffffffff;
308
309 return 0;
310 }
311
312 struct Collision_Polygon *polygons = collision_node->polygons;
313 struct Collision_Polygon *adjacent_polygon = &polygons[(packed_adjacency >> 2) - 1];
314
315 *(uint32_t *)edge_index_in_out = packed_adjacency & COLLISION_EDGE_INDEX_MASK;
316
317 return (adjacent_polygon->flags_low & COLLISION_POLYGON_GROUND_ADJACENCY) ? adjacent_polygon : NULL;
318}
319
320// Original pcdogs.exe: 0x425e30
321// Follows the actor's cached ground polygon and adjacency to find a valid ground node, optionally writing the polygon. A null polygon output also enables exclusion of surfaces carrying flags 0x1800.
323{
324 struct Actor_State *actor_view = actor;
325 struct Collision_Node *ground_collision_node = actor_view->ground_collision_node;
326
327 if (!ground_collision_node || ground_collision_node->node_cull_flags & (COLLISION_NODE_CULL_DISABLED | COLLISION_NODE_CULL_FORCE_HIDDEN))
328 return 0;
329
330 int32_t local_x_fp6 = actor_view->position.x - ground_collision_node->origin.x;
331 struct Collision_Polygon *ground_contact_polygon = actor_view->ground_contact_polygon;
332 int32_t actor_local_y_q6 = actor_view->position.y - ground_collision_node->origin.y;
333 int32_t local_y_fp6 = actor_local_y_q6;
334 int32_t query_local_z_q6 = actor_view->position.z - ground_collision_node->origin.z;
335 int32_t local_z_fp6 = query_local_z_q6;
336
337 if (!(actor_view->behavior_flags_high & (ACTOR_BEHAVIOR_PREPROCESSED >> 8)))
338 {
339 if (!out_polygon && ground_contact_polygon->flags_high & ((COLLISION_POLYGON_GROUND_EXCLUSION_LOW | COLLISION_POLYGON_GROUND_EXCLUSION_HIGH) >> 8))
340 return 0;
341
342 local_x_fp6 += actor_view->sub_pos.x;
343 local_y_fp6 = actor_local_y_q6 + actor_view->sub_pos.y;
344 query_local_z_q6 += actor_view->sub_pos.z;
345 local_z_fp6 = query_local_z_q6;
346 }
347
348 int16_t point_z = (int16_t)(query_local_z_q6 >> 6);
349 int16_t point_x = (int16_t)(local_x_fp6 >> 6);
350
351 if (Collision_IsPointInsidePolygon(ground_collision_node, ground_contact_polygon, point_x,
352 point_z))
353 {
354 if (out_polygon)
355 *out_polygon = ground_contact_polygon;
356
357 return ground_collision_node;
358 }
359
360 struct Collision_Polygon *previous_polygon = ground_contact_polygon;
361 struct Math_Vec3I16 from_point;
362 struct Math_Vec3I16 to_point;
363 int32_t segment_end_y_q6;
364
365 if (out_polygon)
366 {
367 int32_t behavior_flags = actor_view->behavior_flags;
368
369 if ((behavior_flags & ACTOR_BEHAVIOR_PREPROCESSED))
370 {
371 behavior_flags |= ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
372 actor_view->behavior_flags = behavior_flags;
373 }
374
375 struct Collision_Vertex *vertices = ground_collision_node->vertices;
376 int32_t centroid[3] = {0, 0, 0};
377
378 for (int index = 0; index < 4; ++index)
379 {
380 const struct Collision_Vertex *vertex =
381 &vertices[ground_contact_polygon->vertex_idx[index]];
382
383 centroid[0] += vertex->pos.x;
384 centroid[1] += vertex->pos.y;
385 centroid[2] += vertex->pos.z;
386 }
387
388 from_point.x = (int16_t)(centroid[0] >> 2);
389 from_point.y = (int16_t)(centroid[1] >> 2);
390 from_point.z = (int16_t)(centroid[2] >> 2);
391
393 {
394 int32_t x = from_point.x, y = from_point.y, z = from_point.z;
395 const struct Math_Matrix3x3I16 *matrix =
396 (const void *)&ground_collision_node->transform_matrix;
397
398 from_point.x = (int16_t)((matrix->m00 * x + matrix->m01 * y + matrix->m02 * z) >> MATH_Q12_SHIFT);
399 from_point.y = (int16_t)((matrix->m10 * x + matrix->m11 * y + matrix->m12 * z) >> MATH_Q12_SHIFT);
400 from_point.z = (int16_t)((matrix->m20 * x + matrix->m21 * y + matrix->m22 * z) >> MATH_Q12_SHIFT);
401 }
402
403 actor_view = actor;
404 segment_end_y_q6 = local_y_fp6;
405 }
406 else
407 {
408 segment_end_y_q6 = local_y_fp6;
409 from_point.x = (int16_t)((local_x_fp6 - actor_view->sub_pos.x) >> 6);
410 from_point.y = (int16_t)((segment_end_y_q6 - actor_view->sub_pos.y) >> 6);
411 from_point.z = ((local_z_fp6 - actor_view->sub_pos.z) >> 6);
412 }
413
414 to_point.x = point_x;
415 to_point.y = (int16_t)(segment_end_y_q6 >> 6);
416 to_point.z = point_z;
417
418 int32_t initial_crossed_edge = Collision_FindIntersectingPolygonEdge(ground_collision_node,
419 ground_contact_polygon, &from_point, &to_point);
420 int32_t crossed_edge = initial_crossed_edge;
421
422 if (initial_crossed_edge < 0)
423 return 0;
424
425 struct Collision_Polygon *excluded_polygon;
426
427 while (true)
428 {
429 struct Collision_Polygon *polygon =
430 Collision_GetAdjacentPolygon(ground_collision_node, ground_contact_polygon, &crossed_edge);
431
432 if (out_polygon)
433 {
435 {
436 struct Pkg_CollisionFacePlane *face_plane;
437
438 if (polygon)
439 face_plane = polygon->face_plane;
440
441 if (!polygon || (((int32_t)face_plane->normal.z * (int32_t)ground_collision_node->transform_matrix.m12 + (int32_t)face_plane->normal.y * (int32_t)ground_collision_node->transform_matrix.m11 + (int32_t)polygon->face_plane->normal.x * (int32_t)ground_collision_node->transform_matrix.m10) & (int32_t)0xfffff000) <= COLLISION_GROUND_SLOPE_THRESHOLD_Q24)
442 {
444 {
445 actor_view->ground_contact_polygon = ground_contact_polygon;
447 }
448
449 int32_t airborne_flags = actor_view->behavior_flags;
450
451 airborne_flags |= ACTOR_BEHAVIOR_AIRBORNE;
452 actor_view->behavior_flags = airborne_flags;
453 }
454 }
455
456 if (!polygon)
457 {
459 return 0;
460
461 if (!(ground_collision_node->flags & COLLISION_NODE_SPECIAL_CONTACT))
462 return 0;
463
464 excluded_polygon = NULL;
465
466 break;
467 }
468 }
469 else
470 {
471 if (!polygon)
472 return 0;
473
475 return 0;
476 }
477
478 ground_contact_polygon = polygon;
479
480 int32_t next_crossed_edge = Collision_FindIntersectingPolygonEdge(ground_collision_node, polygon,
481 &from_point, &to_point);
482
483 crossed_edge = next_crossed_edge;
484
485 if (next_crossed_edge < 0)
486 {
487 excluded_polygon = previous_polygon;
488
489 break;
490 }
491 }
492
493 if (ground_contact_polygon == excluded_polygon || !(ground_contact_polygon->flags & COLLISION_POLYGON_GROUND_ADJACENCY))
494 return 0;
495
497 {
498 struct Pkg_CollisionFacePlane *contact_plane = ground_contact_polygon->face_plane;
499
500 if (!((((int32_t)contact_plane->normal.z * (int32_t)ground_collision_node->transform_matrix.m12 + (int32_t)contact_plane->normal.y * (int32_t)ground_collision_node->transform_matrix.m11 + (int32_t)contact_plane->normal.x * (int32_t)ground_collision_node->transform_matrix.m10) & (int32_t)0xfffff000) > 0))
501 return 0;
502 }
503
504 if (out_polygon)
505 *out_polygon = ground_contact_polygon;
506
507 return ground_collision_node;
508}
509
510// Original pcdogs.exe: 0x4261f0
511// Tests XZ containment in a triangle or quad, applying the node transform when required. Nonpositive upward normals are rejected, and edge endpoints and horizontal segments retain their explicit inclusion rules.
512int32_t Collision_IsPointInsidePolygon(struct Collision_Node const *collision_node, struct Collision_Polygon const *polygon, int16_t point_x, int16_t point_z)
513{
514 int transformed = collision_node->flags & (COLLISION_NODE_TRANSFORM_PRIMARY | COLLISION_NODE_TRANSFORM_SECONDARY);
515 const struct Pkg_CollisionFacePlane *plane = polygon->face_plane;
516
517 if (transformed)
518 {
519 int32_t normal_y = plane->normal.x * collision_node->transform_matrix.m10 + plane->normal.y * collision_node->transform_matrix.m11 + plane->normal.z * collision_node->transform_matrix.m12;
520
521 if ((int32_t)((uint32_t)normal_y & 0xfffff000u) <= 0)
522 return 0;
523 }
524 else if (plane->normal.y <= 0)
525 {
526 return 0;
527 }
528
529 int edge = !!((polygon->flags & COLLISION_POLYGON_TRIANGLE));
530 int16_t previous[3], current[3];
531
532 Collision_ReadPolygonVertexPosition(collision_node, polygon->vertex_idx[edge ? 0 : 3], transformed, previous);
533
534 int crossings = 0;
535
536 for (; edge < 4; ++edge)
537 {
538 Collision_ReadPolygonVertexPosition(collision_node, polygon->vertex_idx[edge], transformed, current);
539
540 int32_t previous_z = (int32_t)previous[2] - point_z;
541 int32_t current_z = (int32_t)current[2] - point_z;
542
543 if (previous_z == 0)
544 {
545 if (previous[0] == point_x)
546 return 1;
547
548 if (current_z == 0)
549 {
550 if (current[0] == point_x || (((int32_t)previous[0] - point_x) ^ ((int32_t)current[0] - point_x)) < 0)
551 return 1;
552 }
553 }
554
555 if ((previous_z ^ current_z) < 0)
556 {
557 int32_t dz = (int32_t)current[2] - previous[2];
558 int32_t left = ((int32_t)previous[0] - point_x) * dz;
559 int32_t right = ((int32_t)current[0] - previous[0]) * previous_z;
560
561 if (left == right || (dz ^ (left - right)) >= 0)
562 ++crossings;
563 }
564
565 memcpy(previous, current, sizeof previous);
566 }
567
568 return crossings & 1;
569}
570
571// Original pcdogs.exe: 0x4265a0
572// Finds the polygon edge crossed by an XZ movement segment, returning its index or -1. It preserves the signed normal thresholds, 16-bit coordinate truncation and wrapping intersection products.
574 const struct Collision_Polygon *poly, const struct Math_Vec3I16 *from,
575 const struct Math_Vec3I16 *to)
576{
577 const struct Math_Vec3I16 *normal = &poly->face_plane->normal;
578 int32_t ny = (normal->x * (int32_t)node->transform_matrix.m10 +
579 normal->y * (int32_t)node->transform_matrix.m11 +
580 normal->z * (int32_t)node->transform_matrix.m12) >>
581 12;
582
584 return -1;
585
586 int first = poly->flags & COLLISION_POLYGON_TRIANGLE;
587 uint16_t index = poly->vertex_idx[first ? 0 : 3];
589 int32_t dx = (int32_t)to->x - from->x, dz = (int32_t)to->z - from->z;
590
591 for (int i = first; i < 4; i++)
592 {
593 index = poly->vertex_idx[i];
594
596
597 if (((int32_t)current.x - from->x) * dz - ((int32_t)current.z - from->z) * dx > 0 &&
598 ((int32_t)old.x - from->x) * dz - ((int32_t)old.z - from->z) * dx <= 0)
599 {
600 int32_t ex = (int32_t)old.x - current.x, ez = (int32_t)old.z - current.z;
601
602 if (((int32_t)from->x - current.x) * ez - ((int32_t)from->z - current.z) * ex < 0 &&
603 ((int32_t)to->x - current.x) * ez - ((int32_t)to->z - current.z) * ex >= 0)
604 return (i - 1) & COLLISION_EDGE_INDEX_MASK;
605 }
606 old = current;
607 }
608
609 return -1;
610}
611
612// Original pcdogs.exe: 0x426870
613// Forwards the actor ground-contact query and its optional polygon output, returning the selected node.
615{
616 return Collision_FindGroundPolygonUnderActor(actor, out_polygon);
617}
618
619// Original pcdogs.exe: 0x426890
620// Resolves cached, dynamic and static ground contacts using a 0x555-Q6 height hysteresis band and a no-ground sentinel of -0x3ffff800. Cached type-2 ground is rescanned only with polygon flag 0x0800, and behavior flag 0x40 suppresses the final Y snap.
622{
623 struct Actor_State *moving_actor = actor;
624 int32_t ground_height;
625 struct Collision_Node *ground_node = moving_actor->ground_collision_node;
626 struct Math_Vec3I32 out_world_pos;
627
628 if (!ground_node)
629 {
630 moving_actor->ground_contact_polygon = NULL;
631 ground_height = COLLISION_NO_GROUND_HEIGHT_BITS;
632 Collision_DetectDynamicObject(moving_actor, &ground_height);
633 }
634 else
635 {
637 moving_actor->ground_contact_polygon, &moving_actor->position, &out_world_pos);
638 ground_height = out_world_pos.y;
639
641 Collision_DetectDynamicObject(moving_actor, &ground_height);
642 }
643
644 for (struct Collision_Node *runtime_vertices = (struct Collision_Node *)(g_objectNodeRoot->runtime_vertices);
645 runtime_vertices; runtime_vertices = runtime_vertices->next_in_list)
646 {
647 if (((char)runtime_vertices->flags & (COLLISION_NODE_SKIP_GROUND_QUERY | COLLISION_NODE_COLLIDABLE)) == COLLISION_NODE_COLLIDABLE && !(runtime_vertices->node_cull_flags & (COLLISION_NODE_CULL_DISABLED | COLLISION_NODE_CULL_FORCE_HIDDEN)))
648 {
649 int32_t bounds_delta_x =
650 moving_actor->position.x - (uint32_t)runtime_vertices->bounds.sphere.center.x;
651 int32_t bounds_query_y = moving_actor->position.y;
652
653 out_world_pos.x = bounds_delta_x;
654
655 int32_t z = moving_actor->position.z;
656
657 out_world_pos.y = bounds_query_y - (uint32_t)runtime_vertices->bounds.sphere.center.y;
658
659 int32_t half_bounds_delta_z = (int32_t)((uint32_t)z - (uint32_t)runtime_vertices->bounds.sphere.center.z) >> 1;
660
661 out_world_pos.x = bounds_delta_x >> 1;
662 out_world_pos.z = half_bounds_delta_z;
663
664 int64_t squared_half_x_wide = (int64_t)out_world_pos.x * out_world_pos.x;
665 int64_t squared_half_z_wide = (int64_t)out_world_pos.z * out_world_pos.z;
666 int32_t sphere = (int32_t)runtime_vertices->bounds.sphere.radius;
667
668 if (((uint32_t)((uint64_t)squared_half_z_wide >> MATH_Q12_SHIFT)) + ((uint32_t)((uint64_t)squared_half_x_wide >> MATH_Q12_SHIFT)) <= (uint32_t)(sphere * sphere) >> 2 && (-((out_world_pos.y >> 6)) <= sphere || !moving_actor->ground_collision_node))
669 {
670 int32_t polygon_count = (int32_t)runtime_vertices->polygon_count;
671 int32_t remaining_polygons = polygon_count;
672 int32_t polygon_test_x = (moving_actor->position.x - runtime_vertices->origin.x) >> 6;
673 int32_t saved_polygon_test_x = polygon_test_x;
674 int32_t polygon_test_z = (moving_actor->position.z - runtime_vertices->origin.z) >> 6;
675 int32_t saved_polygon_test_z = polygon_test_z;
676 struct Collision_Polygon *polygon_cursor = &runtime_vertices->polygons[polygon_count];
677
678 do
679 {
680 polygon_cursor -= 1;
681
682 if (Collision_IsPointInsidePolygon(runtime_vertices, polygon_cursor, (uint16_t)polygon_test_x,
683 (uint16_t)polygon_test_z))
684 {
685 Collision_ProjectPointOntoPolygonSurface(runtime_vertices, polygon_cursor,
686 &moving_actor->position, &out_world_pos);
687
688 int32_t y = runtime_vertices->collision_offset.y;
689 int32_t projected_surface_y = out_world_pos.y;
690 struct Collision_Node *ground_collision_node;
691
692 if (y > 0)
693 {
694 int32_t actor_world_y = moving_actor->position.y;
695
696 if (projected_surface_y > actor_world_y)
697 {
698 if (projected_surface_y - y < actor_world_y)
699 ground_collision_node = moving_actor->ground_collision_node;
700
701 if (projected_surface_y - y >= actor_world_y || runtime_vertices == ground_collision_node)
702 projected_surface_y = out_world_pos.y;
703 else
704 {
705 projected_surface_y = actor_world_y;
706 out_world_pos.y = projected_surface_y;
707
708 if (ground_collision_node != runtime_vertices)
709 {
710 int32_t behavior_flags = moving_actor->behavior_flags;
711
712 moving_actor->ground_collision_node = runtime_vertices;
713 behavior_flags |= ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
714 moving_actor->behavior_flags = behavior_flags;
715 }
716
717 if (moving_actor->ground_contact_polygon != polygon_cursor)
718 {
719 int32_t polygon_change_flags = moving_actor->behavior_flags;
720
721 moving_actor->ground_contact_polygon = polygon_cursor;
722 polygon_change_flags |= ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
723 moving_actor->behavior_flags = polygon_change_flags;
724 }
725
726 ground_height = projected_surface_y;
727
728 continue;
729 }
730 }
731 }
732
733 ground_collision_node = moving_actor->ground_collision_node;
734
735 if (ground_collision_node)
736 {
737 int32_t candidate_ground_y = ground_height;
738
739 if (projected_surface_y > candidate_ground_y)
740 {
741 if (!(projected_surface_y - COLLISION_GROUND_HEIGHT_TOLERANCE_Q6 <= moving_actor->position.y))
742 continue;
743 }
744 else if (!(candidate_ground_y - COLLISION_GROUND_HEIGHT_TOLERANCE_Q6 > moving_actor->position.y))
745 // Signed 0x555 Q6 hysteresis retains the original ground-selection band.
746 continue;
747 }
748
749 if (ground_collision_node != runtime_vertices)
750 {
751 int32_t behavior_flags = moving_actor->behavior_flags;
752
753 moving_actor->ground_collision_node = runtime_vertices;
754 behavior_flags |= ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
755 moving_actor->behavior_flags = behavior_flags;
756 }
757
758 if (moving_actor->ground_contact_polygon != polygon_cursor)
759 {
760 int32_t polygon_change_flags = moving_actor->behavior_flags;
761
762 moving_actor->ground_contact_polygon = polygon_cursor;
763 polygon_change_flags |= ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
764 moving_actor->behavior_flags = polygon_change_flags;
765 }
766
767 ground_height = projected_surface_y;
768 }
769 } while (--remaining_polygons &&
770 (polygon_test_x = saved_polygon_test_x, polygon_test_z = saved_polygon_test_z, 1));
771 }
772 }
773 }
774
775 if (!moving_actor->ground_contact_polygon)
776 {
777 moving_actor->ground_collision_node = NULL;
778
779 return;
780 }
781
782 if (!(moving_actor->behavior_flags & ACTOR_BEHAVIOR_AIRBORNE))
783 moving_actor->position.y = ground_height;
784}
785
786// Original pcdogs.exe: 0x4331d0
787// Builds temporary vertical wall quads around qualifying ground edges and passes them to the mesh collision resolver. The temporary node and face planes initialize only the fields consumed by that resolver path.
789{
790 struct Collision_FacePlane wall_face_planes[COLLISION_WALL_PLANE_CAPACITY];
791 struct Collision_FacePlane *next_wall_plane = wall_face_planes;
792 int32_t out_vertex_base = 0;
793 bool outside_mode_two = g_currentGameMode != GAME_MODE_COLLISION_CHECK;
794 struct Collision_Polygon *out_polygon_local = NULL;
796 struct Collision_Polygon *out_polygon = wall_polygons;
797 int32_t active_actor_cell_x;
798 int32_t active_actor_cell_z;
799 int32_t active_actor_cell_y;
800
801 if (!outside_mode_two)
802 {
803 struct Actor_State *active_actor = Entity_GetCurrentOrFirstActiveActor();
804
807 active_actor_cell_x = active_actor->position.x >> MATH_Q12_SHIFT;
808 g_collisionGroundCheckResult = active_actor_cell_x;
809 active_actor_cell_y = active_actor->position.y >> MATH_Q12_SHIFT;
810 g_collisionGroundNormalY = active_actor_cell_y;
811 active_actor_cell_z = active_actor->position.z >> MATH_Q12_SHIFT;
812 g_collisionGroundDist = active_actor_cell_z;
813 }
814
815 if (!outside_mode_two && active_actor_cell_x >= COLLISION_EXCLUDED_REGION_MIN_X && active_actor_cell_x <= COLLISION_EXCLUDED_REGION_MAX_X && active_actor_cell_z <= COLLISION_EXCLUDED_REGION_MAX_Z && active_actor_cell_z >= COLLISION_EXCLUDED_REGION_MIN_Z && active_actor_cell_y >= COLLISION_EXCLUDED_REGION_MIN_Y && active_actor_cell_y <= COLLISION_EXCLUDED_REGION_MAX_Y)
816 return;
817
818 struct Actor_State *moving_actor = actor;
819 struct Collision_Node *ground_node = Collision_FindActorGroundContact(moving_actor, &out_polygon_local);
820 struct Collision_Node *saved_ground_node = ground_node;
822
823 if (ground_node)
824 {
825 struct Collision_Polygon *contact_polygon = out_polygon_local;
826 uint16_t polygon_flags = contact_polygon->flags;
827
828 if (polygon_flags & COLLISION_POLYGON_EDGE_CONTACT)
829 {
830 int32_t edge_index = 3 - (polygon_flags & COLLISION_POLYGON_TRIANGLE);
831 int32_t saved_contact_edge = edge_index;
832 int32_t next_edge_vertex_index = edge_index + 1;
833 intptr_t edge_vertex_field_offset = edge_index * 2 + 4;
834 int32_t previous_edge_index;
835
836 do
837 {
838 struct Collision_Vertex *vertices = ground_node->vertices;
839 int32_t edge_start_vertex_index = (uint16_t)(contact_polygon->vertex_idx[(edge_vertex_field_offset - offsetof(struct Collision_Polygon, vertex_idx)) / sizeof(contact_polygon->vertex_idx[0])]);
840 int32_t edge_end_vertex_index;
841
842 if (!*(uint8_t *)((uint8_t *)&vertices[edge_start_vertex_index].reserved_0a + 1))
843 {
844 edge_end_vertex_index = (uint16_t)contact_polygon->vertex_idx[next_edge_vertex_index & COLLISION_EDGE_INDEX_MASK];
845 }
846
847 if (!*(uint8_t *)((uint8_t *)&vertices[edge_start_vertex_index].reserved_0a + 1) && !*(uint8_t *)((uint8_t *)&vertices[edge_end_vertex_index].reserved_0a + 1))
848 {
849 ground_node = saved_ground_node;
850 contact_polygon = out_polygon_local;
851 }
852 else
853 {
854 ground_node = saved_ground_node;
855
856 int32_t edge_index_in_out = edge_index;
857 struct Collision_Polygon *polygon;
858 uint16_t adjacent_polygon_flags;
859
860 polygon = Collision_GetAdjacentPolygon(ground_node, contact_polygon, &edge_index_in_out);
861
862 struct Collision_Polygon *source_polygon = polygon;
863
864 if (polygon)
865 {
866 adjacent_polygon_flags = polygon->flags;
867
868 if (!((adjacent_polygon_flags & COLLISION_POLYGON_EDGE_CONTACT)))
869 contact_polygon = out_polygon_local;
870 else
871 {
873 return;
874
875 int32_t adjacent_edge = 3 - (adjacent_polygon_flags & COLLISION_POLYGON_TRIANGLE);
876 int32_t saved_adjacent_edge = adjacent_edge;
877
878 while (true)
879 {
880 edge_index_in_out = adjacent_edge;
881
882 struct Collision_Polygon *neighbor_polygon_or_flags =
883 Collision_GetAdjacentPolygon(ground_node, polygon, &edge_index_in_out);
884 struct Collision_Polygon *second_neighbor_polygon = neighbor_polygon_or_flags;
885
886 if (second_neighbor_polygon)
887 {
888 if (second_neighbor_polygon != out_polygon_local)
889 {
890 uint16_t neighbor_flags = second_neighbor_polygon->flags;
891
892 if (neighbor_flags & COLLISION_POLYGON_EDGE_CONTACT)
893 {
894 int32_t second_neighbor_edge = 3 - (neighbor_flags & COLLISION_POLYGON_TRIANGLE);
895 int32_t j;
896
897 do
898 {
899 edge_index_in_out = second_neighbor_edge;
900
901 if (!Collision_GetAdjacentPolygon(saved_ground_node, second_neighbor_polygon,
902 &edge_index_in_out) &&
903 (!(second_neighbor_polygon->flags_high & (COLLISION_POLYGON_REVERSE_FACE >> 8)) || !(second_neighbor_polygon->plane_data->normal.x & COLLISION_PLANE_REVERSE_TEST)))
904 {
905 out_polygon->face_plane =
906 (struct Pkg_CollisionFacePlane *)next_wall_plane;
907
909 saved_ground_node, second_neighbor_polygon, second_neighbor_edge,
910 out_polygon, wall_vertices,
911 out_vertex_base))
912 {
913 out_vertex_base += 4;
914 ++out_polygon;
915 ++next_wall_plane;
916 }
917 }
918
919 j = second_neighbor_edge;
920 second_neighbor_edge -= 1;
921 } while (j);
922
923 adjacent_edge = saved_adjacent_edge;
924 }
925 }
926 }
927 else if (!(source_polygon->flags_high & (COLLISION_POLYGON_REVERSE_FACE >> 8)) || !(source_polygon->plane_data->normal.x & COLLISION_PLANE_REVERSE_TEST))
928 {
929 out_polygon->face_plane = (struct Pkg_CollisionFacePlane *)next_wall_plane;
930
931 if (Collision_BuildWallCollisionPlane(actor, saved_ground_node,
932 source_polygon, adjacent_edge, out_polygon, wall_vertices,
933 out_vertex_base))
934 {
935 out_vertex_base += 4;
936 ++out_polygon;
937 ++next_wall_plane;
938 }
939 }
940
941 int32_t previous_adjacent_edge = adjacent_edge;
942
943 adjacent_edge -= 1;
944 saved_adjacent_edge = adjacent_edge;
945
946 if (!previous_adjacent_edge)
947 break;
948
949 polygon = source_polygon;
950 ground_node = saved_ground_node;
951 }
952
953 edge_index = saved_contact_edge;
954 ground_node = saved_ground_node;
955 contact_polygon = out_polygon_local;
956 }
957 }
958 else
959 {
960 contact_polygon = out_polygon_local;
961
962 if (!(contact_polygon->flags_high & (COLLISION_POLYGON_REVERSE_FACE >> 8)) || !(contact_polygon->plane_data->normal.x & COLLISION_PLANE_REVERSE_TEST))
963 {
965 {
966 int32_t z = actor->sub_pos.z;
967 int32_t x = actor->sub_pos.x;
968
969 if (x * x != -((z * z)))
970 return;
971 }
972
973 struct Collision_Polygon *boundary_polygon = out_polygon_local;
974
975 out_polygon->face_plane = (struct Pkg_CollisionFacePlane *)next_wall_plane;
976
977 if (Collision_BuildWallCollisionPlane(actor, ground_node, boundary_polygon,
978 edge_index, out_polygon, wall_vertices, out_vertex_base))
979 {
980 out_vertex_base += 4;
981 ++out_polygon;
982 ++next_wall_plane;
983 }
984
985 contact_polygon = out_polygon_local;
986 }
987 }
988 }
989
990 previous_edge_index = edge_index;
991 edge_index -= 1;
992 edge_vertex_field_offset -= 2;
993 saved_contact_edge = edge_index;
994 next_edge_vertex_index -= 1;
995 } while (previous_edge_index);
996
997 moving_actor = actor;
998 }
999 }
1000
1001 int32_t wall_count = (int32_t)(out_polygon - wall_polygons);
1002
1003 if (!wall_count)
1004 return;
1005
1006 struct Collision_TemporaryNodeContext wall_collision_node;
1007
1008 wall_collision_node.transform_matrix.m00 = MATH_Q12_ONE;
1009 wall_collision_node.transform_matrix.m11 = MATH_Q12_ONE;
1010 wall_collision_node.transform_matrix.m22 = MATH_Q12_ONE;
1011 wall_collision_node.origin.x = ground_node->origin.x;
1012 wall_collision_node.origin.y = ground_node->origin.y;
1013 wall_collision_node.origin.y = moving_actor->position.y;
1014 wall_collision_node.origin.z = ground_node->origin.z;
1015 wall_collision_node.collision_offset.x = ground_node->collision_offset.x;
1016 wall_collision_node.collision_offset.y = ground_node->collision_offset.y;
1017 wall_collision_node.collision_offset.z = ground_node->collision_offset.z;
1018 wall_collision_node.transform_matrix.m01 = 0;
1019 wall_collision_node.transform_matrix.m02 = 0;
1020 wall_collision_node.transform_matrix.m10 = 0;
1021 wall_collision_node.transform_matrix.m12 = 0;
1022 wall_collision_node.transform_matrix.m20 = 0;
1023 wall_collision_node.transform_matrix.m21 = 0;
1024 wall_collision_node.node_type = 0;
1025 Collision_DetectAndResolve3DCollision(moving_actor, (struct Collision_Node *)(&wall_collision_node), wall_vertices,
1026 wall_polygons, wall_count);
1027}
1028
1029// Original pcdogs.exe: 0x4335d0
1030// Builds a vertical collision wall from a selected polygon edge and returns whether a quad was emitted. The output face plane's padding remains uninitialized.
1031uint8_t Collision_BuildWallCollisionPlane(struct Actor_State *actor, struct Collision_Node *collision_node, struct Collision_Polygon *source_polygon, int32_t edge_index, struct Collision_Polygon *out_polygon, struct Collision_Vertex *out_vertices, int32_t out_vertex_base)
1032{
1033 int32_t edge_start_index;
1034
1035 // WALL PLANE BUILDER: Extrudes polygon edge into vertical wall quad. Gets vertex indices from
1036 // polygon+edgeIndex*2+4. Flag 0x22 at level+0x24+4 enables rotation transform via 3x3 matrix at
1037 // level+0x24+8. Uses FastSquareRoot to normalize plane normal. Returns false if degenerate (same vertex).
1038 edge_start_index = source_polygon->vertex_idx[edge_index];
1039
1040 int32_t edge_end_index = (uint16_t)(source_polygon->vertex_idx[(edge_index + 1) & COLLISION_EDGE_INDEX_MASK]);
1041
1042 if (edge_start_index == edge_end_index)
1043 {
1044 return 0;
1045 }
1046
1047 struct Collision_Vertex *vertices = collision_node->vertices;
1048 int16_t *edge_start_position = (int16_t *)&vertices[edge_start_index];
1049 int16_t *edge_end_position = (int16_t *)&vertices[edge_end_index];
1050 int16_t *saved_edge_start_position = edge_start_position;
1051 struct Math_Vec3I16 transformed_a, transformed_b;
1052
1054 {
1055 transformed_a = Collision_TransformIndexedVertexPosition(collision_node, (uint16_t)edge_start_index);
1056 transformed_b = Collision_TransformIndexedVertexPosition(collision_node, (uint16_t)edge_end_index);
1057 saved_edge_start_position = edge_start_position = (int16_t *)&transformed_a;
1058 edge_end_position = (int16_t *)&transformed_b;
1059 }
1060
1061 int32_t wall_normal_y_unscaled = 0;
1062 int32_t edge_delta_x = (int32_t)*edge_start_position - (int32_t)*edge_end_position;
1063 int32_t wall_normal_x_unscaled = ((int32_t)edge_end_position[2] - (int32_t)edge_start_position[2]) << 6;
1064 int32_t wall_normal_z_unscaled = edge_delta_x << 6;
1065 int64_t normal_x_squared_wide = (int64_t)wall_normal_x_unscaled * wall_normal_x_unscaled;
1066 int64_t normal_y_squared_wide = (int64_t)wall_normal_y_unscaled * wall_normal_y_unscaled;
1067 int64_t normal_z_squared_wide = (int64_t)wall_normal_z_unscaled * wall_normal_z_unscaled;
1068 int32_t normal_length = Math_SqrtFP12((int32_t)(((uint64_t)normal_x_squared_wide + (uint64_t)normal_y_squared_wide + (uint64_t)normal_z_squared_wide) >> MATH_Q12_SHIFT));
1069 struct Collision_Polygon *contact_polygon_out;
1070
1071 if (normal_length)
1072 {
1073 int32_t inverse_normal_length = Math_DivideSignedWordPair(1, 0, normal_length);
1074 int64_t normalized_x_product_wide = (int64_t)inverse_normal_length * wall_normal_x_unscaled;
1075
1076 contact_polygon_out = out_polygon;
1077 contact_polygon_out->face_plane->normal.x = (int16_t)(((int32_t)((uint64_t)normalized_x_product_wide >> MATH_Q12_SHIFT)) >> 8);
1078
1079 int64_t normalized_y_product_wide = (int64_t)inverse_normal_length * wall_normal_y_unscaled;
1080
1081 contact_polygon_out->face_plane->normal.y = (int16_t)(((int32_t)((uint64_t)normalized_y_product_wide >> MATH_Q12_SHIFT)) >> 8);
1082
1083 int64_t normalized_z_product_wide = (int64_t)inverse_normal_length * wall_normal_z_unscaled;
1084
1085 contact_polygon_out->face_plane->normal.z = (((int32_t)((uint64_t)normalized_z_product_wide >> MATH_Q12_SHIFT)) >> 8);
1086 }
1087 else
1088 {
1089 contact_polygon_out = out_polygon;
1090 contact_polygon_out->face_plane->normal.z = 0;
1091 contact_polygon_out->face_plane->normal.y = 0;
1092 contact_polygon_out->face_plane->normal.x = 0;
1093 }
1094
1095 if (!wall_normal_x_unscaled && !wall_normal_y_unscaled && !wall_normal_z_unscaled)
1096 {
1097 return 0;
1098 }
1099
1100 int16_t origin_y = (int16_t)((collision_node->origin.y - actor->position.y) >> 6);
1101 struct Collision_Vertex *output = &out_vertices[out_vertex_base];
1102
1103 output[0].pos.x = edge_end_position[0];
1104 output[0].pos.y = edge_end_position[1] + origin_y;
1105 output[0].pos.z = edge_end_position[2];
1106 /* Native copies eight bytes, including the existing fourth word. */
1107 __builtin_memcpy(&output[3], &output[0], 8);
1108
1109 int32_t motion_y = actor->sub_pos.y >> 6;
1110
1111 if ((int16_t)motion_y < 0)
1112 motion_y = -motion_y;
1113
1114 int16_t half_height = motion_y + actor->cull_radius;
1115
1116 output[1].pos.x = saved_edge_start_position[0];
1117 output[1].pos.y = saved_edge_start_position[1] + origin_y;
1118 output[1].pos.z = saved_edge_start_position[2];
1119 __builtin_memcpy(&output[2], &output[1], 8);
1120
1121 int16_t bottom = (int16_t)((actor->world_render_pos.y - actor->position.y) >> 6) - half_height;
1122
1123 output[0].pos.y = output[1].pos.y = bottom;
1124 output[2].pos.y = output[3].pos.y = bottom + 2 * half_height;
1125
1126 for (unsigned j = 0; j < 4; ++j)
1127 contact_polygon_out->vertex_idx[j] = (uint16_t)(out_vertex_base + j);
1128
1129 struct Pkg_CollisionFacePlane *face_plane = contact_polygon_out->face_plane;
1130 const struct Math_Vec3I16 *first = (const struct Math_Vec3I16 *)&out_vertices[contact_polygon_out->vertex_idx[0]].pos;
1131 int32_t dot = (int32_t)first->x * face_plane->normal.x + (int32_t)first->y * face_plane->normal.y + (int32_t)first->z * face_plane->normal.z;
1132
1133 face_plane->plane_distance = -(dot >> 2);
1134 contact_polygon_out->flags = COLLISION_POLYGON_GROUND_ADJACENCY;
1135
1136 return 1;
1137}
1138
1139static int32_t occlusion_edge_distance(const struct Shadow_EdgePlane *edge, struct Math_Vec3I16 eye)
1140{
1141 uint32_t dot = (uint32_t)(edge->nx * eye.x) + (uint32_t)(edge->ny * eye.y) + (uint32_t)(edge->nz * eye.z);
1142 return (int32_t)dot >> MATH_Q12_SHIFT;
1143}
1144
1145// Original pcdogs.exe: 0x43f2e0
1146uint8_t Collision_CheckActorGround(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
1147{
1148 const struct Shadow_OccluderView *occluder = (const struct Shadow_OccluderView *)graphics_shadow_render_list;
1149 if (!occluder)
1150 return 0;
1151
1152 const struct Math_Vec3I32XZY *eye = &render_list_state->eye_pos;
1153 /* Retail pairs the coordinates in storage order with the XZY eye record. */
1154 struct Math_Vec3I16 eye_relative = {(position->x - eye->x) >> 8,
1155 (position->y - eye->z) >> 8,
1156 (position->z - eye->y) >> 8};
1157 int32_t radius = cull_radius >> 2;
1158 int32_t threshold = cull_flags & COLLISION_CULL_CENTER_TEST ? 0 : (int32_t)((uint32_t)radius << 10);
1159 for (; occluder; occluder = occluder->next_occluder)
1160 {
1161 if (occluder->exclusion_flags & cull_flags)
1162 continue;
1163
1164 const struct Collision_FacePlaneCore *base = (const struct Collision_FacePlaneCore *)occluder->source_polygon->face_normal_ptr;
1165 int16_t x = (position->x - occluder->position.x) >> 7;
1166 int16_t y = (position->y - occluder->position.y) >> 7;
1167 int16_t z = (position->z - occluder->position.z) >> 7;
1168 uint32_t dot = (uint32_t)(base->normal.x * x) + (uint32_t)(base->normal.y * y) + (uint32_t)(base->normal.z * z);
1169 int32_t projection = ((int32_t)dot >> 1) & ~1;
1170 int32_t distance = (int32_t)((uint32_t)projection + base->plane_distance) >> 2;
1171 if (distance <= threshold)
1172 continue;
1173
1174 const struct Shadow_EdgePlane *group = occluder->edge_planes;
1175 unsigned i = 0;
1176 while (i < 4 && occlusion_edge_distance(&group[i], eye_relative) >= radius)
1177 ++i;
1178 if (i != 4)
1179 continue;
1180
1181 /* All four initial planes must pass. Each following group needs one. */
1182 uint32_t remaining = (int32_t)occluder->plane_group_count - 1;
1183 while (remaining)
1184 {
1185 group += 4;
1186 i = 0;
1187 while (i < 4 && occlusion_edge_distance(&group[i], eye_relative) < radius)
1188 ++i;
1189 if (i == 4)
1190 break;
1191 --remaining;
1192 }
1193 if (!remaining)
1194 return 1;
1195 }
1196 return 0;
1197}
#define ACTOR_BEHAVIOR_AIRBORNE
Definition constants.h:13
#define ACTOR_BEHAVIOR_GROUND_TRACKING
Definition constants.h:12
#define ACTOR_BEHAVIOR_MOVEMENT_ACTIVE
Definition constants.h:19
#define ACTOR_BEHAVIOR_PREPROCESSED
Definition constants.h:20
#define COLLISION_POLYGON_STEEP_GROUND_REJECT
Definition constants.h:29
#define COLLISION_NODE_TRANSFORM_SECONDARY
Definition constants.h:59
#define COLLISION_EXCLUDED_REGION_MIN_Z
Definition constants.h:112
#define COLLISION_POLYGON_GROUND_EXCLUSION_LOW
Definition constants.h:32
#define COLLISION_NO_GROUND_HEIGHT_BITS
Definition constants.h:107
#define COLLISION_POLYGON_TRIANGLE
Definition constants.h:26
#define COLLISION_TRIANGLE_HEIGHT_MIN_NORMAL_Q12
Definition constants.h:105
#define COLLISION_POLYGON_GROUND_EXCLUSION_HIGH
Definition constants.h:33
#define COLLISION_NODE_CULL_DISABLED
Definition constants.h:60
#define COLLISION_GROUND_NORMAL_MIN_Q12
Definition constants.h:50
#define COLLISION_POLYGON_GROUND_ADJACENCY
Definition constants.h:28
#define COLLISION_EXCLUDED_REGION_MAX_X
Definition constants.h:111
#define COLLISION_POLYGON_REVERSE_FACE
Definition constants.h:31
#define COLLISION_NODE_SKIP_GROUND_QUERY
Definition constants.h:38
#define COLLISION_PLANE_REVERSE_TEST
Definition constants.h:63
#define COLLISION_GROUND_HEIGHT_TOLERANCE_Q6
Definition constants.h:55
#define COLLISION_EXCLUDED_REGION_MIN_X
Definition constants.h:110
#define COLLISION_NODE_COLLIDABLE
Definition constants.h:37
#define COLLISION_NODE_SPECIAL_CONTACT
Definition constants.h:39
#define COLLISION_NODE_TRANSFORM_PRIMARY
Definition constants.h:58
#define COLLISION_NODE_CULL_FORCE_HIDDEN
Definition constants.h:61
#define COLLISION_WALL_VERTEX_CAPACITY
Definition constants.h:109
#define COLLISION_WALL_PLANE_CAPACITY
Definition constants.h:108
#define COLLISION_CULL_CENTER_TEST
Definition constants.h:62
@ COLLISION_NODE_ORIENTED_BOX
Definition constants.h:8
#define COLLISION_EXCLUDED_REGION_MAX_Y
Definition constants.h:115
#define COLLISION_GROUND_SLOPE_THRESHOLD_Q24
Definition constants.h:106
#define COLLISION_POLYGON_EDGE_CONTACT
Definition constants.h:35
#define COLLISION_EDGE_INDEX_MASK
Definition constants.h:98
#define COLLISION_EXCLUDED_REGION_MIN_Y
Definition constants.h:114
void Collision_DetectDynamicObject(struct Actor_State *actor, int32_t *in_out_ground_y)
Definition objects.c:33
void Collision_DetectAndResolve3DCollision(struct Actor_State *actor, struct Collision_Node *collision_source, struct Collision_Vertex *vertices, struct Collision_Polygon *polygons, int32_t polygon_count)
#define g_collisionGroundCheckResult
Definition globals.h:24
#define g_collisionGroundNormalZ
Definition globals.h:66
#define g_collisionGroundDist
Definition globals.h:72
#define g_collisionGroundNormalX
Definition globals.h:63
int32_t global_type_g_collisionGroundNormalX
Definition globals.h:62
#define g_collisionGroundNormalY
Definition globals.h:57
int32_t global_type_g_collisionGroundNormalZ
Definition globals.h:65
static int32_t occlusion_edge_distance(const struct Shadow_EdgePlane *edge, struct Math_Vec3I16 eye)
Definition ground.c:1139
uint8_t Collision_BuildWallCollisionPlane(struct Actor_State *actor, struct Collision_Node *collision_node, struct Collision_Polygon *source_polygon, int32_t edge_index, struct Collision_Polygon *out_polygon, struct Collision_Vertex *out_vertices, int32_t out_vertex_base)
Definition ground.c:1031
static struct Math_Vec3I16 Collision_TransformIndexedVertexPosition(const struct Collision_Node *node, uint16_t index)
Definition ground.c:52
static void Collision_ReadPolygonVertexPosition(const struct Collision_Node *node, uint16_t index, int transformed, int16_t out[3])
Definition ground.c:69
struct Collision_Polygon * Collision_GetAdjacentPolygon(struct Collision_Node const *collision_node, struct Collision_Polygon const *polygon, int32_t *edge_index_in_out)
Definition ground.c:301
static struct Collision_Vertex Collision_TransformVertex(const struct Math_Matrix3x3I16 *m, const struct Collision_Vertex *v)
Definition ground.c:92
void Collision_UpdateActorGroundBarycentric(struct Actor_State *actor)
Definition ground.c:107
void Collision_TransformGroundBarycentricToWorld(struct Actor_State const *actor_state, struct Math_Vec3I32 *out_world_pos)
Definition ground.c:167
uint8_t Collision_CheckActorGround(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
Definition ground.c:1146
struct Collision_Node * Collision_FindGroundPolygonUnderActor(struct Actor_State *actor, struct Collision_Polygon **out_polygon)
Definition ground.c:322
int32_t Collision_FindIntersectingPolygonEdge(const struct Collision_Node *node, const struct Collision_Polygon *poly, const struct Math_Vec3I16 *from, const struct Math_Vec3I16 *to)
Definition ground.c:573
struct Collision_Node * Collision_FindActorGroundContact(struct Actor_State *actor, struct Collision_Polygon **out_polygon)
Definition ground.c:614
void Collision_ProjectPointOntoPolygonSurface(const struct Collision_Node *node, const struct Collision_Polygon *polygon, const struct Math_Vec3I32 *world_pos, struct Math_Vec3I32 *out_world_pos)
Definition ground.c:233
static int32_t Math_DivideQ12WithOverflowTrap(int32_t value, int32_t divisor)
Definition ground.c:39
static int32_t Math_DivideSignedWordPair(int32_t high, uint32_t low, int32_t divisor)
Definition ground.c:27
void Collision_BuildAndResolveGroundEdgeWalls(struct Actor_State *actor)
Definition ground.c:788
void Collision_ResolveActorGroundContact(struct Actor_State *actor)
Definition ground.c:621
int32_t Collision_IsPointInsidePolygon(struct Collision_Node const *collision_node, struct Collision_Polygon const *polygon, int16_t point_x, int16_t point_z)
Definition ground.c:512
struct Actor_State * Entity_GetCurrentOrFirstActiveActor(void)
Definition lifecycle.c:753
@ GAME_MODE_COLLISION_CHECK
Definition enums.h:10
@ GAME_MODE_GAMEPLAY_3D
Definition enums.h:38
void Player_CacheGroundJumpVelocity(struct Actor_State *actor)
Definition player.c:39
#define g_currentGameMode
Definition globals.h:74
#define render_list_state
Definition globals.h:152
#define graphics_shadow_render_list
Definition globals.h:287
#define MATH_Q12_SHIFT
Definition constants.h:8
#define MATH_Q12_ONE
Definition constants.h:9
int32_t Math_CalculateTriangleHeight(int32_t local_xq6, int32_t local_zq6, struct Collision_Vertex const *tri_a, struct Collision_Vertex const *tri_b, struct Collision_Vertex const *tri_c)
int32_t Math_SqrtFP12(uint32_t value)
#define g_objectNodeRoot
Definition globals.h:12
uint16_t triangleSelector
Definition types.h:83
int16_t cull_radius
Definition types.h:424
struct Collision_Polygon * ground_contact_polygon
Definition types.h:452
struct Collision_Node * ground_collision_node
Definition types.h:451
uint8_t behavior_flags_high
Definition types.h:412
int32_t behavior_flags
Definition types.h:407
struct Math_Vec3I32 world_render_pos
Definition types.h:420
struct Math_Vec3I32 sub_pos
Definition types.h:386
struct Math_Vec3I32 position
Definition types.h:385
struct Math_Vec3I32 attach_offset
Definition types.h:382
struct Math_Vec3I16 normal
Definition types.h:169
struct Collision_Vertex * vertices
Definition types.h:292
uint32_t flags
Definition types.h:297
uint8_t node_type
Definition types.h:286
struct Math_Vec3I32 collision_offset
Definition types.h:284
struct Collision_Polygon * polygons
Definition types.h:291
struct Math_Matrix3x3I16 transform_matrix
Definition types.h:281
uint8_t node_cull_flags
Definition types.h:287
struct Math_Vec3I32 origin
Definition types.h:283
struct Collision_Node * next_in_list
Definition types.h:276
struct Collision_Plane::@141200322312224221361126047031300120201020162362 normal
int32_t x
Definition types.h:42
uint16_t vertex_idx[0x4]
Definition types.h:61
struct Collision_Plane * plane_data
Definition types.h:60
uint16_t flags
Definition types.h:66
uint8_t flags_high
Definition types.h:71
struct Pkg_CollisionFacePlane * face_plane
Definition types.h:62
uint8_t flags_low
Definition types.h:70
struct Math_Vec3I32 origin
Definition types.h:225
struct Math_Matrix3x3I16 transform_matrix
Definition types.h:223
struct Math_Vec3I32 collision_offset
Definition types.h:226
int16_t reserved_0a
Definition types.h:123
int16_t z
Definition types.h:111
struct Collision_Vertex::@111126275124057154070202104324102117233165176236::@061322026074334072070347216127267241215241121036 pos
int16_t x
Definition types.h:109
int16_t y
Definition types.h:110
int16_t m21
Definition types.h:169
int16_t m00
Definition types.h:162
int16_t m01
Definition types.h:163
int16_t m12
Definition types.h:167
int16_t m11
Definition types.h:166
int16_t m20
Definition types.h:168
int16_t m22
Definition types.h:170
int16_t m02
Definition types.h:164
int16_t m10
Definition types.h:165
int16_t z
Definition types.h:460
int16_t x
Definition types.h:458
int16_t y
Definition types.h:459
int32_t x
Definition types.h:482
int32_t z
Definition types.h:483
int32_t y
Definition types.h:484
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472
uint32_t face_normal_ptr
Definition types.h:587
struct Math_Vec3I16 normal
Definition types.h:1135
uint16_t adj_edges[0x4]
Definition types.h:1137
int16_t ny
Definition types.h:311
int16_t nx
Definition types.h:310
int16_t nz
Definition types.h:312
struct Math_Vec3I32 position
Definition types.h:632
struct Shadow_EdgePlane * edge_planes
Definition types.h:638
uint32_t exclusion_flags
Definition types.h:640
struct Mesh_Polygon * source_polygon
Definition types.h:636
struct Shadow_OccluderView * next_occluder
Definition types.h:630
int16_t plane_group_count
Definition types.h:634