aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornathan <nathan@disroot.org>2025-07-17 07:05:53 +0000
committernathan <nathan@disroot.org>2025-07-17 07:05:53 +0000
commit0d2212978cf0198f02b48d1fe8cbaa3b3706682c (patch)
tree6e139120147cd9f65163dc1dc870e7ae05af6739
parent5dd8a98ebfaea93e5542e0e149f9df9ebcac9ef4 (diff)
downloadFindThings-0d2212978cf0198f02b48d1fe8cbaa3b3706682c.tar.gz
FindThings-0d2212978cf0198f02b48d1fe8cbaa3b3706682c.tar.bz2
FindThings-0d2212978cf0198f02b48d1fe8cbaa3b3706682c.zip
A little more betters lol (:
-rw-r--r--src/world.c59
-rw-r--r--src/world.h4
2 files changed, 27 insertions, 36 deletions
diff --git a/src/world.c b/src/world.c
index defbec7..39b6bcd 100644
--- a/src/world.c
+++ b/src/world.c
@@ -154,7 +154,6 @@ size_t buildWorldBVHLeafs(BVHNode leafs[WORLD_ENTITY_MAX], const World* world)
size_t buildWorldBVHSubtree(BVHNode* subtree,
const BVHNode* nodes, size_t nodesSize,
- int sizeMax,
const World* world)
{
size_t subtreeSize = 0;
@@ -185,13 +184,10 @@ size_t buildWorldBVHSubtree(BVHNode* subtree,
if (branchIndex == 0)
{
closest = index;
- break;
}
float distance = 0.0;
- BoundingBox overlapBox;
- overlapBox.min = nodes[index].position;
- overlapBox.max = overlapBox.min;
+ BoundingBox overlapBox = nodes[index].box;
for (int innerIndex = 0; innerIndex < node.branchCount; ++innerIndex)
{
@@ -209,15 +205,8 @@ size_t buildWorldBVHSubtree(BVHNode* subtree,
bool overlaps = false;
- // Too big (will count it as a overlap).
- if (Vector3Distance(overlapBox.min, overlapBox.max) >= sizeMax)
- {
- overlaps = true;
- }
-
// Check for overlap.
- for (int subtreeIndex = 0; subtreeIndex < subtreeSize && !overlaps;
- ++subtreeIndex)
+ for (int subtreeIndex = 0; subtreeIndex < subtreeSize; ++subtreeIndex)
{
if (CheckCollisionBoxes(overlapBox, subtree[subtreeIndex].box))
{
@@ -226,24 +215,28 @@ size_t buildWorldBVHSubtree(BVHNode* subtree,
}
}
- // Will cause overlap
- /* if (branchIndex == BVH_MAX_BRANCH_COUNT - 1) */
- /* { */
- /* for (int innerIndex = 0; innerIndex < nodesSize && !overlaps; */
- /* ++innerIndex) */
- /* { */
- /* if (innerIndex == index || grouped[innerIndex]) */
- /* { */
- /* continue; */
- /* } */
-
- /* if (CheckCollisionBoxes(overlapBox, nodes[innerIndex].box)) */
- /* { */
- /* overlaps = true; */
- /* break; */
- /* } */
- /* } */
- /* } */
+ // Will cause overlap.
+ int overlapCauseCount = 0;
+
+ for (int innerIndex = 0; innerIndex < nodesSize && !overlaps;
+ ++innerIndex)
+ {
+ if (innerIndex == index || grouped[innerIndex])
+ {
+ continue;
+ }
+
+ if (CheckCollisionBoxes(overlapBox, nodes[innerIndex].box))
+ {
+ ++overlapCauseCount;
+ }
+
+ if (overlapCauseCount > BVH_MAX_BRANCH_COUNT - node.branchCount)
+ {
+ overlaps = true;
+ break;
+ }
+ }
// Update distance.
if (!overlaps && distance < closestDistance)
@@ -327,13 +320,11 @@ void buildWorldBVH(World* world)
// Get leafs.
BVHNode tree[WORLD_ENTITY_MAX];
size_t treeSize = buildWorldBVHLeafs(tree, world);
- int sizeMax = BVH_BOX_MAX;
while (treeSize > 1)
{
BVHNode subtree[treeSize];
- sizeMax += BVH_BOX_MAX;
- treeSize = buildWorldBVHSubtree(subtree, tree, treeSize, sizeMax, world);
+ treeSize = buildWorldBVHSubtree(subtree, tree, treeSize, world);
memcpy(tree, subtree, treeSize * sizeof(BVHNode));
}
diff --git a/src/world.h b/src/world.h
index 21f5aec..39abb38 100644
--- a/src/world.h
+++ b/src/world.h
@@ -5,8 +5,8 @@
#ifndef WORLD_H
#define WORLD_H
-#define BVH_MAX 2 // Max entities per node.
-#define BVH_MAX_BRANCH_COUNT 4
+#define BVH_MAX 4 // Max entities per node.
+#define BVH_MAX_BRANCH_COUNT 8
#define BVH_BOX_MAX 100.0
#define WORLD_ENTITY_MAX 1000