prosperon/source/engine/gameobject.c

265 lines
6.8 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "gameobject.h"
#include "2dphysics.h"
2023-05-12 13:22:05 -05:00
#include <chipmunk/chipmunk.h>
#include <string.h>
#include "debugdraw.h"
2023-12-11 08:36:45 -06:00
#include "log.h"
2021-11-30 21:29:18 -06:00
2022-08-26 09:19:17 -05:00
#include "stb_ds.h"
2023-12-12 08:46:27 -06:00
static gameobject **gameobjects;
2023-12-11 08:36:45 -06:00
gameobject *body2go(cpBody *body) { return cpBodyGetUserData(body); }
gameobject *shape2go(cpShape *shape)
2023-11-07 17:24:26 -06:00
{
2023-12-11 08:36:45 -06:00
return ((struct phys2d_shape *)cpShapeGetUserData(shape))->go;
2023-11-07 17:24:26 -06:00
}
2023-12-11 08:36:45 -06:00
HMM_Vec2 go_pos(gameobject *go)
2023-11-09 16:44:33 -06:00
{
cpVect p = cpBodyGetPosition(go->body);
return (HMM_Vec2){p.x, p.y};
}
2023-12-11 08:36:45 -06:00
HMM_Vec2 go_worldpos(gameobject *go)
2023-11-30 16:24:26 -06:00
{
HMM_Vec2 ret;
ret.cp = cpBodyGetPosition(go->body);
return ret;
}
2023-12-11 08:36:45 -06:00
float go_angle(gameobject *go) { return go_worldangle(go); }
float go_worldangle(gameobject *go) { return cpBodyGetAngle(go->body); }
float go2angle(gameobject *go) { return cpBodyGetAngle(go->body); }
2023-11-21 17:05:06 -06:00
transform3d go2t3(gameobject *go)
{
transform3d t;
2023-11-30 16:24:26 -06:00
HMM_Vec2 p = go_pos(go);
2023-11-30 10:47:59 -06:00
t.pos.X = p.X;
2023-11-21 17:05:06 -06:00
t.pos.Y = p.Y;
2023-11-30 10:47:59 -06:00
t.pos.Z = go->drawlayer;
2023-11-21 17:05:06 -06:00
t.scale = go->scale;
t.scale.Z = go->scale.X;
2023-11-30 16:24:26 -06:00
t.rotation = HMM_QFromAxisAngle_RH(vFWD, go_angle(go));
2023-11-22 03:51:43 -06:00
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);
2023-11-21 17:05:06 -06:00
return t;
}
2023-12-11 08:36:45 -06:00
HMM_Vec2 go2world(gameobject *go, HMM_Vec2 pos) { return mat_t_pos(t_go2world(go), pos); }
HMM_Vec2 world2go(gameobject *go, HMM_Vec2 pos) { return mat_t_pos(t_world2go(go), pos); }
HMM_Mat3 t_go2world(gameobject *go) { return transform2d2mat(go2t(go)); }
HMM_Mat3 t_world2go(gameobject *go) { return HMM_InvGeneralM3(t_go2world(go)); }
HMM_Mat4 t3d_go2world(gameobject *go) { return transform3d2mat(go2t3(go)); }
HMM_Mat4 t3d_world2go(gameobject *go) { return HMM_InvGeneralM4(t3d_go2world(go)); }
2023-11-15 16:42:39 -06:00
2023-12-11 08:36:45 -06:00
gameobject *pos2gameobject(HMM_Vec2 pos) {
2023-11-14 09:20:09 -06:00
cpShape *hit = phys2d_query_pos(pos.cp);
2022-08-26 09:19:17 -05:00
2023-12-04 13:38:37 -06:00
if (hit)
2023-12-11 08:36:45 -06:00
return shape2go(hit);
2022-11-19 17:13:57 -06:00
for (int i = 0; i < arrlen(gameobjects); i++) {
2023-12-12 08:46:27 -06:00
if (!gameobjects[i]->body) continue;
HMM_Vec2 gpos = go_pos(gameobjects[i]);
float dist = HMM_DistV2(gpos,pos);
2022-08-26 09:19:17 -05:00
2023-12-12 08:46:27 -06:00
if (dist <= 25) return gameobjects[i];
}
2023-12-12 08:46:27 -06:00
2023-12-11 08:36:45 -06:00
return NULL;
2021-11-30 21:29:18 -06:00
}
2023-11-09 16:44:33 -06:00
transform2d go2t(gameobject *go)
{
transform2d t;
2023-11-15 16:42:39 -06:00
t.pos.cp = cpBodyGetPosition(go->body);
2023-11-09 16:44:33 -06:00
t.angle = cpBodyGetAngle(go->body);
2023-11-11 20:01:42 -06:00
t.scale = go->scale.XY;
2023-11-15 16:42:39 -06:00
if (isnan(t.scale.X)) t.scale.X = 1;
if (isnan(t.scale.Y)) t.scale.Y = 1;
2023-11-09 16:44:33 -06:00
return t;
}
2023-11-20 07:49:14 -06:00
unsigned int editor_cat = 1<<31;
2023-12-11 08:36:45 -06:00
void go_shape_apply(cpBody *body, cpShape *shape, gameobject *go) {
2023-05-12 13:22:05 -05:00
cpShapeSetFriction(shape, go->f);
cpShapeSetElasticity(shape, go->e);
2023-12-11 16:59:59 -06:00
cpShapeSetCollisionType(shape, (int)go);
2022-08-26 11:38:35 -05:00
2023-05-24 20:45:50 -05:00
cpShapeFilter filter;
2023-12-11 16:59:59 -06:00
filter.group = (int)go;
2023-11-20 07:49:14 -06:00
filter.categories = 1<<go->layer | editor_cat;
// filter.mask = CP_ALL_CATEGORIES;
filter.mask = category_masks[go->layer] | editor_cat;
// filter.mask = CP_ALL_CATEGORIES;
2023-05-24 20:45:50 -05:00
cpShapeSetFilter(shape, filter);
2023-09-26 17:07:51 -05:00
struct phys2d_shape *ape = cpShapeGetUserData(shape);
if (ape && ape->apply)
2023-09-26 17:07:51 -05:00
ape->apply(ape->data);
2023-02-28 09:40:53 -06:00
}
2022-08-28 22:34:33 -05:00
2023-12-11 08:36:45 -06:00
void go_shape_moi(cpBody *body, cpShape *shape, gameobject *go) {
2023-05-12 13:22:05 -05:00
float moment = cpBodyGetMoment(go->body);
struct phys2d_shape *s = cpShapeGetUserData(shape);
if (!s) {
cpBodySetMoment(go->body, moment + 1);
return;
}
moment += s->moi(s->data, go->mass);
2023-05-27 07:01:17 -05:00
if (moment < 0) moment = 1;
cpBodySetMoment(go->body, 1);
2021-11-30 21:29:18 -06:00
}
2023-12-11 08:36:45 -06:00
void gameobject_apply(gameobject *go) {
2023-05-12 13:22:05 -05:00
cpBodySetType(go->body, go->bodytype);
cpBodyEachShape(go->body, go_shape_apply, go);
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
if (go->bodytype == CP_BODY_TYPE_DYNAMIC) {
cpBodySetMass(go->body, go->mass);
cpBodySetMoment(go->body, 0.f);
cpBodyEachShape(go->body, go_shape_moi, go);
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
if (cpBodyGetMoment(go->body) <= 0.f)
cpBodySetMoment(go->body, 1.f);
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
return;
}
2021-11-30 21:29:18 -06:00
}
static void velocityFn(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
{
2023-12-11 08:36:45 -06:00
gameobject *go = body2go(body);
2023-11-20 07:49:14 -06:00
if (!go) {
cpBodyUpdateVelocity(body,gravity,damping,dt);
2023-11-20 07:49:14 -06:00
return;
}
2023-11-20 07:49:14 -06:00
cpFloat d = isnan(go->damping) ? damping : d;
cpVect g = go->gravity ? gravity : go->cgravity.cp;
cpBodyUpdateVelocity(body,g,d,dt*go->timescale);
if (!isinf(go->maxvelocity))
cpBodySetVelocity(body, cpvclamp(cpBodyGetVelocity(body), go->maxvelocity));
2023-11-20 07:49:14 -06:00
if (!isinf(go->maxangularvelocity)) {
float av = cpBodyGetAngularVelocity(body);
if (fabs(av) > go->maxangularvelocity)
cpBodySetAngularVelocity(body, copysignf(go->maxangularvelocity, av));
}
}
2023-12-11 08:36:45 -06:00
gameobject *MakeGameobject() {
gameobject *ngo = malloc(sizeof(*ngo));
gameobject go = {
2023-11-11 20:01:42 -06:00
.scale = (HMM_Vec3){1.f,1.f,1.f},
2023-05-12 13:22:05 -05:00
.bodytype = CP_BODY_TYPE_STATIC,
.maxvelocity = INFINITY,
.maxangularvelocity = INFINITY,
2023-05-12 13:22:05 -05:00
.mass = 1.f,
.next = -1,
.drawlayer = 0,
2023-05-12 13:22:05 -05:00
.shape_cbs = NULL,
2023-11-20 07:49:14 -06:00
.gravity = 1,
.cgravity = (HMM_Vec2){0,0},
.damping = NAN,
.timescale = 1.0,
2023-11-29 12:40:13 -06:00
.ref = JS_UNDEFINED,
2023-05-12 13:22:05 -05:00
};
2023-11-29 12:40:13 -06:00
go.cbs.begin.obj = JS_UNDEFINED;
go.cbs.separate.obj = JS_UNDEFINED;
2023-05-12 13:22:05 -05:00
go.body = cpSpaceAddBody(space, cpBodyNew(go.mass, 1.f));
cpBodySetVelocityUpdateFunc(go.body, velocityFn);
2023-12-11 08:36:45 -06:00
*ngo = go;
cpBodySetUserData(go.body, ngo);
phys2d_setup_handlers(ngo);
2023-12-12 08:46:27 -06:00
arrpush(gameobjects, ngo);
2023-12-11 08:36:45 -06:00
return ngo;
2023-11-30 10:47:59 -06:00
}
void rm_body_shapes(cpBody *body, cpShape *shape, void *data) {
2023-05-12 13:22:05 -05:00
struct phys2d_shape *s = cpShapeGetUserData(shape);
if (s->data) {
free(s->data);
s->data = NULL;
}
cpSpaceRemoveShape(space, shape);
cpShapeFree(shape);
}
2023-12-11 08:36:45 -06:00
gameobject **go_toclean = NULL;
/* Free this gameobject */
2023-12-11 08:36:45 -06:00
void gameobject_clean(gameobject *go) {
2023-05-12 13:22:05 -05:00
arrfree(go->shape_cbs);
cpBodyEachShape(go->body, rm_body_shapes, NULL);
cpSpaceRemoveBody(space, go->body);
cpBodyFree(go->body);
go->body = NULL;
2023-12-11 08:36:45 -06:00
free(go);
2021-11-30 21:29:18 -06:00
}
/* Really more of a "mark for deletion" ... */
2023-12-11 08:36:45 -06:00
void gameobject_free(gameobject *go) {
if (!go) return;
YughWarn("FREEING A GAMEOBJECT");
2023-11-30 10:47:59 -06:00
JS_FreeValue(js, go->ref);
2023-12-12 08:46:27 -06:00
for (int i = arrlen(gameobjects)-1; i >= 0; i--)
if (gameobjects[i] == go) {
arrdelswap(gameobjects, i);
break;
}
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
if (cpSpaceIsLocked(space))
2023-12-11 08:36:45 -06:00
arrpush(go_toclean, go);
2023-05-12 13:22:05 -05:00
else
2023-12-11 08:36:45 -06:00
gameobject_clean(go);
2023-11-29 12:40:13 -06:00
}
void gameobjects_cleanup() {
2023-05-12 13:22:05 -05:00
for (int i = 0; i < arrlen(go_toclean); i++)
gameobject_clean(go_toclean[i]);
2022-08-26 09:19:17 -05:00
2023-05-12 13:22:05 -05:00
arrsetlen(go_toclean, 0);
2021-11-30 21:29:18 -06:00
}
2023-12-11 08:36:45 -06:00
void gameobject_setangle(gameobject *go, float angle) {
2023-05-12 13:22:05 -05:00
cpBodySetAngle(go->body, angle);
phys2d_reindex_body(go->body);
2021-11-30 21:29:18 -06:00
}
2022-02-04 11:36:24 -06:00
2023-12-11 08:36:45 -06:00
void gameobject_setpos(gameobject *go, cpVect vec) {
2023-05-12 13:22:05 -05:00
if (!go || !go->body) return;
cpBodySetPosition(go->body, vec);
phys2d_reindex_body(go->body);
2022-02-06 10:14:57 -06:00
}
2022-08-12 14:03:56 -05:00
void body_draw_shapes_dbg(cpBody *body, cpShape *shape, void *data) {
2023-05-12 13:22:05 -05:00
struct phys2d_shape *s = cpShapeGetUserData(shape);
s->debugdraw(s->data);
}
2022-08-12 14:03:56 -05:00
2023-12-11 08:36:45 -06:00
void gameobject_draw_debug(gameobject *go) {
if (!go || !go->body) return;
2022-08-25 15:48:15 -05:00
2023-12-11 08:36:45 -06:00
cpVect pos = cpBodyGetPosition(go->body);
cpBodyEachShape(go->body, body_draw_shapes_dbg, NULL);
2022-12-22 16:58:06 -06:00
}
2023-12-12 19:35:34 -06:00
void gameobject_draw_debugs()
{
for (int i = 0; i < arrlen(gameobjects); i++)
gameobject_draw_debug(gameobjects[i]);
}