2021-11-30 21:29:18 -06:00
|
|
|
#ifndef TWODPHYSICS_H
|
|
|
|
#define TWODPHYSICS_H
|
|
|
|
|
2023-01-11 16:57:34 -06:00
|
|
|
#include "script.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include <chipmunk/chipmunk.h>
|
2023-12-11 08:36:45 -06:00
|
|
|
#include "gameobject.h"
|
2023-05-24 20:45:50 -05:00
|
|
|
#include "render.h"
|
2023-11-30 10:47:59 -06:00
|
|
|
#include "transform.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
extern float phys2d_gravity;
|
|
|
|
extern int physOn;
|
|
|
|
extern cpSpace *space;
|
|
|
|
|
2023-05-16 01:31:13 -05:00
|
|
|
extern struct rgba disabled_color;
|
|
|
|
extern struct rgba dynamic_color;
|
|
|
|
extern struct rgba kinematic_color;
|
|
|
|
extern struct rgba static_color;
|
2023-05-24 20:45:50 -05:00
|
|
|
extern struct rgba sleep_color;
|
2023-05-16 01:31:13 -05:00
|
|
|
|
2024-01-14 10:24:31 -06:00
|
|
|
typedef struct constraint {
|
|
|
|
cpConstraint *c;
|
|
|
|
JSValue break_cb; /* function called when it is forcibly broken */
|
|
|
|
JSValue remove_cb; /* called when it is removed at all */
|
|
|
|
} constraint;
|
|
|
|
|
|
|
|
constraint *constraint_make(cpConstraint *c);
|
|
|
|
void constraint_break(constraint *constraint);
|
|
|
|
void constraint_free(constraint *constraint);
|
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
struct phys2d_shape {
|
2023-12-27 07:04:18 -06:00
|
|
|
cpShape *shape; /* user data is this phys2d_shape */
|
2024-04-06 19:41:14 -05:00
|
|
|
JSValue ref;
|
2024-05-12 18:36:14 -05:00
|
|
|
transform t;
|
2023-12-11 08:36:45 -06:00
|
|
|
gameobject *go;
|
2023-10-31 08:31:56 -05:00
|
|
|
void *data; /* The specific subtype; phys2d_circle, etc */
|
2023-12-28 17:38:17 -06:00
|
|
|
float (*moi)(void *data);
|
2023-09-26 17:07:51 -05:00
|
|
|
void (*apply)(void *data);
|
2023-12-22 11:50:03 -06:00
|
|
|
void (*free)(void *data);
|
2021-11-30 21:29:18 -06:00
|
|
|
};
|
|
|
|
|
2023-12-28 17:38:17 -06:00
|
|
|
void phys2d_shape_apply(struct phys2d_shape *s);
|
|
|
|
|
2023-12-19 15:34:36 -06:00
|
|
|
/* Circles are the fastest colldier type */
|
2021-11-30 21:29:18 -06:00
|
|
|
struct phys2d_circle {
|
2023-05-12 13:22:05 -05:00
|
|
|
float radius;
|
2023-11-11 20:01:42 -06:00
|
|
|
HMM_Vec2 offset;
|
2023-05-12 13:22:05 -05:00
|
|
|
struct phys2d_shape shape;
|
2021-11-30 21:29:18 -06:00
|
|
|
};
|
|
|
|
|
2024-03-20 14:32:35 -05:00
|
|
|
typedef struct phys2d_circle circle2d;
|
|
|
|
|
2023-01-25 21:32:58 -06:00
|
|
|
/* A convex polygon; defined as the convex hull around the given set of points */
|
|
|
|
struct phys2d_poly {
|
2023-11-11 20:01:42 -06:00
|
|
|
HMM_Vec2 *points;
|
2023-05-12 13:22:05 -05:00
|
|
|
float radius;
|
|
|
|
struct phys2d_shape shape;
|
2023-01-25 21:32:58 -06:00
|
|
|
};
|
|
|
|
|
2023-02-17 13:15:56 -06:00
|
|
|
/* An edge with no volume. Cannot collide with each other. Join to make levels. Static only. */
|
2021-11-30 21:29:18 -06:00
|
|
|
struct phys2d_edge {
|
2023-11-15 16:42:39 -06:00
|
|
|
HMM_Vec2 *points; /* Points defined relative to the gameobject */
|
2023-05-12 13:22:05 -05:00
|
|
|
float thickness;
|
|
|
|
cpShape **shapes;
|
|
|
|
struct phys2d_shape shape;
|
|
|
|
int draws;
|
2021-11-30 21:29:18 -06:00
|
|
|
};
|
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
struct phys2d_circle *Make2DCircle(gameobject *go);
|
2022-08-28 22:34:33 -05:00
|
|
|
void phys2d_circledel(struct phys2d_circle *c);
|
2024-03-20 14:32:35 -05:00
|
|
|
void circle2d_free(circle2d *c);
|
2021-11-30 21:29:18 -06:00
|
|
|
void phys2d_applycircle(struct phys2d_circle *circle);
|
|
|
|
void phys2d_dbgdrawcircle(struct phys2d_circle *circle);
|
2023-12-28 17:38:17 -06:00
|
|
|
float phys2d_circle_moi(struct phys2d_circle *c);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
struct phys2d_poly *Make2DPoly(gameobject *go);
|
2023-12-22 11:50:03 -06:00
|
|
|
void phys2d_poly_free(struct phys2d_poly *poly);
|
2022-08-28 22:34:33 -05:00
|
|
|
void phys2d_polydel(struct phys2d_poly *poly);
|
2021-11-30 21:29:18 -06:00
|
|
|
void phys2d_applypoly(struct phys2d_poly *poly);
|
|
|
|
void phys2d_dbgdrawpoly(struct phys2d_poly *poly);
|
|
|
|
void phys2d_polyaddvert(struct phys2d_poly *poly);
|
2023-12-21 17:21:01 -06:00
|
|
|
void phys2d_poly_setverts(struct phys2d_poly *poly, HMM_Vec2 *verts);
|
2023-12-28 17:38:17 -06:00
|
|
|
float phys2d_poly_moi(struct phys2d_poly *poly);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
struct phys2d_edge *Make2DEdge(gameobject *go);
|
2023-12-22 11:50:03 -06:00
|
|
|
void phys2d_edge_free(struct phys2d_edge *edge);
|
2022-08-28 22:34:33 -05:00
|
|
|
void phys2d_edgedel(struct phys2d_edge *edge);
|
2021-11-30 21:29:18 -06:00
|
|
|
void phys2d_applyedge(struct phys2d_edge *edge);
|
|
|
|
void phys2d_dbgdrawedge(struct phys2d_edge *edge);
|
2023-12-12 19:35:34 -06:00
|
|
|
void phys2d_edgeaddvert(struct phys2d_edge *edge, HMM_Vec2 v);
|
2023-01-25 21:32:58 -06:00
|
|
|
void phys2d_edge_rmvert(struct phys2d_edge *edge, int index);
|
2023-12-28 17:38:17 -06:00
|
|
|
float phys2d_edge_moi(struct phys2d_edge *edge);
|
2023-02-08 15:30:12 -06:00
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
void phys2d_edge_setvert(struct phys2d_edge *edge, int index, cpVect val);
|
2023-02-08 15:30:12 -06:00
|
|
|
void phys2d_edge_clearverts(struct phys2d_edge *edge);
|
2023-12-12 19:35:34 -06:00
|
|
|
void phys2d_edge_rmvert(struct phys2d_edge *edge, int index);
|
|
|
|
void phys2d_edge_update_verts(struct phys2d_edge *edge, HMM_Vec2 *verts);
|
|
|
|
void phys2d_edge_addverts(struct phys2d_edge *edge, HMM_Vec2 *verts);
|
2023-02-23 17:03:58 -06:00
|
|
|
void phys2d_edge_set_sensor(struct phys2d_edge *edge, int sensor);
|
|
|
|
void phys2d_edge_set_enabled(struct phys2d_edge *edge, int enabled);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
void phys2d_init();
|
|
|
|
void phys2d_update(float deltaT);
|
2023-02-02 17:52:15 -06:00
|
|
|
cpShape *phys2d_query_pos(cpVect pos);
|
2024-03-26 18:31:20 -05:00
|
|
|
void phys2d_query_ray(HMM_Vec2 start, HMM_Vec2 end, float radius, cpShapeFilter filter, JSValue cb);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-02-17 01:16:52 -06:00
|
|
|
struct shape_cb {
|
2023-03-10 13:13:48 -06:00
|
|
|
struct phys2d_shape *shape;
|
2023-02-17 01:16:52 -06:00
|
|
|
struct phys_cbs cbs;
|
|
|
|
};
|
|
|
|
|
2023-03-10 13:13:48 -06:00
|
|
|
void fire_hits();
|
|
|
|
|
2024-01-04 08:14:37 -06:00
|
|
|
void phys2d_set_gravity(HMM_Vec2 v);
|
2022-12-20 08:16:26 -06:00
|
|
|
|
2023-01-17 15:09:14 -06:00
|
|
|
void shape_enabled(struct phys2d_shape *shape, int enabled);
|
2023-01-18 10:45:43 -06:00
|
|
|
int shape_is_enabled(struct phys2d_shape *shape);
|
2023-01-17 15:09:14 -06:00
|
|
|
void shape_set_sensor(struct phys2d_shape *shape, int sensor);
|
2023-09-20 13:33:11 -05:00
|
|
|
int shape_get_sensor(struct phys2d_shape *shape);
|
2023-01-16 17:18:09 -06:00
|
|
|
|
2023-05-16 01:31:13 -05:00
|
|
|
struct rgba shape_color_s(cpShape *shape);
|
2023-01-16 02:16:39 -06:00
|
|
|
|
2022-08-12 14:03:56 -05:00
|
|
|
void shape_gui(struct phys2d_shape *shape);
|
2023-12-11 08:36:45 -06:00
|
|
|
void phys2d_setup_handlers(gameobject *go);
|
2024-01-02 07:55:22 -06:00
|
|
|
int query_point(HMM_Vec2 pos);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-12-21 19:24:59 -06:00
|
|
|
void phys2d_reindex_body(cpBody *body);
|
2024-04-06 19:41:14 -05:00
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
#endif
|