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. */
14#include "pcdogs/actor/types.h"
20#include "pcdogs/game/types.h"
23#include "pcdogs/math/types.h"
28#include <math.h>
29#include <stdarg.h>
30#include <string.h>
31
32// Original pcdogs.exe: 0x4095c0
33// Tests whether motion points into the transformed ground normal once its fixed-point squared magnitude exceeds 28. Below that threshold it returns false and leaves the output normal untouched.
34bool Physics_CheckGroundSlopeDirection(struct Actor_State *actor, struct Collision_Polygon *collision_poly, struct Math_Vec3I16 *out_normal, struct Math_Vec3I32 *delta_vec)
35{
36 int64_t delta_x_squared_wide = (int64_t)delta_vec->x * delta_vec->x;
37 int64_t delta_y_squared_wide = (int64_t)delta_vec->y * delta_vec->y;
38 int64_t delta_z_squared_wide = (int64_t)delta_vec->z * delta_vec->z;
39
40 if (((int32_t)(((uint64_t)delta_z_squared_wide + (uint64_t)delta_x_squared_wide + (uint64_t)delta_y_squared_wide) >> PHYSICS_FIXED_FRACTION_BITS)) <= PHYSICS_GROUND_NEAR_POSITION_SQUARED)
41 {
42 return 0;
43 }
44
45 struct Pkg_CollisionFacePlane *face_plane = collision_poly->face_plane;
46
47 out_normal->x = (int16_t)(((int32_t)face_plane->normal.z * (int32_t)actor->rot_mat.m02 + (int32_t)face_plane->normal.y * (int32_t)actor->rot_mat.m01 + (int32_t)face_plane->normal.x * (int32_t)actor->rot_mat.m00) >> PHYSICS_FIXED_FRACTION_BITS);
48
49 struct Pkg_CollisionFacePlane *contact_plane = collision_poly->face_plane;
50
51 out_normal->y = (int16_t)(((int32_t)contact_plane->normal.z * (int32_t)actor->rot_mat.m12 + (int32_t)contact_plane->normal.y * (int32_t)actor->rot_mat.m11 + (int32_t)contact_plane->normal.x * (int32_t)actor->rot_mat.m10) >> PHYSICS_FIXED_FRACTION_BITS);
52
53 struct Pkg_CollisionFacePlane *face_plane_2 = collision_poly->face_plane;
54
55 out_normal->z = (int16_t)(((int32_t)face_plane_2->normal.z * (int32_t)actor->rot_mat.m22 + (int32_t)face_plane_2->normal.y * (int32_t)actor->rot_mat.m21 + (int32_t)face_plane_2->normal.x * (int32_t)actor->rot_mat.m20) >> PHYSICS_FIXED_FRACTION_BITS);
56
57 int64_t normal_dot_x_wide = (int64_t)(int32_t)out_normal->x * delta_vec->x;
58 int64_t normal_dot_y_wide = (int64_t)(int32_t)out_normal->y * delta_vec->y;
59 int64_t normal_dot_z_wide = (int64_t)(int32_t)out_normal->z * delta_vec->z;
60
61 if (((int32_t)(((uint64_t)normal_dot_z_wide + (uint64_t)normal_dot_x_wide + (uint64_t)normal_dot_y_wide) >> PHYSICS_FIXED_FRACTION_BITS)) < 0)
62 {
63 return 1;
64 }
65
66 return 0;
67}
68
69// Original pcdogs.exe: 0x436870
70// Consumes a pending ground reaction, reflects relative motion and applies contact-specific force and speed limits. A -1 result for a non-self type-3 contact is returned after velocity has already been written.
72{
73 // GROUND REACTION PHYSICS: Reflects actor velocity off ground surface normal using dot product. For
74 // moving platforms (type 4), adds platform velocity scaled by speed multiplier and clamps total magnitude
75 // to max speed. Sets landing state in component, plays landing sound (ID 0xB soft/0xE hard).
76 struct Actor_State *pending_ground_actor = record->pending_ground_actor;
77
78 if (!pending_ground_actor)
79 {
80 record->pending_state_2c = 0;
81
82 return 0;
83 }
84
85 record->pending_ground_actor = NULL;
86
87 int64_t contact_dot_x_wide = (int64_t)(int32_t)record->contact_normal_q12.x * record->relative_contact_velocity_q12.x;
88 int64_t contact_dot_y_wide = (int64_t)(int32_t)record->contact_normal_q12.y * record->relative_contact_velocity_q12.y;
89 int64_t contact_dot_z_wide = (int64_t)(int32_t)record->contact_normal_q12.z * record->relative_contact_velocity_q12.z;
90 int32_t twice_normal_velocity = ((int32_t)(((uint64_t)contact_dot_z_wide + (uint64_t)contact_dot_x_wide + (uint64_t)contact_dot_y_wide) >> PHYSICS_FIXED_FRACTION_BITS)) * 2;
91 int32_t response_x;
92 int32_t reflected_y;
93 int32_t reflected_z;
94 int32_t velocity_z;
95 int32_t velocity_y;
96 int32_t velocity_x;
97
98 if (twice_normal_velocity < 0)
99 {
100 int64_t reflection_x_wide = (int64_t)twice_normal_velocity * (int32_t)record->contact_normal_q12.x;
101
102 response_x = (int32_t)((uint64_t)reflection_x_wide >> PHYSICS_FIXED_FRACTION_BITS);
103
104 int64_t reflection_y_wide = (int64_t)twice_normal_velocity * (int32_t)record->contact_normal_q12.y;
105 int64_t reflection_z_wide = (int64_t)twice_normal_velocity * (int32_t)record->contact_normal_q12.z;
106
107 velocity_x = record->relative_contact_velocity_q12.x - response_x;
108 velocity_y = record->relative_contact_velocity_q12.y - ((int32_t)((uint64_t)reflection_y_wide >> PHYSICS_FIXED_FRACTION_BITS));
109 velocity_z = record->relative_contact_velocity_q12.z - ((int32_t)((uint64_t)reflection_z_wide >> PHYSICS_FIXED_FRACTION_BITS));
110 response_x = velocity_x;
111 reflected_y = velocity_y;
112 reflected_z = velocity_z;
113 }
114 else
115 {
116 velocity_x = record->relative_contact_velocity_q12.x;
117 velocity_y = record->relative_contact_velocity_q12.y;
118 velocity_z = record->relative_contact_velocity_q12.z;
119 response_x = velocity_x;
120 reflected_y = velocity_y;
121 reflected_z = velocity_z;
122 }
123
124 if (pending_ground_actor->actor_type != ACTOR_OBJECT_FORCE_EMITTER)
125 {
126 int32_t previous_velocity_y = actor->velocity.y;
127
128 if (velocity_y > previous_velocity_y)
129 {
130 velocity_y = previous_velocity_y;
131 }
132 }
133 else
134 {
135 int64_t platform_impulse_x_wide = (int64_t)(int32_t)(uintptr_t)pending_ground_actor->runtime_vertex_array * (int32_t)record->contact_normal_q12.x;
136 int64_t platform_impulse_y_wide = (int64_t)(int32_t)(uintptr_t)pending_ground_actor->runtime_vertex_array * (int32_t)record->contact_normal_q12.y;
137 int64_t platform_impulse_z_wide = (int64_t)(int32_t)(uintptr_t)pending_ground_actor->runtime_vertex_array * (int32_t)record->contact_normal_q12.z;
138 struct Mesh_RuntimeVertex *platform_impulse_bits =
139 pending_ground_actor->runtime_vertex_array;
140
141 response_x += (int32_t)((uint64_t)platform_impulse_x_wide >> PHYSICS_FIXED_FRACTION_BITS);
142
143 int32_t response_y = reflected_y + ((int32_t)((uint64_t)platform_impulse_y_wide >> PHYSICS_FIXED_FRACTION_BITS));
144 int32_t response_z = reflected_z + ((int32_t)((uint64_t)platform_impulse_z_wide >> PHYSICS_FIXED_FRACTION_BITS));
145
146 if ((int32_t)(uintptr_t)platform_impulse_bits < 0)
147 {
148 int64_t corrected_dot_x_wide = (int64_t)(int32_t)record->contact_normal_q12.x * response_x;
149 int64_t corrected_dot_y_wide = (int64_t)(int32_t)record->contact_normal_q12.y * response_y;
150 int64_t corrected_dot_z_wide = (int64_t)(int32_t)record->contact_normal_q12.z * response_z;
151 int32_t corrected_normal_velocity = (int32_t)(((uint64_t)corrected_dot_z_wide + (uint64_t)corrected_dot_x_wide + (uint64_t)corrected_dot_y_wide) >> PHYSICS_FIXED_FRACTION_BITS);
152
153 if (corrected_normal_velocity < 0)
154 {
155 int64_t inward_velocity_x_wide = (int64_t)corrected_normal_velocity * (int32_t)record->contact_normal_q12.x;
156 int64_t inward_velocity_y_wide = (int64_t)corrected_normal_velocity * (int32_t)record->contact_normal_q12.y;
157 int64_t inward_velocity_z_wide = (int64_t)corrected_normal_velocity * (int32_t)record->contact_normal_q12.z;
158
159 response_x -= (int32_t)((uint64_t)inward_velocity_x_wide >> PHYSICS_FIXED_FRACTION_BITS);
160 response_y -= (int32_t)((uint64_t)inward_velocity_y_wide >> PHYSICS_FIXED_FRACTION_BITS);
161 response_z -= (int32_t)((uint64_t)inward_velocity_z_wide >> PHYSICS_FIXED_FRACTION_BITS);
162 }
163 }
164
165 int64_t response_x_squared_wide = (int64_t)response_x * response_x;
166 int64_t response_y_squared_wide = (int64_t)response_y * response_y;
167 int64_t response_z_squared_wide = (int64_t)response_z * response_z;
168 int32_t response_speed =
169 Math_SqrtFP12((int32_t)(((uint64_t)response_z_squared_wide + (uint64_t)response_x_squared_wide + (uint64_t)response_y_squared_wide) >> PHYSICS_FIXED_FRACTION_BITS));
170 int32_t platform_speed_limit_bits = (int32_t)pending_ground_actor->scene_anim_data;
171
172 if (response_speed <= platform_speed_limit_bits)
173 velocity_z = response_z;
174 else
175 {
176 int32_t speed_clamp_ratio_q12 = ((int64_t)platform_speed_limit_bits * PHYSICS_FIXED_ONE) / response_speed;
177 int64_t clamped_response_x_wide = (int64_t)speed_clamp_ratio_q12 * response_x;
178
179 response_x = (int32_t)((uint64_t)clamped_response_x_wide >> PHYSICS_FIXED_FRACTION_BITS);
180
181 int64_t clamped_response_y_wide = (int64_t)speed_clamp_ratio_q12 * response_y;
182
183 response_y = (int32_t)((uint64_t)clamped_response_y_wide >> PHYSICS_FIXED_FRACTION_BITS);
184
185 int64_t clamped_response_z_wide = (int64_t)speed_clamp_ratio_q12 * response_z;
186
187 velocity_z = (int32_t)((uint64_t)clamped_response_z_wide >> PHYSICS_FIXED_FRACTION_BITS);
188 }
189
190 velocity_y = response_y;
191 velocity_x = response_x;
192 }
193
194 actor->velocity = (struct Math_Vec3I32){
195 .x = velocity_x,
196 .y = velocity_y,
197 .z = velocity_z};
198
200 {
201 uint8_t configured_jump_hold_ticks = record->configured_jump_hold_ticks;
202
203 record->jump_cache_ticks = 0;
204 record->jump_hold_ticks = configured_jump_hold_ticks + 1;
205
206 if (!(record->input_flags & g_inputLandingFlagsPtr[3]))
207 {
208 record->jump_cache_ticks = 0;
209 record->jump_hold_ticks = 0;
210 }
211 else
212 record->jump_cache_ticks = UINT8_MAX;
213
215 {
216 int32_t landing_sound_offset = -(((uint8_t)record->jump_cache_ticks != UINT8_MAX ? 1 : 0));
217
218 landing_sound_offset &= ~ACTOR_SOUND_VARIANT_LOW_BIT;
219 Audio_PlaySound3D(&g_soundDefinitionTable[landing_sound_offset + ACTOR_LANDING_SOUND_BASE_INDEX], &actor->position);
220 }
221 }
222
223 record->pending_state_2c = 0;
224
225 return ((pending_ground_actor->actor_type == ACTOR_OBJECT_RUNTIME && pending_ground_actor != actor) ? -1 : 1);
226}
227
228// Original pcdogs.exe: 0x436bc0
229// Computes slope- and collision-volume-adjusted jump velocity from the ground normal. With no ground it returns zero; normal Y at or below 0x800 is returned unchanged, and the deferred modifier slot is restored after lookup.
231{
232 struct Collision_Node *ground_collision_node = actor->ground_collision_node;
233
234 if (!ground_collision_node)
235 return 0;
236
237 struct Pkg_CollisionFacePlane *face_plane = actor->ground_contact_polygon->face_plane;
238 int32_t ground_normal_up_q12 = ((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)face_plane->normal.x * (int32_t)ground_collision_node->transform_matrix.m10) >> PHYSICS_FIXED_FRACTION_BITS;
239
240 if (ground_normal_up_q12 <= PHYSICS_FIXED_HALF)
241 return ground_normal_up_q12;
242
243 struct Player_ControllerRecordView *controller_record = actor->record_ptr;
244 int32_t jump_velocity;
245
246 if (ground_normal_up_q12 <= COLLISION_GROUND_NORMAL_MIN_Q12)
247 {
248 int64_t slope_jump_product_wide = (int64_t)ground_normal_up_q12 * controller_record->ground_jump_velocity;
249
250 jump_velocity = (int32_t)((uint64_t)slope_jump_product_wide >> PHYSICS_FIXED_FRACTION_BITS);
251 }
252 else
253 jump_velocity = controller_record->ground_jump_velocity;
254
255 struct Actor_State *saved_jump_modifier_slot = controller_record->collision_response_slots_0_to_4[3];
256 struct Physics_SpeedJumpModifierView *jump_modifier =
258
259 if (!jump_modifier)
260 {
261 controller_record->collision_response_slots_0_to_4[3] = saved_jump_modifier_slot;
262
263 return jump_velocity;
264 }
265
266 int64_t modified_jump_product_wide = (int64_t)jump_modifier->jump_multiplier_q12 * jump_velocity;
267
268 controller_record->collision_response_slots_0_to_4[3] = saved_jump_modifier_slot;
269
270 return (int32_t)((uint64_t)modified_jump_product_wide >> PHYSICS_FIXED_FRACTION_BITS);
271}
#define ACTOR_BEHAVIOR_AIRBORNE
Definition constants.h:13
#define ACTOR_BEHAVIOR_WORKLIST_MEMBER
Definition constants.h:34
@ ACTOR_OBJECT_RUNTIME
Definition constants.h:143
@ ACTOR_OBJECT_FORCE_EMITTER
Definition constants.h:144
#define COLLISION_GROUND_NORMAL_MIN_Q12
Definition constants.h:50
struct Actor_State * Collision_GetValidCollisionTarget(struct Actor_State *actor, int32_t collision_slot)
Definition actors.c:868
int32_t Math_SqrtFP12(uint32_t value)
#define PHYSICS_GROUND_NEAR_POSITION_SQUARED
Definition constants.h:37
#define PHYSICS_FIXED_ONE
Definition constants.h:7
#define PHYSICS_FIXED_FRACTION_BITS
Definition constants.h:6
#define PHYSICS_FIXED_HALF
Definition constants.h:38
bool Physics_CheckGroundSlopeDirection(struct Actor_State *actor, struct Collision_Polygon *collision_poly, struct Math_Vec3I16 *out_normal, struct Math_Vec3I32 *delta_vec)
Definition ground.c:34
int32_t Physics_ApplyGroundReaction(struct Actor_State *actor, struct Physics_GroundReactionRecordView *record)
Definition ground.c:71
int32_t Physics_CalculateGroundJumpVelocity(struct Actor_State *actor)
Definition ground.c:230
struct Math_Vec3I32 velocity
Definition types.h:446
struct Mesh_RuntimeVertex * runtime_vertex_array
Definition types.h:397
uint8_t actor_type
Definition types.h:391
struct Collision_Polygon * ground_contact_polygon
Definition types.h:452
struct Collision_Node * ground_collision_node
Definition types.h:451
int32_t behavior_flags
Definition types.h:407
void * scene_anim_data
Definition types.h:398
void * record_ptr
Definition types.h:477
struct Math_Matrix3x3I16 rot_mat
Definition types.h:383
struct Math_Matrix3x3I16 transform_matrix
Definition types.h:281
struct Pkg_CollisionFacePlane * face_plane
Definition types.h:62
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 y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472
struct Actor_State * pending_ground_actor
Definition types.h:73
struct Math_Vec3I32 relative_contact_velocity_q12
Definition types.h:68
struct Math_Vec3I16 contact_normal_q12
Definition types.h:66
struct Math_Vec3I16 normal
Definition types.h:1135
struct Actor_State * collision_response_slots_0_to_4[0x5]
Definition types.h:183