Add point inflation functions

This commit is contained in:
John Alanbrook 2023-03-20 01:33:05 +00:00
parent 6d69566857
commit a170d88ccc
5 changed files with 68 additions and 11 deletions

View file

@ -100,6 +100,28 @@ void querylistbodies(cpBody *body, void *data)
}
}
int *phys2d_query_box_points(cpVect pos, cpVect wh, cpVect *points, int n)
{
cpShape *box = cpBoxShapeNew(NULL, wh.x, wh.y, 0.f);
cpTransform T = {0};
T.a = 1;
T.d = 1;
T.tx = pos.x;
T.ty = pos.y;
cpShapeUpdate(box, T);
cpBB bbox = cpShapeGetBB(box);
if (qhits) arrfree(qhits);
for (int i = 0; i < n; i++) {
if (cpBBContainsVect(bbox, points[i]))
arrpush(qhits, i);
}
return qhits;
}
int *phys2d_query_box(cpVect pos, cpVect wh)
{
cpShape *box = cpBoxShapeNew(NULL, wh.x, wh.y, 0.f);

View file

@ -141,6 +141,7 @@ struct color shape_color_s(cpShape *shape);
void shape_gui(struct phys2d_shape *shape);
void phys2d_setup_handlers(int go);
int *phys2d_query_shape(struct phys2d_shape *shape);
int *phys2d_query_box_points(cpVect pos, cpVect wh, cpVect *points, int n);
void phys2d_reindex_body(cpBody *body);
cpVect world2go(struct gameobject *go, cpVect worldpos);

View file

@ -83,15 +83,6 @@ float vecs2m(cpVect a, cpVect b)
return (b.y-a.y)/(b.x-a.x);
}
cpVect inflateline(cpVect a, cpVect b, float d)
{
cpVect c;
float m = vecs2m(a, b);
c.x = d/sqrt(1/(pow(m,2)+1));
c.y = d/sqrt(1+pow(m,2));
return c;
}
cpVect inflatepoint(cpVect a, cpVect b, cpVect c, float d)
{
cpVect ba = cpvnormalize(cpvsub(a,b));
@ -136,7 +127,6 @@ void inflatepoints(cpVect *r, cpVect *p, float d, int n)
for (int i = 0; i < n-2; i++)
r[i+1] = inflatepoint(p[i],p[i+1],p[i+2], d);
}
void draw_edge(cpVect *points, int n, struct color color, int thickness)

View file

@ -1,7 +1,7 @@
#ifndef DEBUGDRAW_H
#define DEBUGDRAW_H
struct cpVect;
#include <chipmunk/chipmunk.h>
struct color;
void debugdraw_init();
@ -20,5 +20,7 @@ void draw_poly(float *points, int n, float *color);
void debugdraw_flush(); /* This is called once per frame to draw all queued elements */
cpVect inflatepoint(cpVect a, cpVect b, cpVect c, float d);
void inflatepoints(cpVect *r, cpVect *p, float d, int n);
#endif

View file

@ -159,6 +159,18 @@ duk_idx_t vect2duk(cpVect v) {
return arr;
}
duk_idx_t vecarr2duk(duk_context *duk, cpVect *points, int n)
{
duk_idx_t arr = duk_push_array(duk);
for (int i = 0; i < n; i++) {
duk_idx_t varr = vect2duk(points[i]);
duk_put_prop_index(duk, arr, i);
}
return arr;
}
void duk_dump_stack(duk_context *duk)
{
YughInfo("DUK CALLSTACK");
@ -866,6 +878,10 @@ duk_ret_t duk_cmd(duk_context *duk) {
case 85:
vect2duk(cpvproject(duk2vec2(duk,1), duk2vec2(duk,2)));
return 1;
case 86:
ints2duk(phys2d_query_box_points(duk2vec2(duk, 1), duk2vec2(duk, 2), duk2cpvec2arr(duk,3), duk_to_int(duk,4)));
return 1;
}
return 0;
@ -1333,6 +1349,30 @@ duk_ret_t duk_cmd_edge2d(duk_context *duk)
return 0;
}
duk_ret_t duk_inflate_cpv(duk_context *duk)
{
cpVect *points = duk2cpvec2arr(duk,0);
int n = duk_to_int(duk,1);
float d = duk_to_number(duk,2);
cpVect inflate_out[n];
cpVect inflate_in[n];
inflatepoints(inflate_out, points, d, n);
inflatepoints(inflate_in, points, -d, n);
vecarr2duk(duk,inflate_out,n);
return 1;
duk_idx_t arr = duk_push_array(duk);
duk_idx_t out = vecarr2duk(duk, inflate_out, n);
duk_put_prop_index(duk,arr, out);
duk_idx_t in = vecarr2duk(duk, inflate_in, n);
duk_put_prop_index(duk,arr,in);
return 1;
}
/* These are anims for controlling properties on an object */
duk_ret_t duk_anim(duk_context *duk) {
void *prop = duk_get_heapptr(duk, 0);
@ -1410,5 +1450,7 @@ void ffi_load()
DUK_FUNC(cursor_text,5);
DUK_FUNC(gui_img, 2);
DUK_FUNC(inflate_cpv, 2);
DUK_FUNC(anim, 2);
}