Refactor into more modular architecture

This commit is contained in:
Camden Dixie O'Brien
2025-10-17 14:03:34 +01:00
parent 34e32c6a46
commit 536ee74b61
12 changed files with 813 additions and 428 deletions

129
physics.c Normal file
View File

@@ -0,0 +1,129 @@
#include "physics.h"
#include "entity.h"
#include "renderer.h"
#include <assert.h>
#include <string.h>
typedef struct {
bool valid;
unsigned component_id;
physics_t state;
} entry_t;
static unsigned max;
static unsigned escape_entity;
static entry_t entries[MAX_ENTITIES];
static void update(unsigned new_entity_id, void *ref)
{
entry_t *old_e = (entry_t *)ref;
entry_t *new_e = entries + new_entity_id;
memcpy(new_e, old_e, sizeof(entry_t));
old_e->valid = false;
entity_update_component(new_entity_id, new_e->component_id, new_e);
}
static void remove(void *ref)
{
entry_t *e = (entry_t *)ref;
e->valid = false;
}
void physics_init()
{
max = 0;
escape_entity = MAX_ENTITIES;
}
void physics_update()
{
for (unsigned i = 0; i < max; ++i) {
if (!entries[i].valid)
continue;
physics_t *st = &entries[i].state;
st->dir = mat2_mul_mat2(mat2_rotation(st->rot), st->dir);
st->pos = vec2_add(st->pos, st->vel);
if (st->pos.y > 1)
st->pos.y -= 2;
else if (st->pos.y <= -1)
st->pos.y += 2;
if (st->pos.x >= aspect && i != escape_entity)
st->pos.x -= 2 * aspect;
else if (st->pos.x < -aspect)
st->pos.x += 2 * aspect;
}
}
void physics_add(
unsigned entity, vec2_t pos, mat2_t dir, vec2_t vel, float rot,
float mass)
{
assert(entity < MAX_ENTITIES);
if (entity >= max)
max = entity + 1;
else
assert(!entries[entity].valid);
entry_t *entry = entries + entity;
entry->valid = true;
entry->component_id
= entity_add_component(entity, update, remove, entry);
entry->state = (physics_t) {
.pos = pos,
.dir = dir,
.vel = vel,
.rot = rot,
.mass = mass,
};
}
physics_t *physics_get(unsigned entity)
{
assert(entity < max);
assert(entries[entity].valid);
return &entries[entity].state;
}
void physics_escape(unsigned entity)
{
assert(entity < max);
assert(entries[entity].valid);
escape_entity = entity;
}
physics_sep_t physics_separation(unsigned a, unsigned b)
{
const physics_t *pa = physics_get(a);
const physics_t *pb = physics_get(b);
const vec2_t disp = vec2_sub(pb->pos, pa->pos);
const vec2_t norm = vec2_norm(disp);
return (physics_sep_t) {
.dist = vec2_len(disp),
.norm = norm,
.va = vec2_dot(pa->vel, norm),
.vb = vec2_dot(pb->vel, norm),
};
}
void physics_bounce(unsigned a, unsigned b, physics_sep_t sep)
{
physics_t *pa = physics_get(a);
physics_t *pb = physics_get(b);
const float ma = pa->mass;
const float mb = pb->mass;
const float ms = ma + mb;
const float va = (sep.va * (ma - mb) + 2 * mb * sep.vb) / ms;
const float vb = (sep.vb * (mb - ma) + 2 * ma * sep.va) / ms;
pa->vel = vec2_add(pa->vel, vec2_scale(sep.norm, va - sep.va));
pb->vel = vec2_add(pb->vel, vec2_scale(sep.norm, vb - sep.vb));
}