Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Is there a way to check if a point is intersecting a body?

For example, lets say there's a body in the world at a position of (100,100).

Is there a way to check if a point at (100,100) intersects that body?

Another example is related to a game called "PHUN". In PHUN you can click and drag bodies around. How would you check if a mouse point is intersecting a body?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

As with colin's answer, b2World.Query() is a useful function, if you set the aabb to cover only a single point then you can get a list of objects that may overlap that point. However, due to speed concerns it only checks the bounding box (AABB) for an overlap which is not always entirely accurate.

To check a fixture (shape, in older versions) accurately, use the b2Fixture.TestPoint() function. As a body can have multiple fixtures, you may need to loop through the entire list to be sure. Combining those 2 functions should allow you to recreate the functionality of Phun, if you like.

share|improve this answer

If you have a point you know is inside the shape then you can intersect the line joining the mouse location to that point with the boundary of the shape. If there are an odd number of intersections then the point is inside. If there are an even number of intersections then the point is outside.

alt text

The green dots are the mouse locations, the red is the know point inside the shape.

There are edge cases - if the line joining the points coincides with an edge, for example - but this should get you 90% of the way there.

share|improve this answer

Take a look at b2QueryCallback for finding what fixtures contain a point and use b2MouseJoint for having objects follow a mouse. I have some sample code, but no time to simplify it for you right now. If you need it, I can provide it later.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.