64 .position = {.x = x, .y = y},
65 .velocity = {.x = 0, .y = 0},
66 .acceleration = {.x = 0, .y = 0},
69 .radius = w < h ? w : h,
86 double current_speed = gm_pos_magniture(body->
velocity);
87 if (current_speed > max_speed) {
88 double factor = max_speed / current_speed;
105 double current_speed = gm_pos_magniture(body->
velocity);
106 if (current_speed < min_speed) {
108 if (current_speed == 0)
111 double factor = min_speed / current_speed;
127 double current_speed = gm_pos_magniture(body->
velocity);
128 if (current_speed == speed)
131 if (current_speed == 0) {
136 double factor = speed / current_speed;
155 void animator(
double *value,
double target,
double dt,
157 double dt,
double t) {
158 double current_speed = gm_pos_magniture(body->
velocity);
159 if (current_speed > max_speed) {
160 double factor = max_speed / current_speed;
161 double x_target = body->
velocity.
x * factor;
162 double y_target = body->
velocity.
y * factor;
163 animator(&body->
velocity.
x, x_target, dt, t);
164 animator(&body->
velocity.
y, y_target, dt, t);
181 void animator(
double *value,
double target,
double dt,
183 double dt,
double t) {
184 double current_speed = gm_pos_magniture(body->
velocity);
185 if (current_speed < min_speed) {
187 if (current_speed == 0)
189 double factor = min_speed / current_speed;
190 double x_target = body->
velocity.
x * factor;
191 double y_target = body->
velocity.
y * factor;
192 animator(&body->
velocity.
x, x_target, dt, t);
193 animator(&body->
velocity.
y, y_target, dt, t);
211 void animator(
double *value,
double target,
double dt,
213 double dt,
double t) {
214 double current_speed = gm_pos_magniture(body->
velocity);
215 if (current_speed != speed) {
216 double x_target, y_target;
217 if (current_speed == 0) {
222 double factor = speed / current_speed;
226 animator(&body->
velocity.
x, x_target, dt, t);
227 animator(&body->
velocity.
y, y_target, dt, t);
270static inline int gm_hovered(
gmBody *body) {
279static inline int gm_clicked(
gmBody *body) {
280 return gm_mouse.clicked && gm_hovered(body);
301static inline uint8_t gm_body_bound_clip(
gmBody *body,
double bx,
double ex,
302 double by,
double ey) {
305 if (bx != 0 || ex != 0) {
314 if (by != 0 || ey != 0) {
343static inline int8_t gm_body_bound_reflect(
gmBody *body,
double bx,
double ex,
344 double by,
double ey) {
382static inline int8_t gm_body_bound_bounce(
gmBody *body,
double bx,
double ex,
383 double by,
double ey,
384 double restitution) {
void gm_speed_anim(gmBody *body, double speed, void animator(double *value, double target, double dt, double t), double dt, double t)
Sets the speed of a body using an animation function, preserving its direction.
Definition body.h:210
void gm_max_speed(gmBody *body, double max_speed)
Limits the maximum speed of a body.
Definition body.h:85
gmBody gm_rectangle_body(double m, double x, double y, double w, double h)
Creates a rectangular physics body.
Definition body.h:240
void gm_speed(gmBody *body, double speed)
Sets the speed of a body while preserving its current direction.
Definition body.h:126
gmBody gm_body_create(double mass, double x, double y, double w, double h, gmColliderType c)
Creates a new physics body with specified properties.
Definition body.h:58
void gm_min_speed(gmBody *body, double min_speed)
Sets the minimum speed of a body.
Definition body.h:104
gmColliderType
Enum to define the type of collider for a physics body.
Definition body.h:16
@ GM_COLLIDER_RECT
Definition body.h:18
@ GM_COLLIDER_CIRCLE
Definition body.h:17
gmBody gm_circle_body(double m, double x, double y, double r)
Creates a circular physics body.
Definition body.h:252
int gm_body_contains(gmBody *body, double x, double y)
Checks if a point is contained within a body's collider.
Definition collision.h:151
void gm_min_speed_anim(gmBody *body, double min_speed, void animator(double *value, double target, double dt, double t), double dt, double t)
Sets the minimum speed of a body using an animation function.
Definition body.h:180
void gm_max_speed_anim(gmBody *body, double max_speed, void animator(double *value, double target, double dt, double t), double dt, double t)
Limits the maximum speed of a body using an animation function.
Definition body.h:154
struct _gmMouse gm_mouse
Definition mouse.h:32
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 friction
Definition body.h:41
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
Represents a 2D position or vector.
Definition position.h:8
double x
Definition position.h:9
double y
Definition position.h:9