aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornathan <nathansmith@disroot.org>2025-07-08 23:08:12 +0000
committernathan <nathansmith@disroot.org>2025-07-08 23:08:12 +0000
commitf3a07a4e98444c7d98df8b3f3b57e2136477f87f (patch)
tree650b71413a39acc4af319ab4bdf48ccec943a6d1
parent7c3eb38f22ec3cd951aa4af77d9b66a6d1973b46 (diff)
downloadFindThings-f3a07a4e98444c7d98df8b3f3b57e2136477f87f.tar.gz
FindThings-f3a07a4e98444c7d98df8b3f3b57e2136477f87f.tar.bz2
FindThings-f3a07a4e98444c7d98df8b3f3b57e2136477f87f.zip
Less overlap
-rw-r--r--src/world.c44
1 files changed, 37 insertions, 7 deletions
diff --git a/src/world.c b/src/world.c
index 1958ffe..902e6c1 100644
--- a/src/world.c
+++ b/src/world.c
@@ -2,8 +2,7 @@
#include "game.h"
// Bottom up method because bottom up better. Just if politicians agreed ):
-// z curve broky, last metho was decent but was first come first serve and
-// would leave leafs toward the end with shitty options and end up being big.
+// Very messy right now. Mostly been playing around.
void buildWorldBVH(World* world)
{
Entity* entities = world->entities;
@@ -15,22 +14,29 @@ void buildWorldBVH(World* world)
grouped[index] = false;
}
- for (int leafIndex = 0; leafIndex < BVH_MAX; ++leafIndex)
+ for (int nodeIndex = 0; nodeIndex < BVH_LEAF_COUNT; ++nodeIndex)
{
- for (int nodeIndex = 0; nodeIndex < BVH_LEAF_COUNT; ++nodeIndex)
+ for (int leafIndex = 0; leafIndex < BVH_MAX; ++leafIndex)
{
BVHNode* leaf = &world->bvhTest[nodeIndex];
// First entity.
if (leafIndex == 0)
{
- leaf->entities[0] = nodeIndex * BVH_MAX;
- grouped[nodeIndex * BVH_MAX] = true;
+ for (int index = 0; index < WORLD_ENTITY_MAX; ++index)
+ {
+ if (!grouped[index])
+ {
+ leaf->entities[0] = index;
+ grouped[index] = true;
+ break;
+ }
+ }
+
continue;
}
// Find closest to entity one.
- // TODO: Average out the entities.
int closest = -1;
float closestDistance = world->size.x * world->size.z * 2.0;
@@ -52,6 +58,30 @@ void buildWorldBVH(World* world)
distance /= (float)leafIndex;
+ // Check for overlap.
+ for (int overlapNode = 0; overlapNode < nodeIndex; ++overlapNode)
+ {
+ Vector3 nodeLocation = Vector3Scale(
+ Vector3Add(world->bvhTest[overlapNode].box.min,
+ world->bvhTest[overlapNode].box.max), 0.5);
+
+ Ray ray = (Ray){
+ .position = entities[index].position,
+ .direction = Vector3Normalize(
+ Vector3Subtract(nodeLocation, entities[index].position))
+ };
+
+ RayCollision overlapResult = GetRayCollisionBox(
+ ray, world->bvhTest[overlapNode].box);
+
+ if (overlapResult.hit && overlapResult.distance < distance)
+ {
+ // Dont stop overlap but make it less likely.
+ distance *= 10.0;
+ break;
+ }
+ }
+
if (distance < closestDistance)
{
closestDistance = distance;