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
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
struct phys2d_shape {
|
2023-05-12 13:22:05 -05:00
|
|
|
cpShape *shape;
|
2023-12-04 13:38:37 -06:00
|
|
|
transform2d 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-05-12 13:22:05 -05:00
|
|
|
void (*debugdraw)(void *data);
|
|
|
|
float (*moi)(void *data, float mass);
|
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-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
|
|
|
};
|
|
|
|
|
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-12-04 13:38:37 -06:00
|
|
|
transform2d t;
|
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);
|
2021-11-30 21:29:18 -06:00
|
|
|
void phys2d_applycircle(struct phys2d_circle *circle);
|
|
|
|
void phys2d_dbgdrawcircle(struct phys2d_circle *circle);
|
2023-02-24 12:11:36 -06:00
|
|
|
float phys2d_circle_moi(struct phys2d_circle *c, float m);
|
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-02-24 12:11:36 -06:00
|
|
|
float phys2d_poly_moi(struct phys2d_poly *poly, float m);
|
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-02-24 12:11:36 -06:00
|
|
|
float phys2d_edge_moi(struct phys2d_edge *edge, float m);
|
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);
|
2023-12-21 17:21:01 -06:00
|
|
|
gameobject **phys2d_query_box(HMM_Vec2 pos, HMM_Vec2 wh);
|
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();
|
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
void phys2d_rm_go_handlers(gameobject *go);
|
2023-01-10 17:23:11 -06:00
|
|
|
void phys2d_set_gravity(cpVect 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);
|
2023-12-21 17:21:01 -06:00
|
|
|
gameobject **phys2d_query_shape(struct phys2d_shape *shape);
|
2023-11-14 09:20:09 -06:00
|
|
|
int *phys2d_query_box_points(HMM_Vec2 pos, HMM_Vec2 wh, HMM_Vec2 *points, int n);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-27 10:13:20 -05:00
|
|
|
void flush_collide_cbs();
|
|
|
|
|
2022-12-21 19:24:59 -06:00
|
|
|
void phys2d_reindex_body(cpBody *body);
|
2023-03-10 13:13:48 -06:00
|
|
|
extern unsigned int category_masks[32];
|
|
|
|
void set_cat_mask(int cat, unsigned int mask);
|
2023-03-17 10:25:35 -05:00
|
|
|
int phys2d_in_air(cpBody *body);
|
2021-11-30 21:29:18 -06:00
|
|
|
#endif
|