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

Setting new vertices to a geom

Topics: User Forum
Oct 12, 2009 at 6:49 PM


I'm working on tool that allows for animating sprites easily. I will also handle collisions using the same tool. However with animated sprites, collision boundaries change by frame and direction so I'm thinking of setting new vertices (geom[index].SetVertices(frame.Body[index].Vertices)) for each frame of the animation. 

I'm only using Farseer's collision detection (geom.IsSensor = true).

Will I run in to any big problems such as performance or glitches?

Oct 12, 2009 at 11:03 PM

Most probably not, as I have the same problem of re-constructing vertex buffers each time after Geom' vertices are changed. It would've been great if you can address these vertices by ref, but after all 3D APIs are based on fixed pipeline where you feed vertex by vertex each drawing call.

Oct 13, 2009 at 2:41 PM

Recreating the collision grid every frame could possibly be a problem.  You may want to try pre-creating a geom for each frame and just reuse them, adding/removing them from the simulation each frame as needed.  I wonder if this would work?

Oct 19, 2009 at 7:12 PM

Hey Guys,

I'm looking at a similar problem, and I did have issues with collisions.  I wrote a method to mirror the geom over its center on the x-axis.  (This'll just flip it so it's facing the other way, as when a character turns to face from left to right.)

I grab the local vertices, flip them over the x-axis, and then call setVertices().  Even after calling setVertices, the collision detection doesn't quite work right.  It still seems to be detecting the geom's original verts.

Oddly enough, if I change the geom's rotation, it seems to catch up and realize that there are new vertices.

I believe that part of my problem is that I'm allowing the user to move my geoms with the mouse 1:1, so they can click and drag.  I'm changing the positions and rotations of the bodies directly, without applying impulses, which I'm sure screws things up.

So, yes, you might have some issues if you change the vertices while the simulation is running, but it seems like it's still worth a try.  I would also worry a little bit about adding new geoms to the simulation in mid-flight, as this as proven to be expensive when I tried it, but I have a small collision grid cell size, too.  I wonder if you could add all of the geoms when the sprite was added, and just turn collision on for the one that corresponds to the current frame of your animation?

Anyway, I'm still trying to figure out how to solve my problem of flipping/mirroring a geom without breaking the simulation, if anyone has any suggestions...


Oct 22, 2009 at 1:34 AM

You might want to look into what a Distance Grid is and when it is updated. The default narrow phase collision algorithm uses Distance Grids to calculate depth of penetration, and every time a geom is altered (translations and rotations do not apply), the Distance Grid must be recalculated. An alternative narrow phase algorithm is SAT, and for your situation it may work better, because there is no precomputation overhead like there is with Distance Grid.