Polygon 2d colliders

This commit is contained in:
John Alanbrook 2023-02-18 04:32:23 +00:00
parent a401dbb9d4
commit b4734bba37
3 changed files with 19 additions and 14 deletions

View file

@ -317,6 +317,7 @@ struct phys2d_poly *Make2DPoly(int go)
{ {
struct phys2d_poly *new = malloc(sizeof(struct phys2d_poly)); struct phys2d_poly *new = malloc(sizeof(struct phys2d_poly));
new->points = NULL;
arrsetlen(new->points, 0); arrsetlen(new->points, 0);
new->radius = 0.f; new->radius = 0.f;
@ -337,12 +338,9 @@ void phys2d_polyaddvert(struct phys2d_poly *poly)
arrput(poly->points, cpvzero); arrput(poly->points, cpvzero);
} }
void poly_gui(struct phys2d_poly *poly)
{
}
void phys2d_poly_setverts(struct phys2d_poly *poly, cpVect *verts) void phys2d_poly_setverts(struct phys2d_poly *poly, cpVect *verts)
{ {
if (!verts) return;
arrfree(poly->points); arrfree(poly->points);
poly->points = verts; poly->points = verts;
phys2d_applypoly(poly); phys2d_applypoly(poly);
@ -366,13 +364,7 @@ void phys2d_dbgdrawpoly(struct phys2d_poly *poly)
cpVect b = cpBodyGetPosition(cpShapeGetBody(poly->shape.shape)); cpVect b = cpBodyGetPosition(cpShapeGetBody(poly->shape.shape));
float angle = cpBodyGetAngle(cpShapeGetBody(poly->shape.shape)); float angle = cpBodyGetAngle(cpShapeGetBody(poly->shape.shape));
float s = id2go(poly->shape.go)->scale; 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) { if (arrlen(poly->points) >= 3) {
int n = cpPolyShapeGetCount(poly->shape.shape); 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); 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); draw_points(drawpoints, arrlen(edge->points), 2, kinematic_color);
} }

View file

@ -82,7 +82,6 @@ void phys2d_applypoly(struct phys2d_poly *poly);
void phys2d_dbgdrawpoly(struct phys2d_poly *poly); void phys2d_dbgdrawpoly(struct phys2d_poly *poly);
void phys2d_polyaddvert(struct phys2d_poly *poly); void phys2d_polyaddvert(struct phys2d_poly *poly);
void phys2d_poly_setverts(struct phys2d_poly *poly, cpVect *verts); void phys2d_poly_setverts(struct phys2d_poly *poly, cpVect *verts);
void poly_gui(struct phys2d_poly *poly);
struct phys2d_edge *Make2DEdge(int go); struct phys2d_edge *Make2DEdge(int go);
void phys2d_edgedel(struct phys2d_edge *edge); void phys2d_edgedel(struct phys2d_edge *edge);

View file

@ -1064,14 +1064,28 @@ duk_ret_t duk_make_poly2d(duk_context *duk)
{ {
int go = duk_to_int(duk, 0); int go = duk_to_int(duk, 0);
struct phys2d_poly *poly = Make2DPoly(go); 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) 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; return 0;
} }