From 85984a0a07f2ea45eb4c0df2cc89649623067d59 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Tue, 14 Oct 2025 16:13:46 +0100 Subject: [PATCH] Add collision info to entities --- game.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/game.c b/game.c index cd71ccb..9aa8b69 100644 --- a/game.c +++ b/game.c @@ -9,6 +9,8 @@ #include #include +#define SHIP_COLLIDE_R 0.05 + #define FIRE_MEAN -0.15 #define FIRE_JITTER 0.01 @@ -29,6 +31,7 @@ #define ROT_PWR 0.002 #define SHOT_VEL 0.04 +#define SHOT_COLLIDE_R 0.005 #define MAX_SHAPES 256U #define MAX_ENTITIES 128U @@ -36,6 +39,12 @@ #define NELEMS(arr) (sizeof(arr) / sizeof(arr[0])) +typedef enum { + COLLISION_SHIP, + COLLISION_SHOT, + COLLISION_ASTEROID, +} collision_tag_t; + typedef struct { vec2_t pos; vec2_t vel; @@ -43,6 +52,8 @@ typedef struct { float omg; unsigned shapes[MAX_SHAPES_PER_ENTITY]; unsigned shape_count; + float collision_radius; + collision_tag_t tag; bool wrap; } entity_t; @@ -118,6 +129,8 @@ static void shoot() ship->pos, mat2_mul_vec2(ship->dir, ship_shape->verts[0])); e->vel = vec2_add( ship->vel, mat2_mul_vec2(ship->dir, (vec2_t) { 0, SHOT_VEL })); + e->collision_radius = SHIP_COLLIDE_R; + e->tag = COLLISION_SHOT; shape_t *s = add_shape(id, nullptr); s->visible = true; @@ -130,6 +143,8 @@ static entity_t *gen_asteroid(float r_mean) unsigned id; entity_t *e = add_entity(&id); e->wrap = true; + e->collision_radius = r_mean; + e->tag = COLLISION_ASTEROID; shape_t *s = add_shape(id, nullptr); s->visible = true; @@ -177,7 +192,7 @@ static void remove_shape(unsigned id) { if (id < shape_count - 1) { const shape_t *last = shapes + shape_count - 1; - memcpy(shapes + id, shapes + shape_count - 1, sizeof(shape_t)); + memcpy(shapes + id, last, sizeof(shape_t)); for (unsigned i = 0; i < entities[last->entity].shape_count; ++i) { if (entities[last->entity].shapes[i] == shape_count - 1) { entities[last->entity].shapes[i] = id; @@ -227,9 +242,12 @@ void game_init(float _aspect) aspect = _aspect; quit = false; + entity_count = shape_count = 0; entity_t *ship = add_entity(&ship_entity_id); ship->wrap = true; + ship->collision_radius = SHIP_COLLIDE_R; + ship->tag = COLLISION_SHIP; shape_t *ship_shape = add_shape(ship_entity_id, &ship_shape_id); ship_shape->visible = true;