From b4734bba375192bc130cbb6f1cf87fb28db696bd Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sat, 18 Feb 2023 04:32:23 +0000 Subject: [PATCH] Polygon 2d colliders --- source/engine/2dphysics.c | 14 +++----------- source/engine/2dphysics.h | 1 - source/engine/ffi.c | 18 ++++++++++++++++-- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index a398ce5..63e63bb 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -317,6 +317,7 @@ struct phys2d_poly *Make2DPoly(int go) { struct phys2d_poly *new = malloc(sizeof(struct phys2d_poly)); + new->points = NULL; arrsetlen(new->points, 0); new->radius = 0.f; @@ -337,12 +338,9 @@ void phys2d_polyaddvert(struct phys2d_poly *poly) arrput(poly->points, cpvzero); } -void poly_gui(struct phys2d_poly *poly) -{ -} - void phys2d_poly_setverts(struct phys2d_poly *poly, cpVect *verts) { + if (!verts) return; arrfree(poly->points); poly->points = verts; phys2d_applypoly(poly); @@ -366,13 +364,7 @@ void phys2d_dbgdrawpoly(struct phys2d_poly *poly) cpVect b = cpBodyGetPosition(cpShapeGetBody(poly->shape.shape)); float angle = cpBodyGetAngle(cpShapeGetBody(poly->shape.shape)); - float s = id2go(poly->shape.go)->scale; - for (int i = 0; i < n; i++) { - float d = sqrt(pow(poly->points[i * 2].x * s, 2.f) + pow(poly->points[i * 2].y* s, 2.f)); - float a = atan2(poly->points[i * 2].y, poly->points[i * 2].x) + angle; - draw_point(b.x + d * cos(a), b.y + d * sin(a), 3, color); - } if (arrlen(poly->points) >= 3) { int n = cpPolyShapeGetCount(poly->shape.shape); @@ -519,7 +511,7 @@ void phys2d_dbgdrawedge(struct phys2d_edge *edge) drawpoints[i].y = p.y + d*sin(a); } - draw_edge(drawpoints, arrlen(edge->points), trigger_color, edge->thickness*2); + draw_edge(drawpoints, arrlen(edge->points), shape_color(edge->shapes[0]), edge->thickness*2); draw_points(drawpoints, arrlen(edge->points), 2, kinematic_color); } diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index 26221e4..d4f3fb7 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -82,7 +82,6 @@ void phys2d_applypoly(struct phys2d_poly *poly); void phys2d_dbgdrawpoly(struct phys2d_poly *poly); void phys2d_polyaddvert(struct phys2d_poly *poly); void phys2d_poly_setverts(struct phys2d_poly *poly, cpVect *verts); -void poly_gui(struct phys2d_poly *poly); struct phys2d_edge *Make2DEdge(int go); void phys2d_edgedel(struct phys2d_edge *edge); diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 20e7abb..fb40a32 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -1064,14 +1064,28 @@ duk_ret_t duk_make_poly2d(duk_context *duk) { int go = duk_to_int(duk, 0); struct phys2d_poly *poly = Make2DPoly(go); + phys2d_poly_setverts(poly, duk2cpvec2arr(duk,1)); - YughInfo("Making polygon."); + int idx = duk_push_object(duk); + duk_push_pointer(duk, poly); + duk_put_prop_string(duk, idx, "id"); + duk_push_pointer(duk, &poly->shape); + duk_put_prop_string(duk, idx, "shape"); - return 0; + return 1; } duk_ret_t duk_cmd_poly2d(duk_context *duk) { + int cmd = duk_to_int(duk,0); + struct phys2d_poly *poly = duk_to_pointer(duk,1); + + switch(cmd) { + case 0: + phys2d_poly_setverts(poly, duk2cpvec2arr(duk,2)); + break; + } + return 0; }