Sep 30, 2012 at 4:13 PM
Edited Sep 30, 2012 at 4:17 PM
Depends how many bodies you have and what perfrormance you need.
foreach(body in World.BodyList) distanceSquared=(body.Position-point.Position).LengthSquared; if (distanceSquared<=d*d) Action();
Considering a moderately big world (about 500 bodies) that's an easy way with just a few calculations (use LengthSquared to avoid a square root).
Important note: you wrote bodies so I gave you a calculation for bodies. A body has a position and is infinitely large. It is a plane whose origin lies at Body.Position. What you probably need is not bodies but fixtures which are shapes on the plane. The
fixture, however, may be fat, it may be any polygon etc. So when you speak of a distance to a fixture you really mean the distance to the border and not to the center even if the fixture is attached to the center of the body. That's a more complicated problem. There
are classes in Collision/Distance.cs that implement the necessary algorithms.
Since calculating a precise distance to every fixture is expensive in the general case you need a so-called spatial query. In Farseer it is achieved using World.QueryAABB where the center of the axis-aligned bouding box is your point, the dimensions are
your distance*2. The callback will give you a list of fixtures overlapping the AABB. Since the space spanning a maximum distance d from a point is a disk, whereas the AABB is a box in which the disk is inscribed, you might want to refilter the results further
to make sure they are inside the disk and not between the border of disk and the border of the box.
Another way that I have used successfully: shoot rays (World.Raycast()) from the point in a circular manner from 0 to 360 degrees. Performance will depend on your angle step and can be tuned. Get only the first intersection of the ray. This has some benefits
as it provides additional information like line of sight (!), the intersection point and the normal of the edge.
If you need this to happen often you might want to add a circular fixture to a ghost body centered at the point and set it to sensor so it reports collisions.