10
\$\begingroup\$

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?

\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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
(source: co.uk)

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.

\$\endgroup\$
0
\$\begingroup\$

I know this question is old but I feel like its missing a simple, code-based answer. So here it is:

    /// <summary>
    /// Return if a given position is inside the physical body.
    /// </summary>
    /// <param name="body">Body to test.</param>
    /// <param name="position">Position to check if inside the body.</param>
    /// <returns>If given point is inside the physical body.</returns>
    public bool TestPointCollision(FarseerPhysics.Dynamics.Body body, Vector2 position)
    {
        // get body transformation
        FarseerPhysics.Common.Transform trans;
        body.GetTransform(out trans);

        // iterate fixtures to see if any of them hit the point
        foreach (var fix in body.FixtureList)
        {
            if (fix.Shape.TestPoint(ref trans, ref position))
                return true;
        }

        // if there are no hits, return false
        return false;
    }

Note that this is not plain Box2D but Farseer (and in C#), but it should have the exact same API.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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