This project has moved and is read-only. For the latest updates, please go here.

geom collisons with rectangles

May 13, 2009 at 12:06 PM
i have looked in the manual and can not find if this is possible so here is my question. is there any way of detecting a collission bettween a geom and a rectangle? thx for you help
May 13, 2009 at 4:57 PM
Edited May 13, 2009 at 4:58 PM

I would like a confirmation


definatley no(you can do alternative techniques though)

the geom.collides method can check against a vector2 or another geom

the rectangle intersection method can check against other rectangles or check rectangle.contains for a point,

there are ways you could check the collision though

you could create a geometry to match your rectangle and check if it collides(works but may be innacurate and inneficient)

you could create a rectangle around your geometry when you check collision to see if it intersects(this would be easier and probaly more efficient but it would limit to strictly rectangle boundary checks)

OR You could do a foreach loop on each vertice in the geomtry and check if the rectangle contains each one of the points(best of both worlds just a tad more code and possibly inefficient nothing major)

foreach(Vector2 v in geom.WorldVertices)


Point p = new Point(v.X, v.Y);



collision = true;

//Break the loop cause youve already found the collision






you can try one of those out for now, but check back to see what other people say as I am not the professional around here

May 13, 2009 at 5:04 PM

If the rectangle you need to check against, is not oriented (Is not axis aligned) then you will have to check each vertice on the geometry against the rectangle.

If your rectangle is oriented, then it is indeed possible. All geometries in Farseer Physics carry around a AABB (Axis Aligned Bounding Box). It's a rectangle that covers the geometry. You can create such a AABB and check against a geometry by simply doing: AABB.Intersect(myAABB, geometry.AABB).

May 13, 2009 at 5:58 PM

if my previous posters are making it more complicated than i think and you really just want basic collision detection then...

make a Geom

make another Geom by GeomFactory.CreateRectangleGeom(...). note: rectangular Geoms are also Geoms, as they are all treated as polygons (maybe set IsStatic = true if you dont want it to move)

add a collision handler by: RectangleGeom.OnCollision += OnCollision

implement the OnCollision() method


May 14, 2009 at 4:36 AM

@Genbox: Yeah your way is probaly best, I never made an independant AABB so i want aware you could create one seperately,

the techniques i specified were a tad complicated, but i think it should be used only if you cant use the AABB (in the situation genbox specified)

May 14, 2009 at 10:01 AM
thx that is a big help.
May 14, 2009 at 12:12 PM

@tlegion: I've just uploaded a new version of AABB. It should be more easy to create an independant AABB and position it correctly. AABBs are as simple as they come, they are used in collision detection only and has no physics applied to them.

@riddler: No problem. Both tlegion and yobiv are excellent help here on the forums. Be sure to ask for help if you have any troubel setting Farseer Physics up.