From d27468ea020b2d54da4529294097b64407b43408 Mon Sep 17 00:00:00 2001 From: Emil Kosz Date: Tue, 3 Jun 2025 19:55:36 +0200 Subject: [PATCH] Make entity rotation work properly --- game/src/chunk/chunk.c | 12 ++---------- game/src/entity/manager.c | 4 +++- game/src/util.h | 2 ++ 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/game/src/chunk/chunk.c b/game/src/chunk/chunk.c index b6ef6ad..3ae2f06 100644 --- a/game/src/chunk/chunk.c +++ b/game/src/chunk/chunk.c @@ -14,13 +14,6 @@ Chunk_new () return chunk; } -/* Example function for a tick for an entity. */ -void -tree_grow (Entity *entity) -{ - entity->scale[1] += 0.01; -} - void Chunk_random (Chunk *chunk, int xoffset, int zoffset) { @@ -44,10 +37,9 @@ Chunk_random (Chunk *chunk, int xoffset, int zoffset) .mesh_type = ENTITY_MESH_X, .origin = ENTITY_BOTTOM, .position = { (float) x + xoffset, chunk->heightmap[INDEX (x, z)], (float) z + zoffset }, - .scale = { 3.0, 10.0, 3.0 }, - .rotation = (rand () % 180) * (M_PI / 180.0), + .scale = { 3.0, (rand()) % 5 + 5, 3.0 }, + .rotation = rand () % 180, .texture = "tree0", - .on_tick = tree_grow }; EM_ENTADD (tree); diff --git a/game/src/entity/manager.c b/game/src/entity/manager.c index a8986bb..13f7ce8 100644 --- a/game/src/entity/manager.c +++ b/game/src/entity/manager.c @@ -56,6 +56,8 @@ em_render_internal (gpointer data, gpointer userdata) } mat4 model = GLM_MAT4_IDENTITY_INIT; + + glm_rotate_at (model, entity->position, DEGTORAD (entity->rotation), (vec3) { 0.0, 1.0, 0.0 }); glm_translate (model, entity->position); if (entity->origin == ENTITY_BOTTOM) @@ -64,7 +66,7 @@ em_render_internal (gpointer data, gpointer userdata) } glm_scale (model, entity->scale); - + program_use (program); program_set_mat4 (program, "model", model); diff --git a/game/src/util.h b/game/src/util.h index 1d09f14..ecc1581 100644 --- a/game/src/util.h +++ b/game/src/util.h @@ -17,6 +17,8 @@ #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) +#define DEGTORAD(deg) ((deg) * (M_PI / 180.0)) + #define UNUSED(...) (UNUSED_1 (__VA_ARGS__, 0)) #define UNUSED_1(arg, ...) (void) arg, UNUSED_2 (__VA_ARGS__, 0) #define UNUSED_2(arg, ...) (void) arg, UNUSED_3 (__VA_ARGS__, 0)