How to get distance from a point to a shape/fixture

May 17, 2011 at 12:50 PM

I want to know the distance from a particular point to a particular shape.  The point is just a point in the World somewhere and the shape is attached to a body by a fixture.

At the moment, I'm just doing some raycasting, but it doesn't necessarily get me the closest point unless I send out lots of rays.

Is there a different approach I could consider?

May 17, 2011 at 4:08 PM

Have you tried using the body's world position and use the distance formula from the point you're looking at?

May 17, 2011 at 4:36 PM

Yes, that does give me an approximation, but there are cases when it is wrong enough to cause me trouble.

For example, say my point in space is near one end of a long, thin plank-shaped Shape.  The associated Body's position of that Shape can be a long way from the end of the plank, if the plank is long.  So the distance formula in this case makes it look like the point in space is a long way from the Shape/Body but really it is very close.

May 18, 2011 at 7:12 AM

Hrm...if your shape is simple enough like a quad, you could try getting the vertices for that shape.  I guess from there, you can find the shortest distance from your object's point to the 4 vertices?

May 18, 2011 at 1:27 PM

Yes, that could be a better approximation, I will investigate that, thanks.

May 19, 2011 at 9:37 PM

Check out FarseerPhysics.Collision.Distance.ComputeDistance. You can use this to find the closest points between two shapes, and I'm fairly certain a CircleShape with a Radius of 0 is legit.