prosperon/source/engine/gameobject.c

172 lines
4.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"
2023-12-11 08:36:45 -06:00
gameobject *body2go(cpBody *body) { return cpBodyGetUserData(body); }
2024-04-11 17:17:49 -05:00
gameobject *shape2go(cpShape *shape) {
struct phys2d_shape *pshape = cpShapeGetUserData(shape);
if (!pshape) return NULL;
return pshape->go;
}
2023-11-07 17:24:26 -06:00
2024-05-12 18:36:14 -05:00
HMM_Vec3 go_pos(gameobject *go)
2023-11-09 16:44:33 -06:00
{
cpVect p = cpBodyGetPosition(go->body);
2024-05-12 18:36:14 -05:00
return (HMM_Vec3){p.x, p.y, 0};
2023-11-09 16:44:33 -06:00
}
2023-12-18 17:12:05 -06:00
float go_angle(gameobject *go) { return cpBodyGetAngle(go->body); }
2023-11-21 17:05:06 -06:00
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));
t.scale = go->scale;
2023-12-27 07:04:18 -06:00
if (!isfinite(t.scale.X)) t.scale.X = 1;
if (!isfinite(t.scale.Y)) t.scale.Y = 1;
2023-11-09 16:44:33 -06:00
return t;
}
2023-12-11 08:36:45 -06:00
void go_shape_apply(cpBody *body, cpShape *shape, gameobject *go) {
cpShapeSetFriction(shape, go->friction);
cpShapeSetElasticity(shape, go->elasticity);
cpShapeSetCollisionType(shape, (cpCollisionType)go);
2022-08-26 11:38:35 -05:00
2023-05-24 20:45:50 -05:00
cpShapeFilter filter;
filter.group = (cpCollisionType)go;
2024-04-12 13:53:00 -05:00
filter.categories = go->categories;
filter.mask = go->mask;
2023-11-20 07:49:14 -06:00
// 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-12-28 17:38:17 -06:00
float moment = cpBodyGetMoment(body);
2023-05-12 13:22:05 -05:00
struct phys2d_shape *s = cpShapeGetUserData(shape);
if (!s) {
2023-12-28 17:38:17 -06:00
cpBodySetMoment(body, moment + 1);
2023-05-12 13:22:05 -05:00
return;
}
2023-12-28 17:38:17 -06:00
moment += s->moi(s->data);
if (moment < 0) moment = 0;
cpBodySetMoment(body, moment);
2021-11-30 21:29:18 -06:00
}
2023-12-11 08:36:45 -06:00
void gameobject_apply(gameobject *go) {
2024-03-22 13:40:56 -05:00
YughSpam("Applying gameobject %p", go);
cpBodySetType(go->body, go->phys);
2023-05-12 13:22:05 -05:00
cpBodyEachShape(go->body, go_shape_apply, go);
2021-11-30 21:29:18 -06:00
if (go->phys == CP_BODY_TYPE_DYNAMIC) {
2023-05-12 13:22:05 -05:00
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
}
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 = {
2023-11-11 20:01:42 -06:00
.scale = (HMM_Vec3){1.f,1.f,1.f},
.phys = CP_BODY_TYPE_STATIC,
.maxvelocity = INFINITY,
.maxangularvelocity = INFINITY,
2023-05-12 13:22:05 -05:00
.mass = 1.f,
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-12 13:53:00 -05:00
.mask = ~0,
.categories = 1,
2024-04-20 12:55:20 -05:00
.warp_mask = ~0,
2023-05-12 13:22:05 -05:00
};
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);
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);
2024-04-11 17:17:49 -05:00
if (s) {
2024-04-11 17:17:49 -05:00
JS_FreeValue(js, s->ref);
s->ref = JS_UNDEFINED;
if (s->free)
s->free(s->data);
else
free(s->data);
}
2024-04-11 17:17:49 -05:00
2024-04-12 13:53:00 -05:00
cpShapeSetFilter(shape, CP_SHAPE_FILTER_NONE);
2023-05-12 13:22:05 -05:00
cpSpaceRemoveShape(space, shape);
cpShapeFree(shape);
}
2024-01-03 08:38:17 -06:00
void rm_body_constraints(cpBody *body, cpConstraint *constraint, void *data)
{
2024-01-14 10:24:31 -06:00
constraint_break(cpConstraintGetUserData(constraint));
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;
2023-05-12 13:22:05 -05:00
cpBodyEachShape(go->body, rm_body_shapes, NULL);
2024-01-03 08:38:17 -06:00
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_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
}