add position calculations 2d

This commit is contained in:
John Alanbrook 2023-02-20 17:10:03 +00:00
parent c3b1988fb7
commit 49a17e0d59
3 changed files with 32 additions and 0 deletions

View file

@ -202,6 +202,27 @@ void phys2d_circledel(struct phys2d_circle *c)
phys2d_shape_del(&c->shape);
}
cpVect world2go(struct gameobject *go, cpVect worldpos)
{
worldpos = cpvsub(worldpos, cpBodyGetPosition(go->body));
worldpos = cpvmult(worldpos, 1/go->scale);
return worldpos;
}
cpVect go2world(struct gameobject *go, cpVect gopos)
{
cpVect pos = cpBodyGetPosition(go->body);
float angle = cpBodyGetAngle(go->body);
cpTransform T = {0};
T.a = go->scale * go->flipx * cos(angle);
T.b = -sin(angle) * go->scale;
T.c = sin(angle) * go->scale;
T.d = go->scale * go->flipy * cos(angle);
T.tx = pos.x;
T.ty = pos.y;
return cpTransformPoint(T, gopos);
}
cpTransform body2transform(cpBody *body)
{
cpTransform T = {0};

View file

@ -131,5 +131,7 @@ struct color float2color(float *fcolor);
void shape_gui(struct phys2d_shape *shape);
void phys2d_reindex_body(cpBody *body);
cpVect world2go(struct gameobject *go, cpVect worldpos);
cpVect go2world(struct gameobject *go, cpVect gopos);
#endif

View file

@ -713,6 +713,15 @@ duk_ret_t duk_cmd(duk_context *duk) {
case 69:
gameobject_set_sensor(duk_to_int(duk, 1), duk_to_boolean(duk,2));
break;
case 70:
vect2duk(world2go(id2go(duk_to_int(duk,1)), duk2vec2(duk,2)));
return 1;
case 71:
vect2duk(go2world(id2go(duk_to_int(duk,1)),duk2vec2(duk,2)));
return 1;
}
return 0;