]> git.localhorst.tv Git - blank.git/blobdiff - src/world/chunk.cpp
simplify ray/chunk intersection test
[blank.git] / src / world / chunk.cpp
index cc305cd56a4b293f8aa1df121e797033f6e261b0..9e95458ef45ac7b9b818cd40d32193bc52fc9f51 100644 (file)
@@ -359,7 +359,7 @@ bool Chunk::IsSurface(const RoughLocation::Fine &pos) const noexcept {
 
 bool Chunk::Intersection(
        const Ray &ray,
-       const glm::mat4 &M,
+       const ExactLocation::Coarse &reference,
        WorldCollision &coll
 ) noexcept {
        int idx = 0;
@@ -375,7 +375,7 @@ bool Chunk::Intersection(
                                }
                                float cur_dist;
                                glm::vec3 cur_norm;
-                               if (type.shape->Intersects(ray, M * ToTransform(RoughLocation::Fine(x, y, z), idx), cur_dist, cur_norm)) {
+                               if (type.shape->Intersects(ray, ToTransform(reference, RoughLocation::Fine(x, y, z), idx), cur_dist, cur_norm)) {
                                        if (cur_dist < coll.depth) {
                                                coll.block = idx;
                                                coll.depth = cur_dist;
@@ -560,6 +560,10 @@ glm::mat4 Chunk::ToTransform(const RoughLocation::Fine &pos, int idx) const noex
        return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
 }
 
+glm::mat4 Chunk::ToTransform(const ExactLocation::Coarse &ref, const RoughLocation::Fine &pos, int idx) const noexcept {
+       return glm::translate(ExactLocation::Fine((position - ref) * ExactLocation::Extent()) + ToCoords(pos)) * BlockAt(idx).Transform();
+}
+
 
 BlockLookup::BlockLookup(Chunk *c, const RoughLocation::Fine &p) noexcept
 : chunk(c), pos(p) {