]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/object.cpp
Report the closest object when picking
[r2c2.git] / source / libr2c2 / object.cpp
index 7a23d11a04e4779ee13da5e094a80a059cc4f147..7a896b6581b40c849e5a3296c513260038b4c230 100644 (file)
@@ -2,6 +2,7 @@
 #include "object.h"
 
 using namespace std;
+using namespace Msp;
 
 namespace R2C2 {
 
@@ -95,7 +96,7 @@ const Shape *Object::get_shape() const
        return get_type().get_shape();
 }
 
-bool Object::collide_ray(const Ray &ray) const
+bool Object::collide_ray(const Ray &ray, float *d) const
 {
        const Shape *s = get_shape();
        if(!s)
@@ -103,7 +104,16 @@ bool Object::collide_ray(const Ray &ray) const
 
        Transform reverse_trans = Transform::rotation(rotation, Vector(0, 0, -1))*
                Transform::translation(-position);
-       return s->check_intersection(reverse_trans.transform(ray));
+       if(d)
+       {
+               vector<Geometry::SurfacePoint<float, 3> > points = s->get_intersections(reverse_trans.transform(ray));
+               if(points.empty())
+                       return false;
+               *d = points.front().distance;
+               return true;
+       }
+       else
+               return s->check_intersection(reverse_trans.transform(ray));
 }
 
 BoundingBox Object::get_bounding_box() const