Gama C Library
Gama C API Documentation
system.h
Go to the documentation of this file.
1/**
2 * @file system.h
3 * @brief Manages physics bodies, their interactions, and collision detection within a simulation.
4 *
5 * This file defines the `gmSystem` structure and functions for creating,
6 * populating, and destroying a physics simulation environment.
7 */
8#pragma once
9
10#include "body.h"
11#include "body_list.h"
12#include "position.h"
13
14struct gm_collision ;
15
16/**
17 * @brief Structure representing a physics system containing bodies and
18 * collision information.
19 */
20typedef struct gm_system {
21 int is_active; /**< Whether the system is active and should be updated. */
22 gmBodies bodies; /**< List of pointers to bodies currently managed by this system. */
23
24 struct gm_collision **collisions; /**< Array of active collision information objects. */
25
26 gmPos velocity; /**< Global velocity applied to all bodies in the system (e.g., wind). */
27 gmPos acceleration; /**< Global acceleration applied to all bodies (e.g., gravity). */
28
29 double damping; /**< Global damping factor applied to all bodies (reduces velocity over time). */
31
32#include "collision.h" // Include collision.h for gmCollision definition
33/**
34 * @brief Creates a new physics system with default values.
35 *
36 * Initializes a `gmSystem` with no bodies, zero global velocity/acceleration,
37 * and a damping factor of 0.
38 *
39 * @return A new `gmSystem` instance.
40 */
42 gmSystem sys = {.is_active = 1,
43 .bodies = NULL,
44 .velocity = {0, 0},
45 .acceleration = {0, 0},
46 .damping = 0,
47 .collisions = NULL};
48 return sys;
49}
50
51/**
52 * @brief Adds a body to the physics system.
53 *
54 * This function adds a pointer to a `gmBody` to the system's internal list.
55 * The caller remains responsible for allocating and freeing the `gmBody` itself.
56 *
57 * @param sys Pointer to the system to add the body to.
58 * @param body Pointer to the `gmBody` to add.
59 */
60static inline void gm_system_push(gmSystem *sys, gmBody *body) {
61 sys->bodies = gm_bodies_push(sys->bodies, body);
62}
63
64/**
65 * @brief Adds two bodies to the physics system.
66 * @param sys Pointer to the system to add to.
67 * @param a Pointer to the first body to add.
68 * @param b Pointer to the second body to add.
69 */
70static inline void gm_system_push2(gmSystem *sys, gmBody *a, gmBody *b) {
71 gm_system_push(sys, a);
72 gm_system_push(sys, b);
73}
74
75/**
76 * @brief Adds three bodies to the physics system.
77 * @param sys Pointer to the system to add to.
78 * @param a Pointer to the first body to add.
79 * @param b Pointer to the second body to add.
80 * @param c Pointer to the third body to add.
81 */
82static inline void gm_system_push3(gmSystem *sys, gmBody *a, gmBody *b,
83 gmBody *c) {
84 gm_system_push(sys, a);
85 gm_system_push(sys, b);
86 gm_system_push(sys, c);
87}
88
89/**
90 * @brief Adds four bodies to the physics system.
91 * @param sys Pointer to the system to add to.
92 * @param a Pointer to the first body to add.
93 * @param b Pointer to the second body to add.
94 * @param c Pointer to the third body to add.
95 * @param d Pointer to the fourth body to add.
96 */
97static inline void gm_system_push4(gmSystem *sys, gmBody *a, gmBody *b,
98 gmBody *c, gmBody *d) {
99 gm_system_push(sys, a);
100 gm_system_push(sys, b);
101 gm_system_push(sys, c);
102 gm_system_push(sys, d);
103}
104
105/**
106 * @brief Adds five bodies to the physics system.
107 * @param sys Pointer to the system to add to.
108 * @param a Pointer to the first body to add.
109 * @param b Pointer to the second body to add.
110 * @param c Pointer to the third body to add.
111 * @param d Pointer to the fourth body to add.
112 * @param e Pointer to the fifth body to add.
113 */
114static inline void gm_system_push5(gmSystem *sys, gmBody *a, gmBody *b,
115 gmBody *c, gmBody *d, gmBody *e) {
116 gm_system_push(sys, a);
117 gm_system_push(sys, b);
118 gm_system_push(sys, c);
119 gm_system_push(sys, d);
120 gm_system_push(sys, e);
121}
122
123/**
124 * @brief Adds an array of bodies to the physics system.
125 * @param sys Pointer to the system to add to.
126 * @param number The number of bodies in the array.
127 * @param bodies Pointer to the array of `gmBody` instances to add.
128 */
129static inline void gm_system_push_array(gmSystem *sys, size_t number,
130 gmBody *bodies) {
131 for (size_t i = 0; i < number; i++)
132 gm_system_push(sys, &bodies[i]);
133}
134
135/**
136 * @brief Removes the last body pointer from the physics system's internal list.
137 *
138 * This function only removes the pointer; it does NOT free the `gmBody` itself.
139 *
140 * @param sys Pointer to the system to remove from.
141 */
142static inline void gm_system_pop(gmSystem *sys) {
143 sys->bodies = gm_bodies_pop(sys->bodies);
144}
145
146/**
147 * @brief Gets the number of bodies currently managed by the physics system.
148 * @param sys Pointer to the system to check.
149 * @return The number of bodies in the system.
150 */
151static inline size_t gm_system_size(gmSystem *sys) {
152 return gm_bodies_length(sys->bodies);
153}
154
155/**
156 * @brief Destroys the physics system and frees its internal memory.
157 *
158 * This function frees the memory allocated for the system's internal collision
159 * list and the list of body pointers. It does NOT free the `gmBody` instances
160 * themselves, which must be managed by the caller.
161 *
162 * @param sys Pointer to the system to destroy.
163 */
165 if (sys->collisions != NULL) {
166 for (size_t i = 0; sys->collisions[i] != NULL; i++) {
167 free(sys->collisions[i]);
168 }
170 }
171 // Also clear the bodies list itself
172 gm_bodies_clear(sys->bodies);
173 // Do not free individual gmBody pointers, as they are owned by the caller.
174}
Provides a dynamic, NULL-terminated pointer list implementation.
gmBody ** gmBodies
A specialized dynamic, NULL-terminated list for gmBody pointers.
Definition body_list.h:245
Defines collision structures and provides functions for 2D collision detection.
void free(void *ptr)
Custom implementation of free for memory allocated by malloc (this custom version).
Definition malloc.h:189
Structure to store detailed information about a collision between two bodies.
Definition collision.h:18
gmBody * bodies[2]
Definition collision.h:19
gmSystem * sys
Definition collision.h:23
Structure representing a physics system containing bodies and collision information.
Definition system.h:20
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
Represents a 2D position or vector.
Definition position.h:8
gmSystem gm_system_create()
Creates a new physics system with default values.
Definition system.h:41
void gm_system_destroy(gmSystem *sys)
Destroys the physics system and frees its internal memory.
Definition system.h:164
struct gm_system gmSystem
Structure representing a physics system containing bodies and collision information.