T *Layout::pick(const Ray &ray)
{
const set<Object *> &objs = objects.get();
+ T *closest = 0;
+ float distance = -1;
for(set<Object *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
if(T *t = dynamic_cast<T *>(*i))
- if(t->collide_ray(ray))
- return t;
-
- return 0;
+ {
+ float d = -1;
+ if(t->collide_ray(ray, &d))
+ if(!closest || d<distance)
+ {
+ closest = t;
+ distance = d;
+ }
+ }
+
+ return closest;
}
template Object *Layout::pick<Object>(const Ray &);