]> git.localhorst.tv Git - blank.git/blobdiff - src/world/World.cpp
check for entities under crosshair
[blank.git] / src / world / World.cpp
index 1e372e753d8c82257fb7bcd34fe6295c11e70e78..2c0d164d5b4acb8f09bc4eba77b8a83faf881967 100644 (file)
@@ -95,6 +95,35 @@ bool World::Intersection(
        return chunk;
 }
 
+bool World::Intersection(
+       const Ray &ray,
+       const glm::mat4 &M,
+       Entity *&entity,
+       float &dist,
+       glm::vec3 &normal
+) {
+       entity = nullptr;
+       dist = std::numeric_limits<float>::infinity();
+       for (Entity &cur_entity : entities) {
+               // TODO: better check for skipping self (because the check might not be for the player)
+               if (&cur_entity == player) {
+                       continue;
+               }
+               float cur_dist;
+               glm::vec3 cur_normal;
+               if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(player->ChunkCoords()), &cur_dist, &cur_normal)) {
+                       // TODO: fine grained check goes here? maybe?
+                       if (cur_dist < dist) {
+                               entity = &cur_entity;
+                               dist = cur_dist;
+                               normal = cur_normal;
+                       }
+               }
+       }
+
+       return entity;
+}
+
 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
        AABB box = e.Bounds();
        glm::mat4 M = e.Transform(player->ChunkCoords());