]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Line.hh
dispatch class not a singleton, engine is static
[chaz/yoink] / src / Moof / Line.hh
index 33eecb9b3df6f91dec483b8022011eac8cdda2a4..0747e280fffe42e6449569cc6fe90566fb16db1f 100644 (file)
@@ -33,6 +33,7 @@
 #include <Moof/Math.hh>
 #include <Moof/OpenGL.hh>
 #include <Moof/Ray.hh>
+#include <Moof/Shape.hh>
 #include <Moof/Texture.hh>
 
 #include <Moof/Log.hh>
@@ -56,8 +57,7 @@ struct Line : public Drawable, public Shape<D>
                a(point1),
                b(point2) {}
 
-       Scalar intersectRay(const Ray<2>& ray,
-                       Ray<2>::Intersection& intersection) const
+       bool intersectRay(const Ray<2>& ray, Ray<2>::Intersection& hit) const
        {
                // solve: Cx + r*Dx = Ax + s(Bx - Ax)
                //        Cy + r*Dy = Ay + s(By - Ay)
@@ -71,7 +71,8 @@ struct Line : public Drawable, public Shape<D>
                                           ray.direction[1] * (a[0] - b[0]);
 
                // check if the ray and line are parallel
-               if (isEqual(denom, SCALAR(0.0)))
+               //if (isEqual(denom, SCALAR(0.0)))
+               if (denom == SCALAR(0.0))
                {
                        Scalar numer = a[0] * (ray.point[1] - b[1]) +
                                                   b[0] * (a[1] - ray.point[1]) +
@@ -80,35 +81,29 @@ struct Line : public Drawable, public Shape<D>
                        // check if they are collinear
                        if (isEqual(numer, SCALAR(0.0)))
                        {
-                               intersection.point = ray.point;
-                               intersection.normal.set(0.0, 0.0);
-                               return SCALAR(0.0);
+                               hit.distance = SCALAR(0.0);
+                               hit.normal.set(0.0, 0.0);
+                               return true;
                        }
 
-                       return SCALAR(-1.0);
+                       return false;
                }
 
                Scalar s = (ray.direction[0] * (ray.point[1] - a[1]) +
                                        ray.direction[1] * (a[0] - ray.point[0])) / denom;
 
                // check if the ray hits the segment
-               if (s < SCALAR(0.0) || s > SCALAR(1.0)) return SCALAR(-1.0);
+               if (s < SCALAR(0.0) || s > SCALAR(1.0)) return false;
 
-               Scalar r = -(a[0] * (ray.point[1] - b[1]) +
-                                        b[0] * (a[1] - ray.point[1]) +
-                                        ray.point[0] * (b[1] - a[1])) / denom;
+               hit.distance = -(a[0] * (ray.point[1] - b[1]) +
+                                                b[0] * (a[1] - ray.point[1]) +
+                                                ray.point[0] * (b[1] - a[1])) / denom;
+               if (hit.distance < SCALAR(0.0)) return false;
 
-               // make sure we're dealing with the right side of the ray
-               if (r < SCALAR(0.0)) return SCALAR(-1.0);
-
-               intersection.point = ray.point + r * ray.direction;
-
-               // gotta use the correct normal
-               Vector n = cml::perp(a - b);
-               if (cml::dot(a - ray.point, n) < 0) intersection.normal = n;
-               else                                intersection.normal = -n;
-
-               return r;
+               Vector normal = cml::perp(a - b);
+               if (cml::dot(a - ray.point, normal) < 0) hit.normal = normal;
+               else                                     hit.normal = -normal;
+               return true;
        }
 
        void draw(Scalar alpha = 0.0) const
This page took 0.019562 seconds and 4 git commands to generate.