50 double damp_factor = 1.0 / (1.0 + (sys->
damping * dt));
132 const unsigned int subSteps = (dt / unit) + 1;
133 const double sub_dt = gm_dt() / subSteps;
134 const unsigned count = gm_system_size(sys);
136 for (
int i = 0; i < subSteps; i++) {
137 for (
int j = 0; j < count; j++) {
141 for (
int j = 0; j < count; j++) {
142 for (
int k = j + 1; k < count; k++) {
149 if (collision != NULL) {
150 collision->
sys = sys;
163 if (gm_collision_bodies_are(prevC, newC->
bodies[0], newC->
bodies[1])) {
177 free(prevCollisions);
199 if (gm_collision_bodies_are(coll, a, b)) {
200 if (collision != NULL)
214static inline void gm_system_update_unit(
gmSystem *sys,
double unit) {
230static inline void gm_system_update(
gmSystem *sys) {
257 double penetration_depth = 0;
264 double distance =
sqrt(dx * dx + dy * dy);
270 if (penetration_depth > 0 && normal_x != NULL && normal_y != NULL) {
271 *normal_x = dx / distance;
272 *normal_y = dy / distance;
282 if (overlap_x <= 0)
return 0;
285 if (overlap_y <= 0)
return 0;
287 if (normal_x != NULL && normal_y != NULL) {
288 if (overlap_x < overlap_y) {
289 penetration_depth = overlap_x;
290 *normal_x = (dx < 0) ? -1 : 1;
293 penetration_depth = overlap_y;
295 *normal_y = (dy < 0) ? -1 : 1;
305 double half_w = rect->
width * 0.5;
306 double half_h = rect->
height * 0.5;
317 double dx = circle->
position.
x - closest_x;
318 double dy = circle->
position.
y - closest_y;
319 double distance_sq = dx * dx + dy * dy;
322 if (distance_sq < circle->radius * circle->
radius) {
323 double distance =
sqrt(distance_sq);
326 if (distance > 0.0001 && normal_x != NULL && normal_y != NULL) {
328 *normal_x = dx / distance;
329 *normal_y = dy / distance;
330 penetration_depth = circle->
radius - distance;
340 double min_x =
fmin(left_pen, right_pen);
341 double min_y =
fmin(bottom_pen, top_pen);
345 if (normal_x != NULL && normal_y != NULL) {
347 penetration_depth = min_x + circle->
radius;
348 *normal_x = (left_pen < right_pen) ? -1 : 1;
351 penetration_depth = min_y + circle->
radius;
353 *normal_y = (bottom_pen < top_pen) ? -1 : 1;
364 *normal_x = -*normal_x;
365 *normal_y = -*normal_y;
370 return penetration_depth;
408 double vel_along_normal = rel_vx * coll->
normals.
x + rel_vy * coll->
normals.
y;
411 if (vel_along_normal > 0) {
416 double j = -(1 + e) * vel_along_normal;
418 double inv_mass_a = (a->
mass > 0) ? 1.0 / a->
mass : 0;
419 double inv_mass_b = (b->
mass > 0) ? 1.0 / b->
mass : 0;
421 if (inv_mass_a + inv_mass_b == 0)
424 j /= (inv_mass_a + inv_mass_b);
426 double impulse_x = j * coll->
normals.
x;
427 double impulse_y = j * coll->
normals.
y;
439 const double percent = 0.2;
440 const double slop = 0.01;
441 double correction_amount =
442 fmax(coll->
penetration - slop, 0.0) / (inv_mass_a + inv_mass_b) * percent;
444 double correction_x = correction_amount * coll->
normals.
x;
445 double correction_y = correction_amount * coll->
normals.
y;
448 a->
position.
x -= inv_mass_a * correction_x;
449 a->
position.
y -= inv_mass_a * correction_y;
452 b->
position.
x += inv_mass_b * correction_x;
453 b->
position.
y += inv_mass_b * correction_y;
@ GM_COLLIDER_RECT
Definition body.h:18
@ GM_COLLIDER_CIRCLE
Definition body.h:17
Provides a dynamic, NULL-terminated pointer list implementation.
#define gm_ptr_list_for_each(item, list)
A macro for iterating over a gmPtrList.
Definition body_list.h:232
gmPtrList gm_ptr_list_push(gmPtrList list, void *obj)
Adds a pointer to the end of the list.
Definition body_list.h:78
void ** gmPtrList
A dynamic, NULL-terminated array of generic void* pointers.
Definition body_list.h:29
Defines collision structures and provides functions for 2D collision detection.
struct gm_collision gmCollision
Structure to store detailed information about a collision between two bodies.
Graphics API (GAPI) abstraction layer for Gama.
void free(void *ptr)
Custom implementation of free for memory allocated by malloc (this custom version).
Definition malloc.h:189
double fmin(double a, double b)
Returns the smaller of two double values.
Definition math.h:413
double fmax(double a, double b)
Returns the larger of two double values.
Definition math.h:428
double fabs(double x)
Calculates the absolute value of a double.
Definition math.h:369
double sqrt(double x)
Calculates the square root of x.
Definition math.h:339
gmCollision * gm_collision_detect(gmBody *, gmBody *)
Detects a collision between two physics bodies.
Definition collision.h:110
void gm_system_update_dt(gmSystem *sys, double unit, double dt)
Updates a physics system over a given total time step, performing sub-steps for stable collision dete...
Definition physics.h:125
double gm_collision_penetration_normals(gmBody *a, gmBody *b, double *normal_x, double *normal_y)
Calculates the penetration depth and optionally the normal vector for a collision between two bodies.
Definition physics.h:255
int gm_system_get_collision(gmCollision *collision, gmSystem *sys, gmBody *a, gmBody *b)
Gets the collision information for two specific bodies in a system.
Definition physics.h:195
double gm_system_frame_time
Default time step for physics system frame updates.
Definition physics.h:224
void gm_system_update_body_dt(gmSystem *sys, gmBody *body, double dt)
Updates a single physics body's position and velocity based on applied accelerations and damping.
Definition physics.h:34
void gm_body_update_dt(gmBody *body, double dt)
Updates a single physics body's position and velocity using a specified time step,...
Definition physics.h:72
void gm_collision_resolve(gmCollision *collision)
Resolves a collision between two bodies by applying appropriate forces and corrections.
Definition physics.h:393
void gm_body_update(gmBody *body)
Updates a single physics body's position and velocity using the engine's global delta time (gm_dt()),...
Definition physics.h:81
double gm_collision_penetration(gmBody *a, gmBody *b)
Calculates the penetration depth for a collision between two bodies.
Definition physics.h:384
gmPos normals
Definition collision.h:20
double since
Definition collision.h:22
double penetration
Definition collision.h:21
gmBody * bodies[2]
Definition collision.h:19
gmSystem * sys
Definition collision.h:23
gmPos acceleration
Definition system.h:27
double damping
Definition system.h:29
gmPos velocity
Definition system.h:26
struct gm_collision ** collisions
Definition system.h:24
int is_active
Definition system.h:21
gmBodies bodies
Definition system.h:22
Structure representing a physics body with properties for collision and movement.
Definition body.h:25
double width
Definition body.h:35
gmPos acceleration
Definition body.h:33
gmColliderType collider_type
Definition body.h:30
double height
Definition body.h:35
double restitution
Definition body.h:39
uint8_t is_active
Definition body.h:27
double radius
Definition body.h:35
double mass
Definition body.h:38
gmPos velocity
Definition body.h:32
gmPos position
Definition body.h:31
uint8_t is_static
Definition body.h:28
double x
Definition position.h:9
double y
Definition position.h:9
Manages physics bodies, their interactions, and collision detection within a simulation.
struct gm_system gmSystem
Structure representing a physics system containing bodies and collision information.