50 .position = {.x = x, .y = y},
51 .velocity = {.x = 0, .y = 0},
52 .acceleration = {.x = 0, .y = 0},
55 .radius = w < h ? w : h,
68 double current_speed = gm_pos_magniture(body->
velocity);
69 if (current_speed > max_speed) {
70 double factor = max_speed / current_speed;
82 double current_speed = gm_pos_magniture(body->
velocity);
83 if (current_speed < min_speed) {
85 if (current_speed == 0)
88 double factor = min_speed / current_speed;
100 double current_speed = gm_pos_magniture(body->
velocity);
101 if (current_speed == speed)
104 if (current_speed == 0) {
109 double factor = speed / current_speed;
124 void animator(
double *value,
double target,
double dt,
126 double dt,
double t) {
127 double current_speed = gm_pos_magniture(body->
velocity);
128 if (current_speed > max_speed) {
129 double factor = max_speed / current_speed;
130 double x_target = body->
velocity.
x * factor;
131 double y_target = body->
velocity.
y * factor;
132 animator(&body->
velocity.
x, x_target, dt, t);
133 animator(&body->
velocity.
y, y_target, dt, t);
146 void animator(
double *value,
double target,
double dt,
148 double dt,
double t) {
149 double current_speed = gm_pos_magniture(body->
velocity);
150 if (current_speed < min_speed) {
152 if (current_speed == 0)
154 double factor = min_speed / current_speed;
155 double x_target = body->
velocity.
x * factor;
156 double y_target = body->
velocity.
y * factor;
157 animator(&body->
velocity.
x, x_target, dt, t);
158 animator(&body->
velocity.
y, y_target, dt, t);
171 void animator(
double *value,
double target,
double dt,
173 double dt,
double t) {
174 double current_speed = gm_pos_magniture(body->
velocity);
175 if (current_speed != speed) {
176 double x_target, y_target;
177 if (current_speed == 0) {
182 double factor = speed / current_speed;
186 animator(&body->
velocity.
x, x_target, dt, t);
187 animator(&body->
velocity.
y, y_target, dt, t);
230static inline int gm_hovered(
gmBody *body) {
239static inline int gm_clicked(
gmBody *body) {
240 return gm_mouse.pressed && gm_hovered(body);
252static inline uint8_t gm_body_bound_clip(
gmBody *body,
double bx,
double ex,
253 double by,
double ey) {
256 if (bx != 0 || ex != 0) {
265 if (by != 0 || ey != 0) {
286static inline int8_t gm_body_bound_reflect(
gmBody *body,
double bx,
double ex,
287 double by,
double ey) {
316static inline int8_t gm_body_bound_bounce(
gmBody *body,
double bx,
double ex,
317 double by,
double ey,
318 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.
Definition body.h:170
void gm_max_speed(gmBody *body, double max_speed)
Limits the maximum speed of a body.
Definition body.h:67
gmBody gm_rectangle_body(double m, double x, double y, double w, double h)
Creates a rectangular physics body.
Definition body.h:200
void gm_speed(gmBody *body, double speed)
Sets the speed of a body while preserving its direction.
Definition body.h:99
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:44
void gm_min_speed(gmBody *body, double min_speed)
Sets the minimum speed of a body.
Definition body.h:81
gmColliderType
Enum to define the type of collider for a physics body.
Definition body.h:11
@ GM_COLLIDER_RECT
Definition body.h:13
@ GM_COLLIDER_CIRCLE
Definition body.h:12
gmBody gm_circle_body(double m, double x, double y, double r)
Creates a circular physics body.
Definition body.h:212
int gm_body_contains(gmBody *body, double x, double y)
Checks if a point is contained within a body's collider.
Definition collision.h:89
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:145
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:123
struct _gmMouse gm_mouse
Definition gapi.h:19
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 friction
Definition body.h:31
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
double x
Definition position.h:5
double y
Definition position.h:5