diff options
author | nathan <nathan@disroot.org> | 2025-07-09 09:46:18 +0000 |
---|---|---|
committer | nathan <nathan@disroot.org> | 2025-07-09 09:46:18 +0000 |
commit | 204ad4a59f782ce7faf2f0418b33538cf15a84cb (patch) | |
tree | 263b6d149585ab6585b01cc7ca39a46ddac3f4d7 | |
parent | d594afee3b6c55f24ecf05663b688ea488f073e6 (diff) | |
download | FindThings-204ad4a59f782ce7faf2f0418b33538cf15a84cb.tar.gz FindThings-204ad4a59f782ce7faf2f0418b33538cf15a84cb.tar.bz2 FindThings-204ad4a59f782ce7faf2f0418b33538cf15a84cb.zip |
Using the entity bounding box for something now
-rw-r--r-- | src/entity.c | 17 | ||||
-rw-r--r-- | src/entity.h | 1 | ||||
-rw-r--r-- | src/world.c | 27 |
3 files changed, 29 insertions, 16 deletions
diff --git a/src/entity.c b/src/entity.c index 0fe6366..e510a01 100644 --- a/src/entity.c +++ b/src/entity.c @@ -6,11 +6,13 @@ Entity createEntity(EntityId id, Vector3 position) { Entity entity; entity.id = id; - entity.position = position; - // Test boundingbox. - entity.box.min = Vector3SubtractValue(position, 1.0); - entity.box.max = Vector3AddValue(position, 1.0); + // Test box. + float boxSize = 0.4; + entity.box.min = (Vector3){-boxSize, -boxSize, -boxSize}; + entity.box.max = (Vector3){boxSize, boxSize, boxSize}; + + setEntityPosition(&entity, position); return entity; } @@ -31,3 +33,10 @@ void updateEntity(Entity* entity, Game* game) break; } } + +void setEntityPosition(Entity* entity, Vector3 position) +{ + entity->position = position; + entity->box.min = Vector3Add(entity->box.min, position); + entity->box.max = Vector3Add(entity->box.max, position); +} diff --git a/src/entity.h b/src/entity.h index 2a75f93..11dbde9 100644 --- a/src/entity.h +++ b/src/entity.h @@ -23,5 +23,6 @@ typedef struct { Entity createEntity(EntityId id, Vector3 position); void updateEntity(Entity* entity, Game* game); +void setEntityPosition(Entity* entity, Vector3 position); #endif diff --git a/src/world.c b/src/world.c index 94b6400..3d4682d 100644 --- a/src/world.c +++ b/src/world.c @@ -46,16 +46,16 @@ size_t buildWorldBVHLeafs(BVHNode leafs[WORLD_ENTITY_MAX], const World* world) entities[index].position); overlapBox.min = Vector3Min( overlapBox.min, - entities[leaf.entities[innerIndex]].position); + entities[leaf.entities[innerIndex]].box.min); overlapBox.min = Vector3Min( overlapBox.min, - entities[index].position); + entities[index].box.min); overlapBox.max = Vector3Max( overlapBox.max, - entities[leaf.entities[innerIndex]].position); + entities[leaf.entities[innerIndex]].box.max); overlapBox.max = Vector3Max( overlapBox.max, - entities[index].position); + entities[index].box.max); } distance /= (float)leafIndex; @@ -100,8 +100,8 @@ size_t buildWorldBVHLeafs(BVHNode leafs[WORLD_ENTITY_MAX], const World* world) } // Get bounding box. - leaf.box.min = world->entities[leaf.entities[0]].position; - leaf.box.max = world->entities[leaf.entities[0]].position; + leaf.box.min = world->entities[leaf.entities[0]].box.min; + leaf.box.max = world->entities[leaf.entities[0]].box.max; for (int index = 1; index < BVH_MAX; ++index) { @@ -112,10 +112,10 @@ size_t buildWorldBVHLeafs(BVHNode leafs[WORLD_ENTITY_MAX], const World* world) leaf.box.min = Vector3Min( leaf.box.min, - world->entities[leaf.entities[index]].position); + world->entities[leaf.entities[index]].box.min); leaf.box.max = Vector3Max( leaf.box.max, - world->entities[leaf.entities[index]].position); + world->entities[leaf.entities[index]].box.max); } leafs[leafsSize] = leaf; @@ -193,10 +193,13 @@ World createWorld(int seed) FT_RANDOM16(seed); Entity entity = createEntity(seed % ENTITY_COUNT, Vector3Zero()); - entity.position.x = FT_RANDOM16(seed) % (int)world.size.x; - entity.position.z = FT_RANDOM16(seed) % (int)world.size.z; - entity.position.y = getWorldHeightAtLocation(&world, entity.position.x, - entity.position.z) + 1.0; + Vector3 position; + position.x = FT_RANDOM16(seed) % (int)world.size.x; + position.z = FT_RANDOM16(seed) % (int)world.size.z; + position.y = getWorldHeightAtLocation(&world, + position.x, position.z) + 1.0; + setEntityPosition(&entity, position); + world.entities[index] = entity; } |