From 52d1a04b30b5ce8da9b9dcce02e829a35f664dcb Mon Sep 17 00:00:00 2001 From: nathan Date: Tue, 11 Nov 2025 02:38:51 -0700 Subject: Render distance thingy --- src/world.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) (limited to 'src/world.c') diff --git a/src/world.c b/src/world.c index 5ffec66..6f2e093 100644 --- a/src/world.c +++ b/src/world.c @@ -509,13 +509,13 @@ void generateWorldSamanthasPlace(World* world, const Assets* assets, Vector3 center = Vector3Scale(world->size, 0.5); Entity spot = createEntity(SAMANTHAS_SPOT, center); world->entities[*placeId] = spot; + world->places[*placeId] = *placeId; *placeId = *placeId + 1; // Samantha. Entity samantha = createEntity(SAMANTHA, Vector3Add(center, SAMANTHA_OFFSET)); world->entities[*placeId] = samantha; - *placeId = *placeId + 1; // Trashcans yippee! Vector3 trashPosition = (Vector3){-SAMANTHAS_SPOT, @@ -824,15 +824,57 @@ void drawBVHDebug(BVHNode bvh, int level, int selected) } } +float getBVHNodeDistance(BVHNode node, Vector3 position) +{ + return Vector3Distance(node.position, position) + - (Vector3Distance(node.box.min, node.box.max) / 2.0); +} + +void updateNearByEntites(World* world, BVHNode node, Game* game) +{ + if (getBVHNodeDistance(node, game->player.position) + > game->settings.maxUpdateDistance) + { + return; + } + + // Leaf node. + if (node.branchCount == 0) + { + for (int index = 0; index < BVH_MAX; ++index) + { + if (node.entities[index] != -1) + { + Entity* entity = &world->entities[node.entities[index]]; + + if (!entityIsPlace(entity->id)) + { + updateEntity(entity, game); + } + } + } + } + else // Subtree. + { + for (int index = 0; index < node.branchCount; ++index) + { + updateNearByEntites(world, *node.branches[index], game); + } + } +} + void updateWorld(World* world, Game* game) { DrawModel(world->heightmap, Vector3Zero(), 1.0, WHITE); - for (int index = 0; index < WORLD_ENTITY_MAX; ++index) + // Update places. + for (int index = 0; index < WORLD_PLACE_COUNT; ++index) { - updateEntity(&world->entities[index], game); + updateEntity(&world->entities[world->places[index]], game); } + updateNearByEntites(world, world->bvh, game); + DrawMeshInstanced(game->assets.models[UTILITY_POLE_MODEL].meshes[0], game->assets.models[UTILITY_POLE_MODEL].materials[0], world->utilityPoleTransforms, @@ -993,8 +1035,6 @@ void castRayBVH(const World* world, BVHNode node, Ray ray, bool allowAll, closest, closestDistance); } } - - return; } WorldUID castRayAtWorld(const World* world, Ray ray, bool allowAll, -- cgit v1.2.3