From de8c8c9168c560165ad805e1c57c8f9afd8f1277 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 30 Nov 2023 22:24:26 +0000 Subject: [PATCH] transforms --- Makefile | 2 +- scripts/entity.js | 3 ++- source/engine/gameobject.c | 17 ++++++++++--- source/engine/gameobject.h | 10 +++++--- source/engine/transform.c | 51 +++++++++++--------------------------- source/engine/transform.h | 5 ++++ 6 files changed, 43 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index cb00306..129a085 100755 --- a/Makefile +++ b/Makefile @@ -76,7 +76,7 @@ else endif endif -CPPFLAGS += -DHAVE_CEIL -DCP_USE_CGTYPES=0 -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DHAVE_FLOOR -DHAVE_FMOD -DHAVE_LRINT -DHAVE_LRINTF $(includeflag) -MD $(WARNING_FLAGS) -I. -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" -DENABLE_SINC_MEDIUM_CONVERTER -DENABLE_SINC_FAST_CONVERTER +CPPFLAGS += -DHAVE_CEIL -DCP_USE_CGTYPES=0 -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DHAVE_FLOOR -DHAVE_FMOD -DHAVE_LRINT -DHAVE_LRINTF $(includeflag) -MD $(WARNING_FLAGS) -I. -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" #-DENABLE_SINC_MEDIUM_CONVERTER -DENABLE_SINC_FAST_CONVERTER # ENABLE_SINC_[BEST|FAST|MEDIUM]_CONVERTER # default, fast and medium available in game at runtime; best available in editor diff --git a/scripts/entity.js b/scripts/entity.js index 488d60d..9266373 100644 --- a/scripts/entity.js +++ b/scripts/entity.js @@ -141,7 +141,8 @@ var gameobject = { this.set_worldpos(this.level.this2world(x)); }, - get pos() { + get pos() { return cmd + if (!this.level) return this.worldpos(); return this.level.world2this(this.worldpos()); }, diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index e822f08..d7a0af6 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -44,24 +44,35 @@ struct gameobject *shape2go(cpShape *shape) return id2go(shape2gameobject(shape)); } -HMM_Vec2 go2pos(struct gameobject *go) +HMM_Vec2 go_pos(struct gameobject *go) { cpVect p = cpBodyGetPosition(go->body); return (HMM_Vec2){p.x, p.y}; } +HMM_Vec2 go_worldpos(struct gameobject *go) +{ + HMM_Vec2 ret; + ret.cp = cpBodyGetPosition(go->body); + return ret; +} + +float go_angle(struct gameobject *go) { return go_worldangle(go); } + +float go_worldangle(struct gameobject *go) { return cpBodyGetAngle(go->body); } + float go2angle(struct gameobject *go) { return cpBodyGetAngle(go->body); } transform3d go2t3(gameobject *go) { transform3d t; - HMM_Vec2 p = go2pos(go); + HMM_Vec2 p = go_pos(go); t.pos.X = p.X; t.pos.Y = p.Y; t.pos.Z = go->drawlayer; t.scale = go->scale; t.scale.Z = go->scale.X; - t.rotation = HMM_QFromAxisAngle_RH(vFWD, go2angle(go)); + t.rotation = HMM_QFromAxisAngle_RH(vFWD, go_angle(go)); t.rotation = HMM_MulQ(HMM_QFromAxisAngle_RH(vRIGHT, -t.pos.Y/100), t.rotation); t.rotation = HMM_MulQ(HMM_QFromAxisAngle_RH(vUP, t.pos.X/100), t.rotation); return t; diff --git a/source/engine/gameobject.h b/source/engine/gameobject.h index 987223f..ae5ef94 100644 --- a/source/engine/gameobject.h +++ b/source/engine/gameobject.h @@ -35,8 +35,9 @@ struct component; typedef struct gameobject { cpBodyType bodytype; + cpBody *body; /* NULL if this object is dead; has 2d position and rotation, relative to global 0 */ + HMM_Vec3 scale; /* local */ int next; - HMM_Vec3 scale; /* local */ float mass; float f; /* friction */ float e; /* elasticity */ @@ -48,7 +49,6 @@ typedef struct gameobject { float damping; unsigned int layer; cpShapeFilter filter; - cpBody *body; /* NULL if this object is dead */ int id; struct phys_cbs cbs; struct shape_cb *shape_cbs; @@ -82,7 +82,11 @@ HMM_Mat3 t_world2go(struct gameobject *go); HMM_Mat4 t3d_go2world(struct gameobject *go); HMM_Mat4 t3d_world2go(struct gameobject *go); -HMM_Vec2 go2pos(struct gameobject *go); +HMM_Vec2 go_pos(struct gameobject *go); +HMM_Vec2 go_worldpos(struct gameobject *go); +//float go_angle(struct gameobject *go); +float go_worldangle(struct gameobject *go); + float go2angle(struct gameobject *go); HMM_Vec2 goscale(struct gameobject *go, HMM_Vec2 pos); diff --git a/source/engine/transform.c b/source/engine/transform.c index 47abd1e..f94e04e 100644 --- a/source/engine/transform.c +++ b/source/engine/transform.c @@ -7,40 +7,14 @@ const transform2d t2d_unit = { .angle = 0 }; -HMM_Vec3 trans_forward(const transform3d *const trans) -{ - return HMM_QVRot(vFWD, trans->rotation); -} +HMM_Vec3 trans_forward(const transform3d *const trans) { return HMM_QVRot(vFWD, trans->rotation); } +HMM_Vec3 trans_back(const transform3d *trans) { return HMM_QVRot(vBKWD, trans->rotation); } +HMM_Vec3 trans_up(const transform3d *trans) { return HMM_QVRot(vUP, trans->rotation); } +HMM_Vec3 trans_down(const transform3d *trans) { return HMM_QVRot(vDOWN, trans->rotation); } +HMM_Vec3 trans_right(const transform3d *trans) { return HMM_QVRot(vRIGHT, trans->rotation); } +HMM_Vec3 trans_left(const transform3d *trans) { return HMM_QVRot(vLEFT, trans->rotation); } -HMM_Vec3 trans_back(const transform3d *trans) -{ - return HMM_QVRot(vBKWD, trans->rotation); -} - -HMM_Vec3 trans_up(const transform3d *trans) -{ - return HMM_QVRot(vUP, trans->rotation); -} - -HMM_Vec3 trans_down(const transform3d *trans) -{ - return HMM_QVRot(vDOWN, trans->rotation); -} - -HMM_Vec3 trans_right(const transform3d *trans) -{ - return HMM_QVRot(vRIGHT, trans->rotation); -} - -HMM_Vec3 trans_left(const transform3d *trans) -{ - return HMM_QVRot(vLEFT, trans->rotation); -} - -HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos) -{ - return HMM_MulM3V3(m, (HMM_Vec3){pos.x, pos.y, 0}).XY; -} +HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos) { return HMM_MulM3V3(m, (HMM_Vec3){pos.x, pos.y, 0}).XY; } HMM_Vec2 mat_t_dir(HMM_Mat3 m, HMM_Vec2 dir) { @@ -48,10 +22,13 @@ HMM_Vec2 mat_t_dir(HMM_Mat3 m, HMM_Vec2 dir) return HMM_MulM3V3(m, (HMM_Vec3){dir.x, dir.y, 1}).XY; } -HMM_Vec3 mat3_t_pos(HMM_Mat4 m, HMM_Vec3 pos) -{ - return HMM_MulM4V4(m, (HMM_Vec4){pos.X, pos.Y, pos.Z, 1}).XYZ; -} +HMM_Vec2 mat_up(HMM_Mat3 m) { return HMM_NormV2(m.Columns[1].XY); } +HMM_Vec2 mat_right(HMM_Mat3 m) { return HMM_NormV2(m.Columns[0].XY); } +float vec_angle(HMM_Vec2 a, HMM_Vec2 b) { return acos(HMM_DotV2(a,b)/(HMM_LenV2(a)*HMM_LenV2(b))); } +float vec_dirangle(HMM_Vec2 a, HMM_Vec2 b) { return atan2(b.x, b.y) - atan2(a.x, a.y); } + + +HMM_Vec3 mat3_t_pos(HMM_Mat4 m, HMM_Vec3 pos) { return HMM_MulM4V4(m, (HMM_Vec4){pos.X, pos.Y, pos.Z, 1}).XYZ; } HMM_Vec3 mat3_t_dir(HMM_Mat4 m, HMM_Vec3 dir) { diff --git a/source/engine/transform.h b/source/engine/transform.h index 68b42fd..ca63d36 100644 --- a/source/engine/transform.h +++ b/source/engine/transform.h @@ -29,6 +29,11 @@ HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos); /* Transform a direction via the matrix - does not take into account translation of matrix */ HMM_Vec2 mat_t_dir(HMM_Mat3 m, HMM_Vec2 dir); +HMM_Vec2 mat_up(HMM_Mat3 m); +HMM_Vec2 mat_right(HMM_Mat3 m); +float vec_angle(HMM_Vec2 a, HMM_Vec2 b); +float vec_dirangle(HMM_Vec2 a, HMM_Vec2 b); + HMM_Vec3 mat3_t_pos(HMM_Mat4 m, HMM_Vec3 pos); HMM_Vec3 mat3_t_dir(HMM_Mat4 m, HMM_Vec3 dir);