102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
geometry.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
10#include "pcdogs/math/types.h"
12#include <math.h>
13#include <stdarg.h>
14#include <string.h>
15
16// Original pcdogs.exe: no direct counterpart (shared by 0x434070 and 0x434770)
18{
19 if (!shape->extent_encoding_mode)
20 return shape->shape.sphere.radius_q12;
21
22 // The x86 comparisons zero-extend the candidate, then sign-extend the winner.
23 const struct Actor_CollisionBoxExtentView *box = &shape->shape.box;
24 int32_t minimum = box->extent_x_q6;
25 if ((uint16_t)box->extent_y_q6 < (uint32_t)minimum)
26 minimum = box->extent_y_q6;
27 if ((uint16_t)box->extent_z_q6 < (uint32_t)minimum)
28 minimum = box->extent_z_q6;
29 return minimum * 64;
30}
31
32// Original pcdogs.exe: no direct counterpart (decomp helper)
33// Computes a three-component dot product with signed products, modulo-2^64 accumulation and a 12-bit logical shift, returning the low 32 bits.
34static int32_t Math_DotProduct3Q12(int32_t ax, int32_t ay, int32_t az, int32_t bx, int32_t by, int32_t bz)
35{
36 uint64_t sum = (uint64_t)((int64_t)ax * bx) + (uint64_t)((int64_t)ay * by) + (uint64_t)((int64_t)az * bz);
37
38 return (int32_t)(uint32_t)(sum >> MATH_Q12_SHIFT);
39}
40
41// Original pcdogs.exe: 0x430d00
42// Initializes six collision-plane records, their vertex remaps and paired opposite Q12 axis normals.
44{
45 static const struct Math_Vec3I16 normals[] = {
46 {MATH_Q12_ONE, 0, 0}, {-MATH_Q12_ONE, 0, 0}, {0, MATH_Q12_ONE, 0},
47 {0, -MATH_Q12_ONE, 0}, {0, 0, MATH_Q12_ONE}, {0, 0, -MATH_Q12_ONE}};
48 for (unsigned face = 0; face < 6; ++face)
49 {
50 unsigned remapped = face < 4 ? face ^ 2 : face;
51 struct Collision_Polygon *polygon = &Collision_BoxFacePolygons[remapped];
53 for (unsigned corner = 0; corner < 4; ++corner)
54 polygon->vertex_idx[corner] = Collision_BoxFaceVertexRemap[face * 4 + corner];
55
56 struct Collision_FacePlane *plane = &Collision_BoxFacePlanes[face];
57 plane->normal = normals[face];
58 Collision_BoxFacePolygons[face].face_plane = (struct Pkg_CollisionFacePlane *)plane;
59 }
60}
61
62// Original pcdogs.exe: 0x433030
63// Tests sphere endpoints and projected distance against an actor's swept sphere. The projection is not restricted to the segment, so this is a broadphase candidate test rather than an exact segment intersection.
64uint8_t Collision_CheckLineSphereIntersection(struct Actor_State *actor, struct Math_Vec3I32 *sphere_center, struct Math_Vec3I32 *segment_delta, uint32_t sphere_radius)
65{
66 uint32_t radius = sphere_radius + (int32_t)actor->cull_radius;
67 uint32_t radius_squared = radius * radius;
68 int32_t x = sphere_center->x - actor->world_render_pos.x - actor->sub_pos.x;
69 int32_t y = sphere_center->y - actor->world_render_pos.y - actor->sub_pos.y;
70 int32_t z = sphere_center->z - actor->world_render_pos.z - actor->sub_pos.z;
71 uint32_t start_squared = (uint32_t)Math_DotProduct3Q12(x, y, z, x, y, z);
72
73 if (start_squared <= radius_squared)
74 return 1;
75
76 x -= segment_delta->x;
77 y -= segment_delta->y;
78 z -= segment_delta->z;
79
80 uint32_t end_squared = (uint32_t)Math_DotProduct3Q12(x, y, z, x, y, z);
81
82 if (end_squared <= radius_squared)
83 return 1;
84
85 uint32_t length_squared = (uint32_t)Math_DotProduct3Q12(segment_delta->x, segment_delta->y, segment_delta->z,
86 segment_delta->x, segment_delta->y, segment_delta->z);
87
88 if (!length_squared)
89 return 0;
90
91 uint32_t length_term = length_squared >> 4, end_term = end_squared >> 4;
92 int32_t difference = (start_squared >> 4) - length_term - end_term;
93 int32_t four_length = length_term << 2;
94 int32_t lhs = (int32_t)((int64_t)four_length * (int32_t)end_term >> MATH_Q12_SHIFT) - (int32_t)((int64_t)difference * difference >> MATH_Q12_SHIFT);
95 int32_t rhs = (int32_t)((int64_t)four_length * (int32_t)(radius_squared >> 4) >> MATH_Q12_SHIFT);
96
97 return lhs <= rhs;
98}
99
100// Original pcdogs.exe: 0x433ed0
101// Tests sphere endpoints and projected distance against the global collision probe. The projection is unbounded, and the distance arithmetic preserves unsigned wraparound followed by signed final comparisons.
102uint8_t Collision_TestLineSphereIntersection(struct Math_Vec3I32 *line_start, struct Math_Vec3I32 *segment_delta, uint32_t sphere_radius)
103{
104 uint32_t radius = sphere_radius + g_collisionTestRadiusQ6;
105 uint32_t radius_squared = radius * radius;
106 int32_t x = line_start->x - g_collisionTestPosX - g_collisionTestMotionXQ12;
107 int32_t y = line_start->y - g_collisionTestPosY - g_collisionTestMotionYQ12;
108 int32_t z = line_start->z - g_collisionTestPosZ - g_collisionTestMotionZQ12;
109 // Match the three IMUL products and ADD/ADC carries, then keep SHRD's
110 // low 32 bits. Unsigned accumulation also preserves 64-bit wraparound.
111 uint64_t squared = (uint64_t)((int64_t)x * x) + (uint64_t)((int64_t)y * y) + (uint64_t)((int64_t)z * z);
112 uint32_t start_squared = (uint32_t)(squared >> MATH_Q12_SHIFT);
113
114 if (start_squared <= radius_squared)
115 return 1;
116
117 x -= segment_delta->x;
118 y -= segment_delta->y;
119 z -= segment_delta->z;
120 squared = (uint64_t)((int64_t)x * x) + (uint64_t)((int64_t)y * y) + (uint64_t)((int64_t)z * z);
121
122 uint32_t end_squared = (uint32_t)(squared >> MATH_Q12_SHIFT);
123
124 if (end_squared <= radius_squared)
125 return 1;
126
127 x = segment_delta->x;
128 y = segment_delta->y;
129 z = segment_delta->z;
130 squared = (uint64_t)((int64_t)x * x) + (uint64_t)((int64_t)y * y) + (uint64_t)((int64_t)z * z);
131
132 uint32_t length_squared = (uint32_t)(squared >> MATH_Q12_SHIFT);
133
134 if (!length_squared)
135 return 0;
136
137 // SHR reduces unsigned distances; the final IMULs and SETLE are signed.
138 uint32_t four_length = (length_squared >> 4) << 2;
139 uint32_t end_scaled = end_squared >> 4;
140 int32_t difference = (start_squared >> 4) - (length_squared >> 4) - end_scaled;
141 int32_t product = (int32_t)(((int64_t)(int32_t)four_length * (int32_t)end_scaled) >> MATH_Q12_SHIFT);
142 int32_t difference_squared = (int32_t)(((int64_t)difference * difference) >> MATH_Q12_SHIFT);
143 int32_t limit = (int32_t)(((int64_t)(int32_t)four_length * (int32_t)(radius_squared >> 4)) >> MATH_Q12_SHIFT);
144
145 return (int32_t)((uint32_t)product - (uint32_t)difference_squared) <= limit;
146}
#define COLLISION_POLYGON_GROUND_ADJACENCY
Definition constants.h:28
void Collision_InitializePlaneLookupTables()
Definition geometry.c:43
uint8_t Collision_CheckLineSphereIntersection(struct Actor_State *actor, struct Math_Vec3I32 *sphere_center, struct Math_Vec3I32 *segment_delta, uint32_t sphere_radius)
Definition geometry.c:64
static int32_t Math_DotProduct3Q12(int32_t ax, int32_t ay, int32_t az, int32_t bx, int32_t by, int32_t bz)
Definition geometry.c:34
int32_t Collision_GetShapeRadiusQ12(const struct Actor_CollisionShapeNodeView *shape)
Definition geometry.c:17
uint8_t Collision_TestLineSphereIntersection(struct Math_Vec3I32 *line_start, struct Math_Vec3I32 *segment_delta, uint32_t sphere_radius)
Definition geometry.c:102
#define g_collisionTestRadiusQ6
Definition globals.h:45
#define Collision_BoxFacePolygons
Definition globals.h:93
#define g_collisionTestMotionXQ12
Definition globals.h:36
#define g_collisionTestPosZ
Definition globals.h:33
#define g_collisionTestPosX
Definition globals.h:27
#define g_collisionTestMotionZQ12
Definition globals.h:42
#define Collision_BoxFaceVertexRemap
Definition globals.h:99
#define g_collisionTestPosY
Definition globals.h:30
#define g_collisionTestMotionYQ12
Definition globals.h:39
#define Collision_BoxFacePlanes
Definition globals.h:96
#define MATH_Q12_SHIFT
Definition constants.h:8
#define MATH_Q12_ONE
Definition constants.h:9
union Actor_CollisionShapeExtentOverlay shape
Definition types.h:562
int16_t cull_radius
Definition types.h:424
struct Math_Vec3I32 world_render_pos
Definition types.h:420
struct Math_Vec3I32 sub_pos
Definition types.h:386
struct Math_Vec3I16 normal
Definition types.h:157
uint16_t vertex_idx[0x4]
Definition types.h:61
uint16_t flags
Definition types.h:66
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472
struct Actor_CollisionSphereExtentView sphere
Definition types.h:287
struct Actor_CollisionBoxExtentView box
Definition types.h:288