]> git.localhorst.tv Git - blank.git/commitdiff
trying to fix the initial aiming issue
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 24 Mar 2015 16:45:29 +0000 (17:45 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 24 Mar 2015 16:45:45 +0000 (17:45 +0100)
not successful

TODO
src/entity.cpp
src/geometry.cpp
src/interface.cpp

diff --git a/TODO b/TODO
index 363c635551cfa384065abbd85e03f87b86e79f9e..29fd94f455fd3353136ec2758565f383e4a8d195 100644 (file)
--- a/TODO
+++ b/TODO
@@ -76,6 +76,8 @@ entity/world collision
 
        entities should be stopped from entering solid parts of the world
 
+       also, current ray/obb intersection test sucks
+
 better noise
 
        current simplex noise implementation repeats itself pretty quickly
index 52502c26f256aceebcd7b2a30958cf98ec84c9ff..23586010e1183f4238420d73e5ba458414748711 100644 (file)
@@ -94,7 +94,7 @@ glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const {
 
 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const {
        glm::mat4 transform = Transform(chunk_offset);
-       glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
+       glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
        from /= from.w;
        glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
        to /= to.w;
index 44217e981b49601c68be580eead0385efa6b45e2..05387a5bd10036f2798ccc2a0c1655c82ce49743 100644 (file)
@@ -13,12 +13,11 @@ bool Intersection(
        glm::vec3 *normal
 ) {
        float t_min = 0.0f;
-       float t_max = 1.0e5f;
+       float t_max = std::numeric_limits<float>::infinity();
        const glm::vec3 aabb_pos(M[3].x, M[3].y, M[3].z);
        const glm::vec3 delta = aabb_pos - ray.orig;
 
        glm::vec3 t1(t_min, t_min, t_min), t2(t_max, t_max, t_max);
-       bool x_swap = false, y_swap = false, z_swap = false;
 
        { // X
                const glm::vec3 xaxis(M[0].x, M[0].y, M[0].z);
@@ -29,21 +28,14 @@ bool Intersection(
                        t1.x = (e + aabb.min.x) / f;
                        t2.x = (e + aabb.max.x) / f;
 
-                       if (t1.x > t2.x) {
-                               std::swap(t1.x, t2.x);
-                               x_swap = true;
-                       }
-                       if (t1.x > t_min) {
-                               t_min = t1.x;
-                       }
-                       if (t2.x < t_max) {
-                               t_max = t2.x;
-                       }
+                       t_min = std::max(t_min, std::min(t1.x, t2.x));
+                       t_max = std::min(t_max, std::max(t1.x, t2.x));
+
                        if (t_max < t_min) {
                                return false;
                        }
                } else {
-                       if (aabb.min.x - e > 0.0f || aabb.max.x < 0.0f) {
+                       if (aabb.min.x - e < 0.0f || -aabb.max.x - e > 0.0f) {
                                return false;
                        }
                }
@@ -58,21 +50,14 @@ bool Intersection(
                        t1.y = (e + aabb.min.y) / f;
                        t2.y = (e + aabb.max.y) / f;
 
-                       if (t1.y > t2.y) {
-                               std::swap(t1.y, t2.y);
-                               y_swap = true;
-                       }
-                       if (t1.y > t_min) {
-                               t_min = t1.y;
-                       }
-                       if (t2.y < t_max) {
-                               t_max = t2.y;
-                       }
+                       t_min = std::max(t_min, std::min(t1.y, t2.y));
+                       t_max = std::min(t_max, std::max(t1.y, t2.y));
+
                        if (t_max < t_min) {
                                return false;
                        }
                } else {
-                       if (aabb.min.y - e > 0.0f || aabb.max.y < 0.0f) {
+                       if (aabb.min.y - e < 0.0f || -aabb.max.y - e > 0.0f) {
                                return false;
                        }
                }
@@ -87,41 +72,39 @@ bool Intersection(
                        t1.z = (e + aabb.min.z) / f;
                        t2.z = (e + aabb.max.z) / f;
 
-                       if (t1.z > t2.z) {
-                               std::swap(t1.z, t2.z);
-                               z_swap = true;
-                       }
-                       if (t1.z > t_min) {
-                               t_min = t1.z;
-                       }
-                       if (t2.z < t_max) {
-                               t_max = t2.z;
-                       }
+                       t_min = std::max(t_min, std::min(t1.z, t2.z));
+                       t_max = std::min(t_max, std::max(t1.z, t2.z));
+
                        if (t_max < t_min) {
                                return false;
                        }
                } else {
-                       if (aabb.min.z - e > 0.0f || aabb.max.z < 0.0f) {
+                       if (aabb.min.z - e < 0.0f || -aabb.max.z - e > 0.0f) {
                                return false;
                        }
                }
        }
 
+       glm::vec3 min_all(min(t1, t2));
+
        if (dist) {
                *dist = t_min;
        }
        if (normal) {
-               if (t1.x > t1.y) {
-                       if (t1.x > t1.z) {
-                               *normal = glm::vec3(x_swap ? 1 : -1, 0, 0);
+               glm::vec4 norm(0.0f);
+               if (min_all.x > min_all.y) {
+                       if (min_all.x > min_all.z) {
+                               norm.x = t2.x < t1.x ? 1 : -1;
                        } else {
-                               *normal = glm::vec3(0, 0, z_swap ? 1 : -1);
+                               norm.z = t2.z < t1.z ? 1 : -1;
                        }
-               } else if (t1.y > t1.z) {
-                       *normal = glm::vec3(0, y_swap ? 1 : -1, 0);
+               } else if (min_all.y > min_all.z) {
+                       norm.y = t2.y < t1.y ? 1 : -1;
                } else {
-                       *normal = glm::vec3(0, 0, z_swap ? 1 : -1);
+                       norm.z = t2.z < t1.z ? 1 : -1;
                }
+               norm = M * norm;
+               *normal = glm::vec3(norm);
        }
        return true;
 }
index 45439da0e9a1de63c497063f192a6109c60434b9..b5aac9641061038a294fa5dea4a541ba8d19a546 100644 (file)
@@ -105,6 +105,8 @@ void Interface::PrintBlockInfo() {
        std::cout << std::endl;
        if (!aim_chunk) {
                std::cout << "not looking at any block" << std::endl;
+               Ray aim = ctrl.Aim();
+               std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
                return;
        }
        std::cout << "looking at block " << aim_block