+Gear *Machine::intersect_gear(const Ray &ray, HitPoint *hitp) const
+{
+ Gear *res = 0;
+ HitPoint nearest;
+ nearest.dist = FLT_MAX;
+
+ for(size_t i=0; i<gears.size(); i++) {
+ Vec3 pos = gears[i]->get_global_position();
+ float rad = gears[i]->radius;
+
+ Plane plane = Plane(pos, gears[i]->axis);
+
+ HitPoint hit;
+ if(plane.intersect(ray, &hit) && hit.dist < nearest.dist &&
+ length_sq(hit.pos - pos) <= rad * rad) {
+ nearest = hit;
+ res = gears[i];
+ }
+ }
+
+ if(hitp) *hitp = nearest;
+ return res;
+}