Gama C Library
Gama C API Documentation
physics.h File Reference
#include "_math.h"
#include "body.h"
#include "body_list.h"
#include "collision.h"
#include "gapi.h"
#include "position.h"
#include "system.h"

Go to the source code of this file.

Functions

void gm_collision_resolve (gmCollision *collision)
 Resolves a collision between two bodies by applying appropriate forces and corrections.
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.
void gm_body_update_dt (gmBody *body, double dt)
 Updates a single physics body's position and velocity using a specified time step, without considering a global physics system.
void gm_body_update (gmBody *body)
 Updates a single physics body's position and velocity using the engine's global delta time (gm_dt()), without considering a global physics system.
gmCollisiongm_collision_detect (gmBody *, gmBody *)
 Detects a collision between two physics bodies.
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 detection and resolution.
int gm_system_get_collision (gmCollision *collision, gmSystem *sys, gmBody *a, gmBody *b)
 Gets the collision information for two specific bodies in a system.
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.
double gm_collision_penetration (gmBody *a, gmBody *b)
 Calculates the penetration depth for a collision between two bodies.

Variables

double gm_system_frame_time = 0.001
 Default time step for physics system frame updates.

Function Documentation

◆ gm_body_update()

void gm_body_update ( gmBody * body)

Updates a single physics body's position and velocity using the engine's global delta time (gm_dt()), without considering a global physics system.

Parameters
bodyPointer to the body to update.

◆ gm_body_update_dt()

void gm_body_update_dt ( gmBody * body,
double dt )

Updates a single physics body's position and velocity using a specified time step, without considering a global physics system.

Parameters
bodyPointer to the body to update.
dtThe time step for the update.

◆ gm_collision_detect()

gmCollision * gm_collision_detect ( gmBody * a,
gmBody * b )

Detects a collision between two physics bodies.

Parameters
aPointer to the first body.
bPointer to the second body.
Returns
A pointer to a gmCollision structure if a collision is detected, otherwise NULL. The returned gmCollision must be freed by the caller if it's not managed by a gmSystem.

Detects a collision between two physics bodies.

This function dispatches to specific collision tests (e.g., AABB vs AABB, Circle vs Circle, Circle vs AABB) based on the collider_type of the input bodies.

Parameters
aPointer to the first body.
bPointer to the second body.
Returns
A dynamically allocated gmCollision structure if a collision occurs, otherwise NULL. The caller is responsible for freeing the returned gmCollision object if it's not managed by a gmSystem.

◆ gm_collision_penetration()

double gm_collision_penetration ( gmBody * a,
gmBody * b )

Calculates the penetration depth for a collision between two bodies.

This function is a simplified version of gm_collision_penetration_normals that only returns the depth and does not calculate the normal vector.

Parameters
aPointer to the first body.
bPointer to the second body.
Returns
The penetration depth between the bodies. A positive value indicates overlap.

◆ gm_collision_penetration_normals()

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.

This function handles collision between various collider types (Circle-Circle, Rect-Rect, Circle-Rect). It determines how much the bodies overlap and the direction of the separation.

Parameters
aPointer to the first body.
bPointer to the second body.
normal_xPointer to store the x component of the collision normal (can be NULL if not needed). The normal points from body a to body b.
normal_yPointer to store the y component of the collision normal (can be NULL if not needed). The normal points from body a to body b.
Returns
The penetration depth between the bodies. A positive value indicates overlap.

◆ gm_collision_resolve()

void gm_collision_resolve ( gmCollision * coll)

Resolves a collision between two bodies by applying appropriate forces and corrections.

Resolves a collision by adjusting positions and velocities of colliding bodies.

Parameters
collisionPointer to the collision to resolve.
collPointer to the collision to resolve.

◆ gm_system_get_collision()

int gm_system_get_collision ( gmCollision * collision,
gmSystem * sys,
gmBody * a,
gmBody * b )

Gets the collision information for two specific bodies in a system.

This function searches the system's active collisions to find one involving the two specified bodies.

Parameters
collisionPointer to a gmCollision struct where the found collision data will be copied. Can be NULL if only checking for existence.
sysPointer to the system to search in.
aPointer to the first body.
bPointer to the second body.
Returns
1 if a collision involving a and b is found, 0 otherwise.

◆ gm_system_update_body_dt()

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.

This function integrates the body's motion over a given time step. It accounts for the body's own acceleration and any system-wide acceleration and damping.

Parameters
sysPointer to the physics system the body belongs to (can be NULL if no system-wide effects are desired).
bodyPointer to the body to update.
dtThe time step (delta time) for the update.

◆ gm_system_update_dt()

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 detection and resolution.

This is the main update function for a physics system. It integrates the motion of all bodies, detects new collisions, resolves them, and manages the lifecycle of collision objects.

Parameters
sysPointer to the system to update.
unitThe duration of each sub-step for physics integration.
dtThe total time duration to simulate in this update.

Variable Documentation

◆ gm_system_frame_time

double gm_system_frame_time = 0.001

Default time step for physics system frame updates.

This value determines the granularity of physics calculations per frame. A smaller value leads to more accurate (and potentially slower) simulations.