From 55829a1bef13d9c4c82df00bdce517f72171cb72 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Sun, 2 Mar 2025 01:04:09 +0300 Subject: [PATCH] /apps/demos/scenery: fix world origin relation --- apps/demos/scenery/scenes/ingame.c | 10 ++++++---- apps/demos/scenery/scenes/ingame.h | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/apps/demos/scenery/scenes/ingame.c b/apps/demos/scenery/scenes/ingame.c index 3ef1069..d944358 100644 --- a/apps/demos/scenery/scenes/ingame.c +++ b/apps/demos/scenery/scenes/ingame.c @@ -214,8 +214,8 @@ static void process_fly_mode(State *state) { /* TODO: could be baked in map format */ static Vec3 normal_at(SceneIngame *scn, Vec2 position) { - int const x = (int)(roundf(HALF_TERRAIN_DISTANCE + (position.x - scn->pos.x))); - int const y = (int)(roundf(HALF_TERRAIN_DISTANCE + (position.y - scn->pos.z))); + int const x = (int)(floorf(position.x - scn->world_center.x)); + int const y = (int)(floorf(position.y - scn->world_center.y)); float const height0 = heightmap[x][y]; float const height1 = heightmap[x + 1][y]; @@ -229,8 +229,8 @@ static Vec3 normal_at(SceneIngame *scn, Vec2 position) { /* TODO: don't operate on triangles, instead interpolate on quads */ static float height_at(SceneIngame *scn, Vec2 position) { - int const x = (int)(roundf(HALF_TERRAIN_DISTANCE + (position.x - scn->pos.x))); - int const y = (int)(roundf(HALF_TERRAIN_DISTANCE + (position.y - scn->pos.z))); + int const x = (int)(floorf(position.x - scn->world_center.x)); + int const y = (int)(floorf(position.y - scn->world_center.y)); float const height0 = heightmap[x][y]; float const height1 = heightmap[x + 1][y]; @@ -313,6 +313,8 @@ static void generate_terrain(SceneIngame *scn) { heightmap[lx][ly] = height; } } + + scn->world_center = (Vec2){ floorf(scn->pos.x - HALF_TERRAIN_DISTANCE), floorf(scn->pos.z - HALF_TERRAIN_DISTANCE) }; } diff --git a/apps/demos/scenery/scenes/ingame.h b/apps/demos/scenery/scenes/ingame.h index e97f5d5..797c387 100644 --- a/apps/demos/scenery/scenes/ingame.h +++ b/apps/demos/scenery/scenes/ingame.h @@ -13,6 +13,7 @@ typedef struct SceneIngame { Scene base; Vec3 looking_direction; + Vec2 world_center; Vec3 pos; float yaw;