X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FLine.hh;h=a88405effe417bf8d3329dc362e3ae64038f6826;hp=33eecb9b3df6f91dec483b8022011eac8cdda2a4;hb=7f3984f3f9524f5b6813e01ceb2fe576dadff94e;hpb=99ac607f489023a7aa17bfb046113b0e4a65dab6 diff --git a/src/Moof/Line.hh b/src/Moof/Line.hh index 33eecb9..a88405e 100644 --- a/src/Moof/Line.hh +++ b/src/Moof/Line.hh @@ -56,8 +56,7 @@ struct Line : public Drawable, public Shape 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 +70,8 @@ struct Line : public Drawable, public Shape 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 +80,29 @@ struct Line : public Drawable, public Shape // 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