Way to test if a geom completely contains/surrounds another geom?

Topics: Developer Forum, User Forum
Jan 24, 2009 at 2:00 AM
Hey.  I've been playing around with Farseer Physics for a while now, and I've found it to be a flexible and powerful API.  There's one thing I haven't quite figured out yet, though...

Is there a way that I can tell if a geom (or body) completely surrounds another geom (or body)?  I can tell if there's a point in an AABB or the Grid (obviously used for culling possible collisions until objects get close together), but I don't see an obvious way to see if a geom completely surrounds another geom.  Am I missing something?  Maybe something related to the Arbiter or ArbiterList?

Any help would be greatly appreciated!  It would be awesome if I could avoid writing this myself...

Jan 24, 2009 at 4:18 AM
Okay, well, this wasn't quite as tough as I thought it might be...

 public bool contains(Geom otherGeom)
            foreach (Vector2 vertex in otherGeom.WorldVertices)
                if (thisGeom.Collide(vertex) == false)
                    return false;
            return true;

If there's a built-in way to do this, I'd still be interested in hearing it.   :)
Jan 24, 2009 at 12:33 PM
We don't have something like this builtin. There are several ways of achieving this depending on how your game is set up.
Your solutions seems fine. It will test if the points from one geometry is 100% contained in another geometry.

Just for good mesure, I will post how to do it with AABBs. Someone might need it.
Here is how you do it:

if (((Min.X <= aabb.Min.X) && (aabb.Max.X <= Max.X)) && ((Min.Y <= aabb.Min.Y) && (aabb.Max.Y <= .Max.Y)))
    return true;

Warning: Code not tested. purely from mind - might contain errors.

I've made a note to have a look at this for future inclusion into FP engine.
Jan 26, 2009 at 8:29 AM
I have a method in my polygon operations extensions (PointInPolygonAngle) that does this test on the vertices which would be used in much the same way as Geom.Collide is used in DoYouWantKarate's method, except it does a raw polygon check instead of using the Grid.

Jan 26, 2009 at 12:33 PM
DrDeth, would you be willing to share your code for this?

Just last night I started running into some issues with complex geometries and the Collide method anyway.  It works pretty well when my geoms collide from the outside, but when I put one inside the other, the edge collisions get a lot "looser" and I get collisions in certain places when the objects are still many pixels apart. :(

Genbox, are you interested in seeing more about the problems I've been having?  I wonder if it's not related to the 'floating point accuracy errors' mentioned in the comments for the Grid.Intersect method...
Jan 26, 2009 at 12:36 PM
Edited Jan 26, 2009 at 12:39 PM
I've seen ghost collision for complex geometries before. I would like to see your code to reproduce this. Thanks for reporting this.

EDIT: @DoYouWantKarate: You can find DrDeth's code here: Polygon manipulation
Feb 8, 2009 at 3:55 PM
@DoYouWantKarate: Are you able to send me a demo to show this bug?
Feb 10, 2009 at 1:20 AM
Hey Genbox,

Thanks for posting DrDeth's code.  I never got an email notification about that 1/26 post.  Must have been spam-blocked for some reason. :(

About 3 nights ago I had a revelation, and realized that the grid cell size probably just needed to be tweaked for the more complex geometries.  By changing the grid cell size to smaller values (sometimes as low as 1 or 2), I was able to dramatically reduce the number of ghost collisions.  So I'm guessing that this is the correct behavior.

And, just to make things clear, I'm using the framework in kind of a strange way.  I'm trying to test for collisions while objects are already inside each other.  I got far fewer ghost collisions when I tested the same geometries using only external collisions (where the larger object doesn't contain the smaller one.)

Do you still want to see an example, even though it was just a CollisionGridCellSize issue?


Feb 10, 2009 at 3:36 AM
I believe I already tested if it was a grid precision issue (grid cell size), if it indeed is the precision of the grid, I must have done something wrong when I first tested it. Thanks for pointing it out, I will retest it.
No need to see a demo if it's the case.