]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Line.hh
dispatch class not a singleton, engine is static
[chaz/yoink] / src / Moof / Line.hh
index 4a54e2d5f008c5bcd62c21340b552ffeb0eb809a..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>
@@ -49,7 +50,14 @@ struct Line : public Drawable, public Shape<D>
        Vector  a;
        Vector  b;
 
-       Scalar intersectRay(const Ray<2>& ray, Ray<2>::Intersection& intersection)
+
+       Line() {}
+
+       Line(const Vector& point1, const Vector& point2) :
+               a(point1),
+               b(point2) {}
+
+       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)
@@ -63,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]) +
@@ -72,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);
-
-               Scalar r = -(a[0] * (ray.point[1] - b[1]) +
-                                        b[0] * (a[1] - ray.point[1]) +
-                                        ray.point[0] * (b[1] - a[1])) / denom;
-
-               // 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;
+               if (s < SCALAR(0.0) || s > SCALAR(1.0)) return false;
 
-               // 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;
+               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;
 
-               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.02003 seconds and 4 git commands to generate.