X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2FWorld.cpp;h=82701ce8686f48f20b3f24039a1239547375671f;hb=c04ea5a6f67d446ea29aa2e88dc4c666956d7732;hp=529968ea7470ce67ae7d9ed2e3a77a173f37e47d;hpb=bc1cefd505bf1f34639b8839cb337b08310ceb8e;p=blank.git diff --git a/src/world/World.cpp b/src/world/World.cpp index 529968e..82701ce 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -3,6 +3,7 @@ #include "../graphics/BlockLighting.hpp" #include "../graphics/DirectionalLighting.hpp" +#include #include #include @@ -113,6 +114,9 @@ World::World(const Config &config) generate.Solids({ 1, 4, 7, 10 }); player = &AddEntity(); + player->Name("player"); + player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } }); + player->WorldCollidable(true); player->Position(config.spawn); chunks.GenerateSurrounding(player->ChunkCoords()); @@ -131,12 +135,13 @@ std::vector candidates; } bool World::Intersection( - const Ray &ray, - const glm::mat4 &M, - Chunk **chunk, - int *blkid, - float *dist, - glm::vec3 *normal) { + const Ray &ray, + const glm::mat4 &M, + Chunk *&chunk, + int &blkid, + float &dist, + glm::vec3 &normal +) { candidates.clear(); for (Chunk &cur_chunk : chunks.Loaded()) { @@ -148,39 +153,37 @@ bool World::Intersection( if (candidates.empty()) return false; - Chunk *closest_chunk = nullptr; - float closest_dist = std::numeric_limits::infinity(); - int closest_blkid = -1; - glm::vec3 closest_normal; + chunk = nullptr; + dist = std::numeric_limits::infinity(); + blkid = -1; for (Candidate &cand : candidates) { - if (cand.dist > closest_dist) continue; + if (cand.dist > dist) continue; int cur_blkid; float cur_dist; glm::vec3 cur_normal; if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_blkid, cur_dist, cur_normal)) { - if (cur_dist < closest_dist) { - closest_chunk = cand.chunk; - closest_blkid = cur_blkid; - closest_dist = cur_dist; - closest_normal = cur_normal; + if (cur_dist < dist) { + chunk = cand.chunk; + blkid = cur_blkid; + dist = cur_dist; + normal = cur_normal; } } } - if (chunk) { - *chunk = closest_chunk; - } - if (blkid) { - *blkid = closest_blkid; - } - if (dist) { - *dist = closest_dist; - } - if (normal) { - *normal = closest_normal; + return chunk; +} + +bool World::Intersection(const Entity &e) { + AABB box = e.Bounds(); + glm::mat4 M = e.Transform(player->ChunkCoords()); + for (Chunk &cur_chunk : chunks.Loaded()) { + if (cur_chunk.Intersection(box, M, cur_chunk.Transform(player->ChunkCoords()))) { + return true; + } } - return closest_chunk; + return false; } @@ -198,6 +201,12 @@ void World::Update(int dt) { for (Entity &entity : entities) { entity.Update(dt); } + for (Entity &entity : entities) { + if (entity.WorldCollidable() && Intersection(entity)) { + // entity collides with the world + std::cout << entity.Name() << " entity intersects world" << std::endl; + } + } chunks.Rebase(player->ChunkCoords()); chunks.Update(dt); }