102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
objects.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
11#include "pcdogs/entity/types.h"
14#include "pcdogs/math/types.h"
18#include <math.h>
19#include <stdarg.h>
20#include <string.h>
21
22// Original pcdogs.exe: no direct counterpart (decomp helper)
23// Computes a three-component dot product with signed products, modulo-2^64 accumulation and a 12-bit logical shift, returning the low 32 bits.
24static int32_t Math_DotProduct3Q12(int32_t ax, int32_t ay, int32_t az, int32_t bx, int32_t by, int32_t bz)
25{
26 uint64_t sum = (uint64_t)((int64_t)ax * bx) + (uint64_t)((int64_t)ay * by) + (uint64_t)((int64_t)az * bz);
27
28 return (int32_t)(uint32_t)(sum >> MATH_Q12_SHIFT);
29}
30
31// Original pcdogs.exe: 0x426ae0
32// Scans dynamic collision nodes for ground beneath the actor, updating its ground node and polygon and the supplied ground height. Candidate selection uses the existing height hysteresis and polygon rescan flag.
33void Collision_DetectDynamicObject(struct Actor_State *actor, int32_t *in_out_ground_y)
34{
35 /* Root +0x78 holds the terminated node-pointer array. Node +0x00 is
36 a subtree skip count in this view, rather than a linked-list pointer. */
37 struct Collision_Node **cursor = g_objectNodeRoot->collision_node_list;
38 struct Collision_Node *node = *cursor++;
39
40 while (node)
41 {
43 {
44 cursor += (uint32_t)node->next_in_list;
45 node = *cursor++;
46
47 continue;
48 }
49
50 int32_t dy = actor->position.y - node->origin.y;
51 int32_t dx = (actor->position.x - node->origin.x) >> 1;
52 int32_t dz = (actor->position.z - node->origin.z) >> 1;
53 uint32_t distance = (uint32_t)((int64_t)dx * dx >> MATH_Q12_SHIFT) + (uint32_t)((int64_t)dz * dz >> MATH_Q12_SHIFT);
54 int32_t radius = node->collision_radius;
55
56 if (distance > ((uint32_t)(radius * radius) >> 2) ||
57 (-(dy >> 6) > radius && actor->ground_collision_node))
58 {
59 cursor += (uint32_t)node->next_in_list;
60 }
61 else if (node->polygon_count && !(node->flags & COLLISION_NODE_SKIP_POLYGON_QUERY))
62 {
63 struct Collision_Node *geometry = node->vertex_count > 0 ? node : (struct Collision_Node *)node->vertices;
64 int32_t x = (actor->position.x - geometry->origin.x) >> 6;
65 int32_t z = (actor->position.z - geometry->origin.z) >> 6;
66 int32_t remaining = node->polygon_count;
67 struct Collision_Polygon *polygon = node->polygons + remaining;
68
69 do
70 {
71 --polygon;
72
73 if (Collision_IsPointInsidePolygon(geometry, polygon, (uint16_t)x, (uint16_t)z))
74 {
75 struct Math_Vec3I32 projected;
76
77 Collision_ProjectPointOntoPolygonSurface(geometry, polygon, &actor->position, &projected);
78
79 int32_t y = projected.y;
80
81 if (!actor->ground_collision_node ||
82 (y > *in_out_ground_y ? y - COLLISION_GROUND_HEIGHT_TOLERANCE_Q6 <= actor->position.y : *in_out_ground_y - COLLISION_GROUND_HEIGHT_TOLERANCE_Q6 > actor->position.y))
83 {
84 if (actor->ground_collision_node != geometry)
85 {
86 actor->ground_collision_node = geometry;
88 }
89
90 if (actor->ground_contact_polygon != polygon)
91 {
92 actor->ground_contact_polygon = polygon;
94 }
95
96 *in_out_ground_y = y;
97
99 return;
100 }
101 }
102 } while (--remaining);
103 }
104
105 node = *cursor++;
106 }
107}
108
109// Original pcdogs.exe: 0x433d60
110// Scans the selected object list for swept actor contacts, applying class callbacks and sphere tests before resolution. List slot 2 can remap to slot 1 and also trigger a slot-4 scan.
111void Collision_DetectObjectNodeCollisions(struct Actor_State *actor, int32_t object_list_index)
112{
113 int32_t selected_object_list = object_list_index;
114
115 if (selected_object_list == 2)
116 {
118 {
119 if (!(pkg_resource_current_level_data->entity_array[(int32_t)(uint16_t)((const struct Actor_MovementRecordView *)actor->record_ptr)->entity_index].flags & ENTITY_FLAG_ACTOR_COLLISION_SCAN))
120 return;
121 }
122 else
123 selected_object_list = 1;
124
126 }
127
128 for (struct Actor_State *node_actor =
129 g_objectNodeRoot->animation_actor_lists[selected_object_list];
130 node_actor;
131 node_actor = node_actor->list_next)
132 {
133 if (!(node_actor->lifecycle_flags & (ACTOR_LIFECYCLE_DISABLED | ACTOR_LIFECYCLE_FORCE_HIDDEN)) && !(node_actor->attach_slot_index & COLLISION_ATTACHMENT_SKIP_ACTOR_SCAN))
134 {
135 int32_t dy = g_collisionTestMotionYQ12 - node_actor->sub_pos.y;
136 int32_t dx = g_collisionTestMotionXQ12 - node_actor->sub_pos.x;
137 int32_t dz = g_collisionTestMotionZQ12 - node_actor->sub_pos.z;
138 struct Math_Vec3I32 segment_delta = (struct Math_Vec3I32){
139 .x = dx,
140 .y = dy,
141 .z = dz};
142
143 int32_t swept_bounding_radius = Math_ApproxDistance3DWeighted(dx, dy, dz) + (((int32_t)((const struct Actor_CollisionBoxExtentView *)&node_actor->visual_scale)->collision_probe_radius_q6 + g_collisionTestRadiusQ6) << 6);
144 int32_t swept_bounding_diameter = swept_bounding_radius * 2;
145
146 if ((uint32_t)(g_collisionTestPosX - (node_actor->position.x - node_actor->sub_pos.x) + swept_bounding_radius) <= (uint32_t)swept_bounding_diameter && (uint32_t)(g_collisionTestPosY - (node_actor->position.y - node_actor->sub_pos.y) + swept_bounding_radius) <= (uint32_t)swept_bounding_diameter && (uint32_t)(g_collisionTestPosZ - (node_actor->position.z - node_actor->sub_pos.z) + swept_bounding_radius) <= (uint32_t)swept_bounding_diameter && Actor_DefaultUpdateCallbackSlot[(int32_t)(int8_t)actor->collision_class](actor, node_actor, 0, (uint32_t)COLLISION_CALLBACK_DAMAGE_PROBE) && Collision_TestLineSphereIntersection(&node_actor->position, &segment_delta, (int32_t)((const struct Actor_CollisionBoxExtentView *)&node_actor->visual_scale)->collision_probe_radius_q6))
147 Collision_ResolveObjectNodeCollision(actor, node_actor);
148 }
149 }
150}
151
152// Original pcdogs.exe: 0x434070
153// Resolves an actor against an object-node collision shape. Static overlap calls the class callback directly, while separated axes use six temporary planes for swept resolution.
154void Collision_ResolveObjectNodeCollision(struct Actor_State *actor, struct Actor_State *other_actor)
155{
156 struct Actor_CollisionShapeNodeView *actor_collision_shape_node =
158 int32_t collision_radius_q12 = Collision_GetShapeRadiusQ12(actor_collision_shape_node);
159
160 int32_t relative_z_before_motion = g_collisionTestPosZ - other_actor->position.z;
161 int32_t relative_y = g_collisionTestPosY - other_actor->position.y + other_actor->sub_pos.y;
162 int32_t relative_x = g_collisionTestPosX - other_actor->position.x + other_actor->sub_pos.x;
163 int16_t *rotation_column = &other_actor->rot_mat.m10;
164 int32_t relative_z = relative_z_before_motion + other_actor->sub_pos.z;
165 int32_t axis_index = 0;
166 int32_t m00;
167 int16_t *next_rotation_column = rotation_column;
168
169 while (true)
170 {
171 int32_t m10 = (int32_t)rotation_column[0];
172 int32_t axis_half_extent;
173
174 m00 = (int32_t)rotation_column[-3];
175 axis_half_extent = (uint16_t)((const struct Actor_CollisionBoxExtentView *)&other_actor->visual_scale)->half_extents_q6[axis_index];
176
177 int32_t m20 = (int32_t)rotation_column[3];
178 int32_t expanded_extent_or_corner_z = (axis_half_extent << 6) + collision_radius_q12;
179
180 if ((uint32_t)Math_DotProduct3Q12(relative_x, relative_y, relative_z, m00, m10, m20) + (uint32_t)expanded_extent_or_corner_z > (uint32_t)expanded_extent_or_corner_z * 2u)
181 {
182 struct Collision_Vertex vertices[8];
183 int32_t packed_corner_xy = 0;
184
185 for (int32_t i = 0; i < 4;)
186 {
187 switch (i)
188 {
189 case 0:
190 {
191 (*(uint16_t *)&packed_corner_xy) = -(other_actor->visual_scale.x);
192 *(uint16_t *)((char *)&packed_corner_xy + 2) = -(other_actor->visual_scale.y);
193 (*(uint16_t *)&expanded_extent_or_corner_z) = -(int16_t)(uintptr_t)other_actor->mesh_polygon_array;
194
195 break;
196 }
197
198 case 1:
199 {
200 (*(uint16_t *)&expanded_extent_or_corner_z) = (uint16_t)(uintptr_t)(other_actor->mesh_polygon_array);
201
202 break;
203 }
204
205 case 2:
206 {
207 *(uint16_t *)((char *)&packed_corner_xy + 2) = other_actor->visual_scale.y;
208
209 break;
210 }
211
212 case 3:
213 {
214 (*(uint16_t *)&expanded_extent_or_corner_z) = -(int16_t)(uintptr_t)other_actor->mesh_polygon_array;
215
216 break;
217 }
218 }
219
220 *(uint32_t *)&vertices[i] = packed_corner_xy;
221 vertices[i].pos.z = (int16_t)expanded_extent_or_corner_z;
222
223 int32_t opposite_vertex_byte_offset = (i - ((i & 1) << 1) + 5) * 0xc;
224
225 vertices[i - ((i & 1) << 1) + 5].pos.x = (int16_t)-(packed_corner_xy);
226 i += 1;
227 vertices[opposite_vertex_byte_offset / 12].pos.y = -(int16_t)(packed_corner_xy >> 16);
228 vertices[opposite_vertex_byte_offset / 12].pos.z = -(int16_t)expanded_extent_or_corner_z;
229 }
230
231 for (int32_t extent_axis_index = 0; extent_axis_index < 3; extent_axis_index++)
232 {
233 Collision_BoxFacePolygons[extent_axis_index * 2 + 1].face_plane->plane_distance =
234 -((int32_t)((const struct Actor_CollisionBoxExtentView *)&other_actor->visual_scale)->half_extents_q6[extent_axis_index]) << 0xa;
235 Collision_BoxFacePolygons[extent_axis_index * 2].face_plane->plane_distance =
236 Collision_BoxFacePolygons[extent_axis_index * 2 + 1].face_plane->plane_distance;
237 }
238
239 Collision_DetectAndResolve3DCollision(actor, (struct Collision_Node *)(other_actor), vertices,
241
242 return;
243 }
244
245 bool has_more_axes = axis_index + 1 < 3;
246
247 axis_index += 1;
248 next_rotation_column = &next_rotation_column[1];
249
250 if (!has_more_axes)
251 break;
252
253 rotation_column = next_rotation_column;
254 }
255
256 /* Native MOVSX at 0x43419c sign-extends this callback index. */
257 if (Actor_DefaultUpdateCallbackSlot[(int32_t)(int8_t)actor->collision_class](actor, other_actor, 0, 0))
258 actor->scale_factor = 0;
259}
#define ACTOR_LIFECYCLE_DISABLED
Definition constants.h:39
#define ACTOR_BEHAVIOR_WORKLIST_MEMBER
Definition constants.h:34
#define ACTOR_BEHAVIOR_MOVEMENT_ACTIVE
Definition constants.h:19
#define ACTOR_LIFECYCLE_FORCE_HIDDEN
Definition constants.h:44
#define Actor_DefaultUpdateCallbackSlot
Definition globals.h:30
#define COLLISION_POLYGON_GROUND_EXCLUSION_LOW
Definition constants.h:32
#define COLLISION_NODE_CULL_DISABLED
Definition constants.h:60
#define COLLISION_NODE_SKIP_POLYGON_QUERY
Definition constants.h:41
#define COLLISION_NODE_SKIP_GROUND_QUERY
Definition constants.h:38
#define COLLISION_CALLBACK_DAMAGE_PROBE
Definition constants.h:46
#define COLLISION_GROUND_HEIGHT_TOLERANCE_Q6
Definition constants.h:55
#define COLLISION_NODE_COLLIDABLE
Definition constants.h:37
#define COLLISION_NODE_CULL_FORCE_HIDDEN
Definition constants.h:61
@ COLLISION_NODE_ORIENTED_BOX
Definition constants.h:8
#define COLLISION_ATTACHMENT_SKIP_ACTOR_SCAN
Definition constants.h:64
void Collision_ProjectPointOntoPolygonSurface(struct Collision_Node const *collision_node, struct Collision_Polygon const *contact_poly, struct Math_Vec3I32 const *world_pos, struct Math_Vec3I32 *out_world_pos)
Definition ground.c:233
int32_t Collision_GetShapeRadiusQ12(const struct Actor_CollisionShapeNodeView *shape)
Definition geometry.c:17
void Collision_DetectAndResolve3DCollision(struct Actor_State *actor, struct Collision_Node *collision_source, struct Collision_Vertex *vertices, struct Collision_Polygon *polygons, int32_t polygon_count)
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
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 g_collisionResponseBuffer
Definition globals.h:48
#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 g_collisionTestPosY
Definition globals.h:30
#define g_collisionTestMotionYQ12
Definition globals.h:39
#define ENTITY_FLAG_ACTOR_COLLISION_SCAN
Definition constants.h:25
#define MATH_Q12_SHIFT
Definition constants.h:8
int32_t Math_ApproxDistance3DWeighted(int32_t dx, int32_t dy, int32_t dz)
Definition fixed_point.c:59
void Collision_DetectObjectNodeCollisions(struct Actor_State *actor, int32_t object_list_index)
Definition objects.c:111
void Collision_DetectDynamicObject(struct Actor_State *actor, int32_t *in_out_ground_y)
Definition objects.c:33
static int32_t Math_DotProduct3Q12(int32_t ax, int32_t ay, int32_t az, int32_t bx, int32_t by, int32_t bz)
Definition objects.c:24
void Collision_ResolveObjectNodeCollision(struct Actor_State *actor, struct Actor_State *other_actor)
Definition objects.c:154
#define pkg_resource_current_level_data
Definition globals.h:117
#define g_objectNodeRoot
Definition globals.h:12
uint8_t collision_class
Definition types.h:435
struct Component_Instance * collision_component_or_parent_component
Definition types.h:437
struct Collision_Polygon * ground_contact_polygon
Definition types.h:452
struct Collision_Node * ground_collision_node
Definition types.h:451
struct Math_Vec2I16 visual_scale
Definition types.h:395
struct Mesh_Polygon * mesh_polygon_array
Definition types.h:396
int32_t behavior_flags
Definition types.h:407
int16_t scale_factor
Definition types.h:439
void * record_ptr
Definition types.h:477
struct Math_Vec3I32 sub_pos
Definition types.h:386
struct Math_Matrix3x3I16 rot_mat
Definition types.h:383
struct Math_Vec3I32 position
Definition types.h:385
struct Collision_Vertex * vertices
Definition types.h:292
int16_t polygon_count
Definition types.h:290
uint32_t flags
Definition types.h:297
uint8_t node_type
Definition types.h:286
int16_t vertex_count
Definition types.h:289
struct Collision_Polygon * polygons
Definition types.h:291
int32_t collision_radius
Definition types.h:307
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
uint16_t flags
Definition types.h:66
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 m10
Definition types.h:165
int16_t x
Definition types.h:402
int16_t y
Definition types.h:403
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472