How to flip/mirror a geometry?

Topics: Developer Forum, User Forum
Oct 20, 2009 at 3:33 AM


I've been using Farseer for a while, now, and it's super cool, but I've finally found something that doesn't look like it's easy to do.

I'd like to take a geom and flip/mirror it over the x-axis around its center.  (For example, when a character in a side-scroller turns from the left to the right.)

I've tried manually updating the vertices in place in the vertices collection, and also copying the vertices collection, updating it, and then calling setVertices() with the updated vertices.  Both of these options flip the geom, but both also break collision detection in the simulation.

Right now I'm looking at creating two geoms for each of my bodies, and then enabling/disabling collision detection on the geoms when it's time to flip, but this would cause some minor refactoring of my code.

So, I figured I'd ask.  Am I missing something here?  Is there an easy way to transform my vertices and flip them over their center?



Oct 20, 2009 at 5:17 AM

Okay, so I think I got this working better, simply by switching to SAT for my NarrowPhaseCollider.  Then I update the LocalVertices in place with a FlipOverXAxis method that I added to the Vertices class.  So far so good.

I'd still be interested in anyone else's ideas on how to PROPERLY flip/mirror a geom. :)

If this really is the right way to do things, let me know if you want me to submit my code to flip the geom.  I can easily add a method that does the Y-axis, too.

BTW, while trying to switch NPCs, I think I found an outdated example in the docs for 2.1.1. 

The doc has this line in the section about SAT: 

  • PhysicsSimulator.NarrowPhaseCollider = new SAT(PhysicsSimulator);

I think it probably needs to say, because the line above doesn't compile:

  • PhysicsSimulator.NarrowPhaseCollider = NarrowPhaseCollider.SAT;





Oct 22, 2009 at 12:48 AM

Hi Chris, it sounds like you've thought of all the standard ways you'd flip a geom. You are correct that mirroring a geom can break a simulation, and that is probably why it's not directly supported in the physics engine. What you have done is a good solution though. BTW, yes, some of the code examples are old and could use some updating, but the major contributors are looking to Farseer Physics 3.0, so the current documentation is of low priority to them right now.

Oct 23, 2009 at 6:23 AM

Cool.  Thanks, Cowdozer.

At least now I know I wasn't just missing something obvious. :)


Oct 24, 2009 at 1:29 PM

If you flip the vertices, it invalidates the distance grid for that Geom (assuming an asymmetrical geometry along the axis you flip it).  This is why collisions no longer work properly.  You could try forcing the engine to recompute the distance grid:


// Update the vertices

// Update the distance grid
DistanceGrid.Instance.RemoveDistanceGrid(_geom);  // remove the old grid
DistanceGrid.Instance.CreateDistanceGrid(_geom);  // compute a new grid

Obviously, it might impact performance of your game if you do this often.  Also, this is not a "proper" way to do it, but works in my experiences :)

Oct 25, 2009 at 4:06 AM

Thanks, Sam!

That makes a lot of sense about the distance grid.

That's probably why it worked when I switched to the SAT narrow phase collider, right?  As I understand it, the SAT NPC doesn't use the distance grid at all...

It's good to know how that distance grid works, though.  I'll have to keep that one in the back of my head.

If you haven't tried it, try switching to the SAT NPC.  It really helped performance and accuracy for my game.


Oct 26, 2009 at 5:32 AM

Yes, that's true that Distance Grids are not used or calculated when using SAT.

Oct 26, 2009 at 6:06 PM

I could set it to automatically recalculate the the distance grid in the SetVertices() method. There are also one other factor to take into consideration; the center of the geometry is the centroid of the vertices by default. You will have to translate the vertices to be centered around the centroid before submitting the vertices to the SetVertices() method to get a consistent behavior. Take a look at the GeomFactory.CreatePolygonGeom() method for more info on how to translate the vertices relative to the centroid.

The best solution when using the distance grid is to pre-create the two geometries (one facing left and the other facing right) and switch them out whenever you make your character turn.

I will update the documentation later today with the SAT narrow phase stuff. As Cowdozer wrote, I'm focusing more on Farseer Physics Engine 3.0 - I will also soon be calling for some editors to help out with the documentation and Wiki. (Editor role was just included here on Codeplex)

Mar 8, 2010 at 1:18 PM
Edited Mar 8, 2010 at 2:44 PM

Would it be better to go around making a "FlipOverXAxis" method or to just have an entirely separate geom. I can't decide what would be best. As of right now I have a geom for each body part attached and for each direction it can face.