diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index a8f659b..1f89cbb 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -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); diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index 516c94b..a68396b 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -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); diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index 540cb5a..fcf8f1f 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -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) diff --git a/source/engine/debug/debugdraw.h b/source/engine/debug/debugdraw.h index 6d7b7e9..fe12a4f 100644 --- a/source/engine/debug/debugdraw.h +++ b/source/engine/debug/debugdraw.h @@ -1,7 +1,7 @@ #ifndef DEBUGDRAW_H #define DEBUGDRAW_H -struct cpVect; +#include 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 diff --git a/source/engine/ffi.c b/source/engine/ffi.c index ce6f706..d7a7627 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -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); @@ -1409,6 +1449,8 @@ void ffi_load() DUK_FUNC(ui_text, 4); DUK_FUNC(cursor_text,5); DUK_FUNC(gui_img, 2); + + DUK_FUNC(inflate_cpv, 2); DUK_FUNC(anim, 2); }