45 double damp_factor = 1.0 / (1.0 + (sys->
damping * dt));
116 const unsigned int subSteps = (dt / unit) + 1;
117 const double sub_dt = gm_dt() / subSteps;
118 const unsigned count = gm_system_size(sys);
120 for (
int i = 0; i < subSteps; i++) {
121 for (
int j = 0; j < count; j++) {
125 for (
int j = 0; j < count; j++) {
126 for (
int k = j + 1; k < count; k++) {
133 if (collision != NULL) {
134 collision->
sys = sys;
147 if (gm_collision_bodies_are(prevC, newC->
bodies[0], newC->
bodies[1])) {
161 free(prevCollisions);
178 if (gm_collision_bodies_are(coll, a, b)) {
179 if (collision != NULL)
192static inline void gm_system_update_unit(
gmSystem *sys,
double unit) {
205static inline void gm_system_update(
gmSystem *sys) {
226 double penetration_depth;
232 double distance = sqrt(dx * dx + dy * dy);
238 if (penetration_depth > 0 && normal_x != NULL && normal_y != NULL) {
239 *normal_x = dx / distance;
240 *normal_y = dy / distance;
247 double overlap_x = (a->
width / 2 + b->
width / 2) - fabs(dx);
250 double overlap_y = (a->
height / 2 + b->
height / 2) - fabs(dy);
252 if (normal_x != NULL && normal_y != NULL) {
253 if (overlap_x < overlap_y) {
254 penetration_depth = overlap_x;
255 *normal_x = (dx < 0) ? -1 : 1;
258 penetration_depth = overlap_y;
260 *normal_y = (dy < 0) ? -1 : 1;
271 double half_w = rect->
width * 0.5;
272 double half_h = rect->
height * 0.5;
283 double dx = circle->
position.
x - closest_x;
284 double dy = circle->
position.
y - closest_y;
285 double distance_sq = dx * dx + dy * dy;
288 if (distance_sq < circle->radius * circle->
radius) {
289 double distance = sqrt(distance_sq);
292 if (distance > 0.0001 && normal_x != NULL && normal_y != NULL) {
294 *normal_x = dx / distance;
295 *normal_y = dy / distance;
296 penetration_depth = circle->
radius - distance;
306 double min_x = fmin(left_pen, right_pen);
307 double min_y = fmin(bottom_pen, top_pen);
311 if (normal_x != NULL && normal_y != NULL) {
313 penetration_depth = min_x + circle->
radius;
314 *normal_x = (left_pen < right_pen) ? -1 : 1;
317 penetration_depth = min_y + circle->
radius;
319 *normal_y = (bottom_pen < top_pen) ? -1 : 1;
330 *normal_x = -*normal_x;
331 *normal_y = -*normal_y;
336 return penetration_depth;
369 double vel_along_normal = rel_vx * coll->
normals.
x + rel_vy * coll->
normals.
y;
372 if (vel_along_normal > 0) {
377 double j = -(1 + e) * vel_along_normal;
379 double inv_mass_a = (a->
mass > 0) ? 1.0 / a->
mass : 0;
380 double inv_mass_b = (b->
mass > 0) ? 1.0 / b->
mass : 0;
382 if (inv_mass_a + inv_mass_b == 0)
385 j /= (inv_mass_a + inv_mass_b);
387 double impulse_x = j * coll->
normals.
x;
388 double impulse_y = j * coll->
normals.
y;
400 const double percent = 0.2;
401 const double slop = 0.01;
402 double correction_amount =
403 fmax(coll->
penetration - slop, 0.0) / (inv_mass_a + inv_mass_b) * percent;
405 double correction_x = correction_amount * coll->
normals.
x;
406 double correction_y = correction_amount * coll->
normals.
y;
409 a->
position.
x -= inv_mass_a * correction_x;
410 a->
position.
y -= inv_mass_a * correction_y;
413 b->
position.
x += inv_mass_b * correction_x;
414 b->
position.
y += inv_mass_b * correction_y;
@ GM_COLLIDER_RECT
Definition body.h:13
@ GM_COLLIDER_CIRCLE
Definition body.h:12
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:223
gmPtrList gm_ptr_list_push(gmPtrList list, void *obj)
Adds a pointer to the end of the list.
Definition body_list.h:73
void ** gmPtrList
A dynamic, NULL-terminated array of generic pointers.
Definition body_list.h:25
gmCollision * gm_collision_detect(gmBody *, gmBody *)
Detects collision between two bodies.
Definition collision.h:59
void gm_system_update_dt(gmSystem *sys, double unit, double dt)
Updates the physics system with collision detection at specified time intervals.
Definition physics.h:109
double gm_collision_penetration_normals(gmBody *a, gmBody *b, double *normal_x, double *normal_y)
Calculates the penetration depth and normal vector for a collision between two bodies.
Definition physics.h:224
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:174
double gm_system_frame_time
Default time step for physics system frame updates.
Definition physics.h:199
void gm_system_update_body_dt(gmSystem *sys, gmBody *body, double dt)
Updates a single body in the system by integrating its position and velocity over time.
Definition physics.h:29
void gm_body_update_dt(gmBody *body, double dt)
Updates a single body by integrating its position and velocity over time.
Definition physics.h:67
void gm_collision_resolve(gmCollision *collision)
Resolves a collision between two bodies by applying appropriate forces and corrections.
Definition physics.h:354
void gm_body_update(gmBody *body)
Updates a single body by integrating its position and velocity over time.
Definition physics.h:76
double gm_collision_penetration(gmBody *a, gmBody *b)
Calculates the penetration depth for a collision between two bodies.
Definition physics.h:345
gmPos acceleration
Definition system.h:30
double damping
Definition system.h:32
gmCollision ** collisions
Definition system.h:27
gmPos velocity
Definition system.h:29
int is_active
Definition system.h:24
gmBodies bodies
Definition system.h:25
Structure representing a physics body with properties for collision and movement.
Definition body.h:19
double width
Definition body.h:28
gmPos acceleration
Definition body.h:26
gmColliderType collider_type
Definition body.h:23
double height
Definition body.h:28
double restitution
Definition body.h:30
uint8_t is_active
Definition body.h:20
double radius
Definition body.h:28
double mass
Definition body.h:29
gmPos velocity
Definition body.h:25
gmPos position
Definition body.h:24
uint8_t is_static
Definition body.h:21
Structure representing a collision between two bodies.
Definition system.h:10
gmPos normals
Definition system.h:16
double since
Definition system.h:15
gmBody * bodies[2]
Definition system.h:13
double penetration
Definition system.h:14
struct gm_system * sys
Definition system.h:11
double x
Definition position.h:5
double y
Definition position.h:5
struct gm_system gmSystem
Structure representing a physics system containing bodies and collision information.