Gama C Library
Gama C API Documentation
Loading...
Searching...
No Matches
system.h
Go to the documentation of this file.
1#pragma once
2
3#include "body.h"
4#include "body_list.h"
5#include "position.h"
6
7/**
8 * @brief Structure representing a collision between two bodies.
9 */
10typedef struct {
11 struct gm_system
12 *sys; /**< Pointer to the system where the collision occurred */
13 gmBody *bodies[2]; /**< Array containing the two colliding bodies */
14 double penetration; /**< Depth of penetration between the bodies */
15 double since; /**< Time since the collision began */
16 gmPos normals; /**< Normal vector of the collision */
18
19/**
20 * @brief Structure representing a physics system containing bodies and
21 * collision information.
22 */
23typedef struct gm_system {
24 int is_active; /**< Whether the system is active */
25 gmBodies bodies; /**< List of bodies in the system */
26
27 gmCollision **collisions; /**< Array of collision information */
28
29 gmPos velocity; /**< Velocity applied to all bodies in the system */
30 gmPos acceleration; /**< Acceleration applied to all bodies in the system */
31
32 double damping; /**< Damping factor applied to all bodies in the system */
34
35/**
36 * @brief Creates a new physics system with default values.
37 * @return A new gmSystem instance.
38 */
40 gmSystem sys = {.is_active = 1,
41 .bodies = NULL,
42 .velocity = {0, 0},
43 .acceleration = {0, 0},
44 .damping = 0,
45 .collisions = NULL};
46 return sys;
47}
48
49/**
50 * @brief Adds a body to the physics system.
51 * @param sys Pointer to the system to add to.
52 * @param body Pointer to the body to add.
53 */
54static inline void gm_system_push(gmSystem *sys, gmBody *body) {
55 sys->bodies = gm_bodies_push(sys->bodies, body);
56}
57
58/**
59 * @brief Adds two bodies to the physics system.
60 * @param sys Pointer to the system to add to.
61 * @param a Pointer to the first body to add.
62 * @param b Pointer to the second body to add.
63 */
64static inline void gm_system_push2(gmSystem *sys, gmBody *a, gmBody *b) {
65 gm_system_push(sys, a);
66 gm_system_push(sys, b);
67}
68
69/**
70 * @brief Adds three bodies to the physics system.
71 * @param sys Pointer to the system to add to.
72 * @param a Pointer to the first body to add.
73 * @param b Pointer to the second body to add.
74 * @param c Pointer to the third body to add.
75 */
76static inline void gm_system_push3(gmSystem *sys, gmBody *a, gmBody *b,
77 gmBody *c) {
78 gm_system_push(sys, a);
79 gm_system_push(sys, b);
80 gm_system_push(sys, c);
81}
82
83/**
84 * @brief Adds four bodies to the physics system.
85 * @param sys Pointer to the system to add to.
86 * @param a Pointer to the first body to add.
87 * @param b Pointer to the second body to add.
88 * @param c Pointer to the third body to add.
89 * @param d Pointer to the fourth body to add.
90 */
91static inline void gm_system_push4(gmSystem *sys, gmBody *a, gmBody *b,
92 gmBody *c, gmBody *d) {
93 gm_system_push(sys, a);
94 gm_system_push(sys, b);
95 gm_system_push(sys, c);
96 gm_system_push(sys, d);
97}
98
99/**
100 * @brief Adds five bodies to the physics system.
101 * @param sys Pointer to the system to add to.
102 * @param a Pointer to the first body to add.
103 * @param b Pointer to the second body to add.
104 * @param c Pointer to the third body to add.
105 * @param d Pointer to the fourth body to add.
106 * @param e Pointer to the fifth body to add.
107 */
108static inline void gm_system_push5(gmSystem *sys, gmBody *a, gmBody *b,
109 gmBody *c, gmBody *d, gmBody *e) {
110 gm_system_push(sys, a);
111 gm_system_push(sys, b);
112 gm_system_push(sys, c);
113 gm_system_push(sys, d);
114 gm_system_push(sys, e);
115}
116
117/**
118 * @brief Adds an array of bodies to the physics system.
119 * @param sys Pointer to the system to add to.
120 * @param number The number of bodies to add.
121 * @param bodies Pointer to the array of bodies to add.
122 */
123static inline void gm_system_push_array(gmSystem *sys, size_t number,
124 gmBody *bodies) {
125 for (size_t i = 0; i < number; i++)
126 gm_system_push(sys, &bodies[i]);
127}
128
129/**
130 * @brief Removes the last body from the physics system.
131 * @param sys Pointer to the system to remove from.
132 */
133static inline void gm_system_pop(gmSystem *sys) {
134 sys->bodies = gm_bodies_pop(sys->bodies);
135}
136
137/**
138 * @brief Gets the number of bodies in the system.
139 * @param sys Pointer to the system to check.
140 * @return The number of bodies in the system.
141 */
142static inline size_t gm_system_size(gmSystem *sys) {
143 return gm_bodies_length(sys->bodies);
144}
145
146/**
147 * @brief Destroy the system and free memory.
148 * @param sys Pointer to the system to destroy.
149 */
151 if (sys->collisions != NULL) {
152 for (size_t i = 0; sys->collisions[i] != NULL; i++) {
153 free(sys->collisions[i]);
154 }
155 free(sys->collisions);
156 }
157}
Provides a dynamic, NULL-terminated pointer list implementation.
gmBody ** gmBodies
A specialized pointer list for gmBody pointers.
Definition body_list.h:233
Structure representing a physics system containing bodies and collision information.
Definition system.h:23
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
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
Definition position.h:4
gmSystem gm_system_create()
Creates a new physics system with default values.
Definition system.h:39
void gm_system_destroy(gmSystem *sys)
Destroy the system and free memory.
Definition system.h:150
struct gm_system gmSystem
Structure representing a physics system containing bodies and collision information.