prosperon/source/engine/gameobject.c

100 lines
2.4 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 <string.h>
2023-12-11 08:36:45 -06:00
#include "log.h"
2023-12-24 09:14:46 -06:00
#include "math.h"
2021-11-30 21:29:18 -06:00
2022-08-26 09:19:17 -05:00
#include "stb_ds.h"
2024-05-12 18:36:14 -05:00
transform go2t(gameobject *go)
2023-11-21 17:05:06 -06:00
{
2024-05-12 18:36:14 -05:00
transform t = {0};
2023-11-15 16:42:39 -06:00
t.pos.cp = cpBodyGetPosition(go->body);
2024-05-12 18:36:14 -05:00
t.rotation = angle2rotation(cpBodyGetAngle(go->body));
2024-05-17 12:39:04 -05:00
t.scale = go->t->scale;
2023-11-09 16:44:33 -06:00
return t;
}
2024-05-17 12:39:04 -05:00
gameobject *body2go(cpBody *b)
{
return cpBodyGetUserData(b);
}
gameobject *shape2go(cpShape *s)
{
cpBody *b = cpShapeGetBody(s);
return cpBodyGetUserData(b);
2023-02-28 09:40:53 -06:00
}
2022-08-28 22:34:33 -05:00
2024-05-17 12:39:04 -05:00
void gameobject_apply(gameobject *go) { *go->t = go2t(go); }
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);
2024-01-04 08:14:37 -06:00
cpVect pos = cpBodyGetPosition(body);
2024-04-12 13:53:00 -05:00
HMM_Vec2 g = warp_force((HMM_Vec3){pos.x, pos.y, 0}, go->warp_mask).xy;
2023-11-20 07:49:14 -06:00
if (!go) {
2024-01-04 08:14:37 -06:00
cpBodyUpdateVelocity(body,g.cp,damping,dt);
2023-11-20 07:49:14 -06:00
return;
}
2024-03-13 16:30:55 -05:00
// cpFloat d = isfinite(go->damping) ? go->damping : damping;
cpFloat d = damping;
2023-11-20 07:49:14 -06:00
2024-01-04 08:14:37 -06:00
cpBodyUpdateVelocity(body,g.cp,d,dt*go->timescale);
2023-12-26 15:39:46 -06:00
if (isfinite(go->maxvelocity))
cpBodySetVelocity(body, cpvclamp(cpBodyGetVelocity(body), go->maxvelocity));
2023-11-20 07:49:14 -06:00
2023-12-26 15:39:46 -06:00
if (isfinite(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 = {
.maxvelocity = INFINITY,
.maxangularvelocity = INFINITY,
2023-12-24 09:14:46 -06:00
.damping = INFINITY,
2023-11-20 07:49:14 -06:00
.timescale = 1.0,
2023-11-29 12:40:13 -06:00
.ref = JS_UNDEFINED,
2024-04-20 12:55:20 -05:00
.warp_mask = ~0,
2023-05-12 13:22:05 -05:00
};
2024-05-17 12:39:04 -05:00
go.body = cpSpaceAddBody(space, cpBodyNew(1, 1));
cpBodySetVelocityUpdateFunc(go.body, velocityFn);
2023-12-11 08:36:45 -06:00
*ngo = go;
cpBodySetUserData(go.body, ngo);
phys2d_setup_handlers(ngo);
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
cpSpaceRemoveShape(space, shape);
}
void rm_body_constraints(cpBody *body, cpConstraint *c, void *data)
2024-01-03 08:38:17 -06:00
{
cpSpaceRemoveConstraint(space, c);
2024-01-03 08:38:17 -06:00
}
2024-01-03 17:19:13 -06:00
void gameobject_free(gameobject *go) {
go->ref = JS_UNDEFINED;
cpBodyEachShape(go->body, rm_body_shapes, NULL);
cpBodyEachConstraint(go->body, rm_body_constraints, NULL);
2023-05-12 13:22:05 -05:00
cpSpaceRemoveBody(space, go->body);
cpBodyFree(go->body);
2023-12-11 08:36:45 -06:00
free(go);
2021-11-30 21:29:18 -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);
2024-05-17 12:39:04 -05:00
// phys2d_reindex_body(go->body);
2022-02-06 10:14:57 -06:00
}