102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
behavior.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
15#include "pcdogs/actor/types.h"
20#include "pcdogs/entity/types.h"
21#include "pcdogs/game/globals.h"
22#include "pcdogs/game/types.h"
24#include "pcdogs/math/types.h"
31#include "pcdogs/scene/types.h"
33#include <math.h>
34#include <stdarg.h>
35#include <string.h>
36
37// Original pcdogs.exe: 0x40b080
38// Aggregates actor contacts, caching nearest targets and collision responses. For negative selectors, the second argument is an actor-pointer output slot rather than an actor, and the function resolves the requested reference or a filtered fallback.
39int32_t Physics_ProcessActorCollision(struct Actor_State *actor, void *other_actor_or_out_reference, int32_t distance_or_reference_selector)
40{
41 struct Player_ControllerRecordView *controller = actor->record_ptr;
42
43 if (distance_or_reference_selector < 0)
44 {
45 struct Actor_State **out_reference_actor = other_actor_or_out_reference;
46 int32_t component_reference_result = ComponentCollision_ResolveReferenceActor(controller->component_records, 2,
47 UINT32_MAX - distance_or_reference_selector, out_reference_actor);
48
49 if (component_reference_result && *out_reference_actor)
50 return component_reference_result;
51
52 struct Actor *target;
53
55 target = controller->contact_slots[0].target;
56 else
57 target = controller->contact_slots[1].target;
58
60 target = NULL;
61
62 *out_reference_actor = (struct Actor_State *)target;
63
64 return 0;
65 }
66
67 struct Actor_State *other_actor = other_actor_or_out_reference;
68 int32_t contact_distance_q6 = distance_or_reference_selector >> 6;
69 int32_t collision_class = (int32_t)(int8_t)other_actor->collision_class;
70 struct Actor_State *contact_candidate = other_actor;
71
73 return UINT32_MAX;
74
76 {
77 struct Player_ControllerRecordView *other_controller = other_actor->record_ptr;
78 struct Entity *entity_array = pkg_resource_current_level_data->entity_array;
79
80 if (entity_array[(int32_t)other_controller->owner_entity_index].flags & ENTITY_FLAG_ACTIVE_WORKLIST_REQUEST)
81 collision_class = 1;
82 else if (!(*(uint8_t *)&entity_array[(int32_t)controller->owner_entity_index].flags & ENTITY_FLAG_ACTIVE_WORKLIST_REQUEST) && contact_distance_q6 < ((int32_t)actor->collision_radius + (int32_t)actor->collision_height + (int32_t)other_actor->collision_radius + (int32_t)other_actor->collision_height) >> 1)
83 {
84 if (controller->collision_weight < other_controller->collision_weight * ACTOR_PUPPY_MAX_WEIGHT_RATIO)
85 {
86 int32_t separation_z = actor->position.z - other_actor->position.z;
87 int32_t separation_x = actor->position.x - other_actor->position.x;
88 int32_t horizontal_separation_y = 0;
89
90 int64_t separation_x_squared_wide = (int64_t)separation_x * separation_x;
91 int64_t separation_y_squared_wide = (int64_t)horizontal_separation_y * horizontal_separation_y;
92 int64_t separation_xy_squared_wide = separation_x_squared_wide + separation_y_squared_wide;
93 int64_t separation_z_squared_wide = (int64_t)separation_z * separation_z;
94 int32_t horizontal_separation_length =
95 Math_SqrtFP12((int32_t)((separation_z_squared_wide + separation_xy_squared_wide) >> PHYSICS_FIXED_FRACTION_BITS));
96 int32_t repulsion_z;
97 int32_t repulsion_y;
98 int32_t repulsion_x;
99
100 if (horizontal_separation_length)
101 {
102 int32_t repulsion_scale_q16 = (int64_t)(uint32_t)PHYSICS_REPULSION_SCALE_Q24 / horizontal_separation_length;
103 int64_t repulsion_x_wide = (int64_t)repulsion_scale_q16 * separation_x;
104
105 separation_x = ((int32_t)((repulsion_x_wide) >> PHYSICS_FIXED_FRACTION_BITS)) >> 4;
106
107 int64_t repulsion_y_wide = (int64_t)repulsion_scale_q16 * horizontal_separation_y;
108 int64_t repulsion_z_wide = (int64_t)repulsion_scale_q16 * separation_z;
109
110 repulsion_y = ((int32_t)((repulsion_y_wide) >> PHYSICS_FIXED_FRACTION_BITS)) >> 4;
111 repulsion_x = separation_x;
112 repulsion_z = ((int32_t)((repulsion_z_wide) >> PHYSICS_FIXED_FRACTION_BITS)) >> 4;
113 }
114 else
115 {
116 repulsion_z = 0;
117 repulsion_y = 0;
118 repulsion_x = 0;
119 }
120
121 int32_t queued_impulse_y = controller->queued_impulse_q12.y + repulsion_y;
122 int32_t queued_impulse_z = controller->queued_impulse_q12.z + repulsion_z;
123
124 controller->queued_impulse_q12.x += repulsion_x;
125 controller->queued_impulse_q12.y = queued_impulse_y;
126 controller->queued_impulse_q12.z = queued_impulse_z;
127 }
128
131 }
132
133 if (other_controller->death_stage)
134 contact_candidate = NULL;
135 }
136
137 if (actor->component_count && contact_candidate)
138 {
139 struct Actor_ContactSlot *contact = &controller->contacts_by_collision_class[collision_class];
140 --collision_class;
141 if (!contact->target || contact_distance_q6 < contact->distance)
142 {
143 contact->target = (struct Actor *)contact_candidate;
144 contact->distance = contact_distance_q6;
145 }
146 }
147
148 return ((collision_class != 1 && !(pkg_resource_current_level_data->entity_array[(int32_t)controller->owner_entity_index].flags & ENTITY_FLAG_ACTIVE_WORKLIST_REQUEST)) ? 0 : -1);
149}
150
151// Original pcdogs.exe: 0x434ad0
152// Preprocesses an active actor's transforms, motion and ground contacts, runs collision passes and marks the actor processed. Inactive or filtered actors return false.
154{
156 {
158
160 {
161 return 0;
162 }
163 }
164 else
165 {
166 int32_t behavior_flags = actor->behavior_flags;
167
170 }
171
173
175 {
176 return 0;
177 }
178
179 int32_t behavior_flags_1 = actor->behavior_flags;
180 int32_t movement_flags = behavior_flags_1 & (ACTOR_BEHAVIOR_AIRBORNE | ACTOR_BEHAVIOR_GROUNDED_MOTION);
181 int8_t movement_handler = actor->movement_handler_index;
182
183 if (movement_handler > 0)
184 g_actorPreprocessCallbacks[movement_handler](actor);
186 {
187 uint16_t facing_angle = actor->facing_blend + actor->facing_current;
188
189 Math_BuildMatrixRotationY(&actor->rot_mat, NULL, facing_angle & PHYSICS_ANGLE_MASK);
190 }
191
192 struct Actor_State *parent_actor = actor->parent_actor;
193
194 // Init sub_pos from velocity; clear ANIMATED flag and some behavior bits
196
197 // Platform attachment: inherit velocity from platform actor
199
200 actor->behavior_flags = behavior_flags_8;
201
202 int32_t preprocess_behavior_flags = behavior_flags_8;
203 union Physics_PreprocessMatrixOrVelocity rotation_or_resolved_velocity;
204 union Physics_PreprocessQuaternionOrVelocity quaternion_or_saved_velocity;
205
206 if (!parent_actor)
207 {
208 actor->sub_pos.x = actor->velocity.x;
209 actor->sub_pos.y = actor->velocity.y;
210 actor->sub_pos.z = actor->velocity.z;
211
212 int32_t airborne_movement_bit = movement_flags & ACTOR_BEHAVIOR_AIRBORNE;
213
214 if (!airborne_movement_bit && !(preprocess_behavior_flags & ACTOR_BEHAVIOR_MOVEMENT_LOCKED))
215 {
216 struct Collision_Node *ground_collision_node = actor->ground_collision_node;
217
218 preprocess_behavior_flags |= ACTOR_BEHAVIOR_GROUNDED_MOTION;
219 actor->behavior_flags = preprocess_behavior_flags;
220
221 if (!ground_collision_node || ground_collision_node->node_type == COLLISION_NODE_ORIENTED_BOX)
222 actor->facing_blend = 0;
223 else
224 {
226
227 int32_t ground_world_y = actor->sub_pos.y;
228 int32_t previous_position_y = actor->position.y;
229
230 actor->sub_pos.x += actor->velocity.x - actor->position.x;
231
232 int32_t previous_position_z = actor->position.z;
233 int32_t ground_world_z = actor->sub_pos.z;
234
235 actor->sub_pos.y = ground_world_y + actor->velocity.y - previous_position_y;
236
237 int32_t behavior_flags_2 = actor->behavior_flags;
238
239 actor->sub_pos.z = ground_world_z + actor->velocity.z - previous_position_z;
240
241 if ((uint8_t)behavior_flags_2 & ACTOR_BEHAVIOR_AIRBORNE || actor->facing_blend == PHYSICS_FIXED_ONE)
242 {
243 actor->behavior_flags = behavior_flags_2 | ACTOR_BEHAVIOR_GROUND_BASIS_CHANGED;
244
245 struct Math_Matrix3x3I16 ground = ground_collision_node->transform_matrix;
246
247 actor->contact_basis = (struct Math_Matrix3x3I16){.elements = {
248 ground.m00, ground.m10, ground.m20,
249 ground.m01, ground.m11, ground.m21,
250 ground.m02, ground.m12, ground.m22}};
251
252 int16_t blended_facing_word = actor->facing_current;
253 int32_t facing_target = (int32_t)actor->facing_target;
254 int32_t current_facing = (int32_t)(int16_t)blended_facing_word;
255
256 if (current_facing == facing_target)
257 {
258 actor->facing_target = (actor->facing_blend + (uint16_t)blended_facing_word) & PHYSICS_ANGLE_MASK;
259 }
260
261 blended_facing_word = (uint16_t)blended_facing_word + actor->facing_blend;
262 // Child actor quaternion-to-matrix: build 3x3 rotation from contact_tangent quat, apply to rot_mat
263 actor->facing_current = (uint16_t)blended_facing_word & PHYSICS_ANGLE_MASK;
264 actor->facing_blend = 0;
265 }
266 else
267 {
268 Math_MulMat3x3Q12(&rotation_or_resolved_velocity.rotation_q12,
269 &actor->contact_basis, &ground_collision_node->transform_matrix);
270
271 int32_t rotation_q12 =
272 (int32_t)rotation_or_resolved_velocity.rotation_q12.m22;
273 uint16_t angle = PHYSICS_ANGLE_QUARTER_TURN - actor->facing_blend;
274 int32_t facing_blend_cosine_q14 = Math_SinQ14(angle);
275 uint16_t angle_1 = actor->facing_blend;
276 int32_t ground_facing_cosine_term = ((facing_blend_cosine_q14 >> 2) * (int32_t)rotation_or_resolved_velocity.rotation_q12.m02) >> PHYSICS_FIXED_FRACTION_BITS;
277 int32_t facing_blend_sine_q14;
278
279 facing_blend_sine_q14 = Math_SinQ14(angle_1);
280
281 if ((uint32_t)((((facing_blend_sine_q14 >> 2) * rotation_q12) >> PHYSICS_FIXED_FRACTION_BITS) - ground_facing_cosine_term + PHYSICS_GROUND_YAW_ERROR_SLOP) > PHYSICS_GROUND_YAW_ERROR_RANGE)
282 {
283 int16_t ground_facing_angle;
284
285 ground_facing_angle = Math_CalculateAtan2Fast(rotation_q12,
286 (int32_t)rotation_or_resolved_velocity.rotation_q12.m02);
287 actor->facing_blend = ground_facing_angle;
288
289 uint16_t combined_facing_angle = actor->facing_current + ground_facing_angle;
290 int32_t combined_facing_cosine_q14 = Math_SinQ14(PHYSICS_ANGLE_QUARTER_TURN - (combined_facing_angle & PHYSICS_ANGLE_MASK));
291 uint16_t current_blended_facing = actor->facing_current + actor->facing_blend;
292 int16_t combined_facing_cosine_q12 = (int16_t)(combined_facing_cosine_q14 >> 2);
293 int32_t combined_facing_sine_q12 = Math_SinQ14(current_blended_facing & PHYSICS_ANGLE_MASK) >> 2;
294
295 actor->rot_mat.m00 = combined_facing_cosine_q12;
296 actor->rot_mat.m20 = (uint16_t)-combined_facing_sine_q12;
297 actor->rot_mat.m02 = (uint16_t)combined_facing_sine_q12;
298 actor->rot_mat.m22 = combined_facing_cosine_q12;
299 }
300 }
301 }
302 }
303
305 {
306 struct Physics_GroundTiltChildView *ground_tilt_child = (struct Physics_GroundTiltChildView *)actor->child_actor;
307
308 if (ground_tilt_child)
309 {
310 if (ground_tilt_child->blend_remaining_q6)
311 {
312 if (airborne_movement_bit)
313 {
314 quaternion_or_saved_velocity.constant_quaternion.value_q14.w = PHYSICS_Q14_ONE;
315 // Parent-relative sub_pos: if parent is type 3 (platform) with flag 0x20, add parent sub_pos
316 quaternion_or_saved_velocity.constant_quaternion.value_q14.z = 0;
317 quaternion_or_saved_velocity.constant_quaternion.value_q14.y = 0;
318 quaternion_or_saved_velocity.constant_quaternion.value_q14.x = 0;
319 }
320 else
321 {
322 struct Math_QuaternionI16 blend_target_wx;
323
324 blend_target_wx.w = ground_tilt_child->target_tilt_q14.w;
325 blend_target_wx.x = ground_tilt_child->target_tilt_q14.x;
326
327 struct Math_QuaternionI16 blend_target_yz;
328
329 blend_target_yz.y = ground_tilt_child->target_tilt_q14.y;
330 blend_target_yz.z = ground_tilt_child->target_tilt_q14.z;
331 quaternion_or_saved_velocity.constant_quaternion.value_q14.w = blend_target_wx.w;
332 quaternion_or_saved_velocity.constant_quaternion.value_q14.x = blend_target_wx.x;
333 quaternion_or_saved_velocity.constant_quaternion.value_q14.y = blend_target_yz.y;
334 quaternion_or_saved_velocity.constant_quaternion.value_q14.z = blend_target_yz.z;
335 }
336
337 quaternion_or_saved_velocity.constant_quaternion.channel_data_minus_one_sentinel = (uint8_t *)UINT32_MAX;
339 PHYSICS_GROUND_TILT_BLEND_SCALE / (int32_t)ground_tilt_child->blend_remaining_q6,
340 (struct Animation_SplineChannelQuat *)&quaternion_or_saved_velocity.constant_quaternion, &ground_tilt_child->blended_tilt_q14);
341
342 struct Actor_State *child_actor = actor->child_actor;
343
345
346 // Apply spin_speed_hi as X-axis rotation (pitch tilt) via Matrix_BuildRotationX
347 int32_t blended_tilt_x = (int32_t)ground_tilt_child->blended_tilt_q14.x;
348 int32_t blended_tilt_x_squared = blended_tilt_x * blended_tilt_x;
349 int32_t blended_tilt_y = (int32_t)ground_tilt_child->blended_tilt_q14.y;
350 int32_t blended_tilt_z = (int32_t)ground_tilt_child->blended_tilt_q14.z;
351 int32_t blended_tilt_y_squared = blended_tilt_y * blended_tilt_y;
352 // Collision detection: reset push vectors, run Collision_DetectActorCollisions (twice if bounce flag
353 // 0x8000000)
354 int32_t blended_tilt_zy = blended_tilt_z * blended_tilt_y;
355 int32_t blended_tilt_w = (int32_t)ground_tilt_child->blended_tilt_q14.w;
356 int32_t blended_tilt_xy = blended_tilt_y * blended_tilt_x;
357 int32_t blended_tilt_wx = blended_tilt_w * blended_tilt_x;
358 int32_t blended_tilt_zx = blended_tilt_z * blended_tilt_x;
359 int32_t blended_tilt_wz = blended_tilt_w * blended_tilt_z;
360 int32_t blended_tilt_wy = blended_tilt_w * blended_tilt_y;
361
362 rotation_or_resolved_velocity.rotation_q12.m00 =
363 (int16_t)((PHYSICS_QUATERNION_DIAGONAL_HALF_Q28 - blended_tilt_z * blended_tilt_z - blended_tilt_y_squared) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
364 rotation_or_resolved_velocity.rotation_q12.m10 =
365 (int16_t)((blended_tilt_xy - blended_tilt_wz) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
366 rotation_or_resolved_velocity.rotation_q12.m20 =
367 (int16_t)((blended_tilt_wy + blended_tilt_zx) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
368 rotation_or_resolved_velocity.rotation_q12.m01 =
369 (int16_t)((blended_tilt_wz + blended_tilt_xy) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
370 rotation_or_resolved_velocity.rotation_q12.m11 =
371 (int16_t)((PHYSICS_QUATERNION_DIAGONAL_HALF_Q28 - blended_tilt_z * blended_tilt_z - blended_tilt_x_squared) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
372 rotation_or_resolved_velocity.rotation_q12.m02 =
373 (int16_t)((blended_tilt_zx - blended_tilt_wy) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
374 rotation_or_resolved_velocity.rotation_q12.m21 =
375 (int16_t)((blended_tilt_zy - blended_tilt_wx) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
376 // Post-collision: if XZ velocity near zero after bounce, check dot product to decide if
377 // actor should stop
378 rotation_or_resolved_velocity.rotation_q12.m12 =
379 (int16_t)((blended_tilt_wx + blended_tilt_zy) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
380 rotation_or_resolved_velocity.rotation_q12.m22 =
381 (int16_t)((PHYSICS_QUATERNION_DIAGONAL_HALF_Q28 - blended_tilt_y_squared - blended_tilt_x_squared) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
382 Math_MulMat3x3Q12(&actor->rot_mat, &actor->rot_mat,
383 &rotation_or_resolved_velocity.rotation_q12);
384 }
385 else if (!airborne_movement_bit)
386 {
387 struct Math_QuaternionI16 target_tilt_wx_pair;
388
389 target_tilt_wx_pair.w = ground_tilt_child->target_tilt_q14.w;
390 target_tilt_wx_pair.x = ground_tilt_child->target_tilt_q14.x;
391
392 int16_t target_tilt_y_word = ground_tilt_child->target_tilt_q14.y;
393 int16_t target_tilt_z_word = ground_tilt_child->target_tilt_q14.z;
394
395 ground_tilt_child->blended_tilt_q14.w = target_tilt_wx_pair.w;
396 ground_tilt_child->blended_tilt_q14.x = target_tilt_wx_pair.x;
397 target_tilt_wx_pair.w = ground_tilt_child->target_tilt_q14.w;
398 ground_tilt_child->blended_tilt_q14.y = target_tilt_y_word;
399 ground_tilt_child->blended_tilt_q14.z = target_tilt_z_word;
400
401 if (target_tilt_wx_pair.w != PHYSICS_Q14_ONE)
402 {
403 int32_t target_tilt_x = (int32_t)ground_tilt_child->target_tilt_q14.x;
404 int32_t target_tilt_x_squared = target_tilt_x * target_tilt_x;
405 int32_t target_tilt_y = (int32_t)ground_tilt_child->target_tilt_q14.y;
406 int32_t target_tilt_y_squared = target_tilt_y * target_tilt_y;
407 int32_t target_tilt_z = (int32_t)ground_tilt_child->target_tilt_q14.z;
408 int32_t target_tilt_zy = target_tilt_z * target_tilt_y;
409 int32_t target_tilt_w = (int32_t)target_tilt_wx_pair.w;
410 int32_t target_tilt_xy = target_tilt_y * target_tilt_x;
411 int32_t target_tilt_wx = target_tilt_w * target_tilt_x;
412 int32_t target_tilt_zx = target_tilt_z * target_tilt_x;
413 int32_t target_tilt_wz = target_tilt_w * target_tilt_z;
414 int32_t target_tilt_wy = target_tilt_w * target_tilt_y;
415
416 rotation_or_resolved_velocity.rotation_q12.m00 =
417 (int16_t)((PHYSICS_QUATERNION_DIAGONAL_HALF_Q28 - target_tilt_z * target_tilt_z - target_tilt_y_squared) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
418 rotation_or_resolved_velocity.rotation_q12.m10 =
419 (int16_t)((target_tilt_xy - target_tilt_wz) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
420 rotation_or_resolved_velocity.rotation_q12.m20 =
421 (int16_t)((target_tilt_wy + target_tilt_zx) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
422 rotation_or_resolved_velocity.rotation_q12.m01 =
423 (int16_t)((target_tilt_wz + target_tilt_xy) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
424 rotation_or_resolved_velocity.rotation_q12.m11 =
425 (int16_t)((PHYSICS_QUATERNION_DIAGONAL_HALF_Q28 - target_tilt_z * target_tilt_z - target_tilt_x_squared) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
426 rotation_or_resolved_velocity.rotation_q12.m02 =
427 (int16_t)((target_tilt_zx - target_tilt_wy) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
428 rotation_or_resolved_velocity.rotation_q12.m21 =
429 (int16_t)((target_tilt_zy - target_tilt_wx) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
430 rotation_or_resolved_velocity.rotation_q12.m12 =
431 (int16_t)((target_tilt_wx + target_tilt_zy) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
432 rotation_or_resolved_velocity.rotation_q12.m22 =
433 (int16_t)((PHYSICS_QUATERNION_DIAGONAL_HALF_Q28 - target_tilt_y_squared - target_tilt_x_squared) >> PHYSICS_QUATERNION_MATRIX_SHIFT);
434 Math_MulMat3x3Q12(&actor->rot_mat, &actor->rot_mat,
435 &rotation_or_resolved_velocity.rotation_q12);
436 }
437 }
438 }
439 }
440 }
441 else if (parent_actor->actor_type == ACTOR_OBJECT_RUNTIME)
442 {
443 int32_t parent_behavior_flags = parent_actor->behavior_flags;
444
445 if (parent_behavior_flags & ACTOR_BEHAVIOR_PREPROCESSED)
446 {
447 int32_t child_sub_position_y = actor->sub_pos.y;
448 int32_t child_sub_position_z = actor->sub_pos.z;
449
450 actor->sub_pos.x += parent_actor->sub_pos.x;
451 // Barycentric ground tracking: transform barycentric to world, add velocity delta to sub_pos
452 actor->sub_pos.y = child_sub_position_y + parent_actor->sub_pos.y;
453 actor->sub_pos.z = child_sub_position_z + parent_actor->sub_pos.z;
454 }
455 }
456
457 uint16_t roll_angle = actor->roll_angle;
458
459 if (roll_angle)
460 Math_BuildMatrixRotationZ(&actor->rot_mat, &actor->rot_mat, roll_angle);
461
462 int32_t behavior_flags_3 = actor->behavior_flags;
463
464 if (behavior_flags_3 & ACTOR_BEHAVIOR_BYPASS_COLLISION)
465 {
466 actor->behavior_flags = behavior_flags_3 & ((uint32_t)~(ACTOR_BEHAVIOR_PENETRATING | ACTOR_BEHAVIOR_COLLISION_CORRECTION));
467
468 if (actor->sub_pos.x || actor->sub_pos.y || actor->sub_pos.z)
469 {
470 actor->collision_push_components.z = 0;
471 actor->collision_push_xz = 0;
472 }
473 }
474 else
475 {
476 quaternion_or_saved_velocity.saved_velocity_q12.x = actor->velocity.x;
477 quaternion_or_saved_velocity.saved_velocity_q12.y = actor->velocity.y;
478 quaternion_or_saved_velocity.saved_velocity_q12.z = actor->velocity.z;
479 actor->collision_push_components.z = 0;
480 actor->collision_push_xz = 0;
482
484 {
486
487 int32_t post_collision_x = actor->velocity.x;
488 // Post-collision Y velocity: if bounced upward and grounded clamp; if airborne and fast set flag 0x10
489 int64_t post_collision_x_squared_wide = (int64_t)post_collision_x * post_collision_x;
490 int32_t post_collision_z = actor->velocity.z;
491 int64_t post_collision_z_squared_wide = (int64_t)post_collision_z * post_collision_z;
492
493 if (((int32_t)((uint64_t)post_collision_z_squared_wide >> PHYSICS_FIXED_FRACTION_BITS)) + ((int32_t)((uint64_t)post_collision_x_squared_wide >> PHYSICS_FIXED_FRACTION_BITS)) <= 2)
494 {
495 int32_t resolved_velocity_x = actor->velocity.x;
496
497 rotation_or_resolved_velocity.collision.resolved_velocity_q12.x = resolved_velocity_x;
498
499 int32_t resolved_velocity_y = actor->velocity.y;
500
501 rotation_or_resolved_velocity.collision.resolved_velocity_q12.y = resolved_velocity_y;
502 rotation_or_resolved_velocity.collision.resolved_velocity_q12.z = actor->velocity.z;
503 actor->velocity.z = 0;
504
505 // Set behavior_flags bit 0x2000 (physics-processed) and return
506 int32_t behavior_flags_4 = actor->behavior_flags;
507
508 actor->velocity.x = 0;
509
510 if (!((uint8_t)behavior_flags_4 & ACTOR_BEHAVIOR_AIRBORNE) && actor->velocity.y - quaternion_or_saved_velocity.saved_velocity_q12.y < PHYSICS_SETTLED_VERTICAL_SPEED_SLOP)
511 {
512 quaternion_or_saved_velocity.saved_velocity_q12.y = 0;
513 actor->velocity.y = 0;
514 }
515
516 if (!actor->velocity.y && !(behavior_flags_4 & (ACTOR_BEHAVIOR_AIRBORNE | ACTOR_BEHAVIOR_PENETRATING)))
517 {
518 int64_t velocity_alignment_x_wide = (int64_t)quaternion_or_saved_velocity.saved_velocity_q12.x *
519 rotation_or_resolved_velocity.collision.resolved_velocity_q12.x;
520 int64_t velocity_alignment_z_wide = (int64_t)quaternion_or_saved_velocity.saved_velocity_q12.z *
521 rotation_or_resolved_velocity.collision.resolved_velocity_q12.z;
522
523 if (((int32_t)((uint64_t)velocity_alignment_z_wide >> PHYSICS_FIXED_FRACTION_BITS)) + ((int32_t)((uint64_t)velocity_alignment_x_wide >> PHYSICS_FIXED_FRACTION_BITS)) >= -2)
524 {
525 int32_t settled_behavior_flags = actor->behavior_flags & ((uint32_t)~ACTOR_BEHAVIOR_COLLISION_CORRECTION);
526
527 actor->sub_pos = (struct Math_Vec3I32){
528 .x = actor->sub_pos.x - resolved_velocity_x,
529 .y = actor->sub_pos.y - resolved_velocity_y,
530 .z = actor->sub_pos.z - (uint32_t)rotation_or_resolved_velocity.collision.resolved_velocity_q12.z};
531
532 actor->behavior_flags = settled_behavior_flags;
533 }
534 }
535 }
536
537 int32_t post_collision_y = actor->velocity.y;
538 int32_t saved_velocity_y = quaternion_or_saved_velocity.saved_velocity_q12.y;
539
540 if (post_collision_y > saved_velocity_y)
541 {
542 int32_t behavior_flags_5 = actor->behavior_flags;
543
544 if ((uint8_t)behavior_flags_5 & ACTOR_BEHAVIOR_AIRBORNE)
545 actor->velocity.y = saved_velocity_y;
546 else if (post_collision_y > PHYSICS_MOVEMENT_VERTICAL_THRESHOLD)
547 {
548 behavior_flags_5 |= ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
549 actor->behavior_flags = behavior_flags_5;
550 }
551 }
552 }
553 }
554
555 int32_t behavior_flags_6 = actor->behavior_flags;
556
557 behavior_flags_6 |= ACTOR_BEHAVIOR_PREPROCESSED;
558 actor->behavior_flags = behavior_flags_6;
559
560 return 1;
561}
562
563// Original pcdogs.exe: 0x4356b0
564// Integrates movement immediately for an already-processed actor; otherwise it integrates only after preprocessing succeeds.
566{
568 // Main physics update: velocity integration, collision, ground detection
570 // Flag 0x2000 = physics enabled. If clear, run preprocessing first
571 else if (Physics_UpdateActorPreprocess(actor))
573}
574
575static int32_t force_product_q12(int32_t magnitude, int32_t direction)
576{
577 return (int32_t)(((int64_t)magnitude * direction) >> PHYSICS_FIXED_FRACTION_BITS);
578}
579
580// Applies axial, radial and orbital force and adjusts the movement speed limit.
581int32_t Physics_ApplyEmitterForces(struct Actor_State *affected_actor, struct Pkg_ActorRecord *unused_record,
582 struct Physics_CollisionForceEmitterActorView *emitter, struct Math_Vec3I32 *velocity, int32_t *speed_limit,
583 int32_t attenuation)
584{
585 (void)unused_record;
586 uint32_t flags = emitter->mode_and_attenuation_flags;
587 int32_t strength = emitter->axial_or_inward_force_q12;
588
590 {
591 const struct Math_Matrix3x3I16 *rotation = &emitter->transform.world_rot_matrix;
592 int32_t fx = force_product_q12(strength, rotation->m01);
593 int32_t fy = force_product_q12(strength, rotation->m11);
594 int32_t fz = force_product_q12(strength, rotation->m21);
595 velocity->x += fx;
596 velocity->z += fz;
597 velocity->y += fy;
598 *speed_limit += emitter->axial_or_inward_force_q12;
599 return fy;
600 }
601
602 int32_t dx = emitter->transform.world_position.x - affected_actor->position.x;
603 int32_t dy = emitter->transform.world_position.y - affected_actor->position.y;
604 int32_t dz = emitter->transform.world_position.z - affected_actor->position.z;
605
607 {
608 int32_t distance = Math_ApproxDistance3DWeighted(dx, dy, dz);
609 int32_t max_extent = emitter->radius_extent_x_q6;
610 if (max_extent < emitter->radius_extent_y_q6)
611 max_extent = emitter->radius_extent_y_q6;
612 if (max_extent < emitter->radius_extent_z_q6)
613 max_extent = emitter->radius_extent_z_q6;
614 int32_t radius = max_extent * 64;
615 if (distance > radius)
616 return 0;
617 attenuation = PHYSICS_FIXED_ONE - (int32_t)(((int64_t)distance * PHYSICS_FIXED_ONE) / radius);
618 strength = force_product_q12(attenuation, emitter->axial_or_inward_force_q12);
619 }
620
621 *speed_limit += strength;
622 uint64_t squared = (uint64_t)((int64_t)dx * dx) + (uint64_t)((int64_t)dz * dz);
623 int32_t horizontal_distance = Math_SqrtFP12((int32_t)(squared >> PHYSICS_FIXED_FRACTION_BITS));
624 int32_t nx = 0;
625 int32_t nz = 0;
626 if (horizontal_distance)
627 {
628 int32_t inverse_q20 = (int32_t)((INT64_C(1) << 32) / horizontal_distance);
629 /* SHRD narrows to 32 bits before the final signed scale adjustment. */
630 nx = force_product_q12(inverse_q20, dx) >> 8;
631 nz = force_product_q12(inverse_q20, dz) >> 8;
632 }
633 velocity->x += force_product_q12(strength, nx);
634 velocity->z += force_product_q12(strength, nz);
635
636 int32_t orbit = emitter->orbit_force_q12;
637 if (orbit)
638 {
639 /* Retail rereads the mode after writing velocity. */
641 orbit = force_product_q12(attenuation, orbit);
642 *speed_limit += orbit < 0 ? -orbit : orbit;
643 velocity->x += force_product_q12(orbit, nz);
644 velocity->z -= force_product_q12(orbit, nx);
645 }
646 return 0;
647}
#define ACTOR_BEHAVIOR_SKIP_UPDATE
Definition constants.h:7
#define ACTOR_BEHAVIOR_MOVEMENT_LOCKED
Definition constants.h:16
#define ACTOR_BEHAVIOR_AIRBORNE
Definition constants.h:13
#define ACTOR_BEHAVIOR_GROUND_BASIS_CHANGED
Definition constants.h:32
#define ACTOR_LIFECYCLE_DISABLED
Definition constants.h:39
#define ACTOR_BEHAVIOR_GROUND_TILT_ENABLED
Definition constants.h:21
#define ACTOR_PUPPY_MAX_WEIGHT_RATIO
Definition constants.h:209
#define ACTOR_BEHAVIOR_WORKLIST_MEMBER
Definition constants.h:34
#define ACTOR_LIFECYCLE_DESTROYING
Definition constants.h:43
#define ACTOR_BEHAVIOR_PENETRATING
Definition constants.h:31
@ ACTOR_NAV_COMMAND_ACTOR_CONTACT
Definition constants.h:261
#define ACTOR_BEHAVIOR_GROUNDED_MOTION
Definition constants.h:14
@ ACTOR_OBJECT_RUNTIME
Definition constants.h:143
#define ACTOR_BEHAVIOR_BYPASS_COLLISION
Definition constants.h:18
#define ACTOR_BEHAVIOR_MOVEMENT_ACTIVE
Definition constants.h:19
#define ACTOR_BEHAVIOR_PREPROCESSED
Definition constants.h:20
#define ACTOR_BEHAVIOR_COLLISION_CORRECTION
Definition constants.h:33
void Actor_UpdateWorldRenderPosition(struct Actor_State *actor)
Definition rendering.c:686
void Actor_IntegrateMovementAndCollision(struct Actor_State *actor)
Definition collision.c:495
#define g_actorPreprocessCallbacks
Definition globals.h:55
void Animation_InterpolateKeyframeQuatBlend(uint32_t frame_time, int32_t blend_weight_q14, struct Animation_SplineChannelQuat *quat_track, struct Math_QuaternionI16 *inout_quat)
Definition quaternion.c:48
int32_t Physics_ApplyEmitterForces(struct Actor_State *affected_actor, struct Pkg_ActorRecord *unused_record, struct Physics_CollisionForceEmitterActorView *emitter, struct Math_Vec3I32 *velocity, int32_t *speed_limit, int32_t attenuation)
Definition behavior.c:581
int32_t Physics_ProcessActorCollision(struct Actor_State *actor, void *other_actor_or_out_reference, int32_t distance_or_reference_selector)
Definition behavior.c:39
void Physics_UpdateActorBehavior(struct Actor_State *actor)
Definition behavior.c:565
static int32_t force_product_q12(int32_t magnitude, int32_t direction)
Definition behavior.c:575
uint8_t Physics_UpdateActorPreprocess(struct Actor_State *actor)
Definition behavior.c:153
@ COLLISION_ACTOR_CLASS_PROJECTILE
Definition constants.h:69
@ COLLISION_ACTOR_CLASS_CHARACTER
Definition constants.h:68
@ COLLISION_NODE_ORIENTED_BOX
Definition constants.h:8
void Collision_DetectActorCollisions(struct Actor_State *actor, BOOL use_cached_collision_list)
Definition actors.c:136
void Collision_TransformGroundBarycentricToWorld(struct Actor_State const *actor_state, struct Math_Vec3I32 *out_world_pos)
Definition ground.c:167
int32_t ComponentCollision_ResolveReferenceActor(struct Component_CollisionRecord *collision_records, int32_t record_count, int32_t attachment_joint_index, struct Actor_State **out_reference_actor)
Definition actors.c:31
#define ENTITY_FLAG_ACTIVE_WORKLIST_REQUEST
Definition constants.h:9
#define GAME_OVERLAY_SKIP_NODE_ANIMATION
Definition constants.h:34
#define GAME_OVERLAY_SKIP_OBJECT_RENDER
Definition constants.h:36
#define Game_FrameOverlayFlags
Definition globals.h:44
void Math_BuildMatrixRotationY(struct Math_Matrix3x3I16 *out_matrix, struct Math_Matrix3x3I16 const *left_factor, int32_t angle)
Definition matrices.c:76
int32_t Math_SinQ14(int32_t angle)
int32_t Math_SqrtFP12(uint32_t value)
void Math_BuildMatrixRotationZ(struct Math_Matrix3x3I16 *out_matrix, struct Math_Matrix3x3I16 const *left_factor, int32_t angle)
Definition matrices.c:98
void Math_MulMat3x3Q12(struct Math_Matrix3x3I16 *out_matrix, struct Math_Matrix3x3I16 const *right_factor, struct Math_Matrix3x3I16 const *left_factor)
Definition matrices.c:11
int32_t Math_ApproxDistance3DWeighted(int32_t dx, int32_t dy, int32_t dz)
Definition fixed_point.c:59
int32_t Math_CalculateAtan2Fast(int32_t y, int32_t x)
#define NAVIGATION_COMMAND_UNLIMITED_SPEED
Definition constants.h:12
void Navigation_AddCommand(uint8_t command_type, int16_t target_selector_a, int16_t target_selector_b, int32_t speed, struct Math_Vec3I32 const *position)
Definition commands.c:42
#define pkg_resource_current_level_data
Definition globals.h:117
#define PHYSICS_ANGLE_MASK
Definition constants.h:8
#define PHYSICS_QUATERNION_DIAGONAL_HALF_Q28
Definition constants.h:32
#define PHYSICS_ANGLE_QUARTER_TURN
Definition constants.h:9
#define PHYSICS_GROUND_YAW_ERROR_RANGE
Definition constants.h:47
#define PHYSICS_REPULSION_SCALE_Q24
Definition constants.h:33
#define PHYSICS_FIXED_ONE
Definition constants.h:7
#define PHYSICS_GROUND_YAW_ERROR_SLOP
Definition constants.h:46
#define PHYSICS_SETTLED_VERTICAL_SPEED_SLOP
Definition constants.h:42
#define PHYSICS_Q14_ONE
Definition constants.h:11
#define PHYSICS_GROUND_TILT_BLEND_SCALE
Definition constants.h:36
#define PHYSICS_MOVEMENT_VERTICAL_THRESHOLD
Definition constants.h:43
#define PHYSICS_QUATERNION_MATRIX_SHIFT
Definition constants.h:31
#define PHYSICS_FIXED_FRACTION_BITS
Definition constants.h:6
#define PHYSICS_FORCE_EMITTER_AXIAL_MODE
Definition enums.h:9
#define PHYSICS_FORCE_EMITTER_ATTENUATE_BY_MAX_EXTENT
Definition enums.h:15
#define SCENE_NODE_DISABLED
Definition constants.h:24
struct Actor * target
Definition types.h:73
int32_t distance
Definition types.h:72
uint8_t collision_class
Definition types.h:435
int16_t anim_seq_timer
Definition types.h:389
uint8_t component_count
Definition types.h:430
struct Math_Vec3I32 velocity
Definition types.h:446
int32_t collision_push_xz
Definition types.h:492
struct Math_Matrix3x3I16 contact_basis
Definition types.h:380
struct Scene_Node ** render_node_list
Definition types.h:440
int16_t facing_current
Definition types.h:447
uint8_t lifecycle_flags
Definition types.h:392
uint8_t movement_handler_index
Definition types.h:433
uint8_t actor_type
Definition types.h:391
struct Actor_State * child_actor
Definition types.h:478
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
uint16_t z
Definition types.h:497
struct Actor_State::@000347335321225257200273166125221262042042274352::@355017126165076161135046166331324200333365100231 collision_push_components
int16_t collision_radius
Definition types.h:423
int16_t scale_factor
Definition types.h:439
struct Actor_State * parent_actor
Definition types.h:488
int16_t facing_target
Definition types.h:448
int16_t collision_height
Definition types.h:421
void * record_ptr
Definition types.h:477
struct Math_Vec3I32 sub_pos
Definition types.h:386
int16_t facing_blend
Definition types.h:449
struct Math_Matrix3x3I16 rot_mat
Definition types.h:383
int16_t roll_angle
Definition types.h:450
struct Math_Vec3I32 position
Definition types.h:385
Definition types.h:118
uint8_t collision_class
Definition types.h:188
uint8_t lifecycle_flags
Definition types.h:139
int32_t behavior_flags
Definition types.h:160
struct Math_QuaternionI16 value_q14
Definition types.h:832
uint8_t node_type
Definition types.h:286
struct Math_Matrix3x3I16 transform_matrix
Definition types.h:281
Definition types.h:22
uint32_t flags
Definition types.h:25
int16_t m21
Definition types.h:169
int16_t m00
Definition types.h:162
int16_t m01
Definition types.h:163
int16_t elements[9]
Definition types.h:173
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
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472
struct Scene_TransformNodePrefix transform
Definition types.h:113
Physics_ForceEmitterModeFlags mode_and_attenuation_flags
Definition types.h:121
struct Math_QuaternionI16 blended_tilt_q14
Definition types.h:85
struct Math_QuaternionI16 target_tilt_q14
Definition types.h:84
struct Actor_ContactSlot contact_slots[4]
Definition types.h:216
struct Actor_ContactSlot contacts_by_collision_class[5]
Definition types.h:218
struct Component_CollisionRecord * component_records
Definition types.h:209
struct Math_Vec3I32 queued_impulse_q12
Definition types.h:177
struct Math_Matrix3x3I16 world_rot_matrix
Definition types.h:260
struct Math_Vec3I32 world_position
Definition types.h:262
struct Physics_PreprocessMatrixOrVelocity::@262154025151120033125165136317020260333064206015 collision
struct Math_Matrix3x3I16 rotation_q12
Definition types.h:97
struct Math_Vec3I32 resolved_velocity_q12
Definition types.h:102
struct Math_Vec3I32 saved_velocity_q12
Definition types.h:132
struct Animation_SplineChannelQuatConstantView constant_quaternion
Definition types.h:131