Merging polygons problem

Topics: Developer Forum
Sep 1, 2009 at 10:56 PM

Hi I'm making a game using farseer at the moment and thus far it has been great.  I have now encountered a problem however and could use a little help.  Hopefully this is just from my misuse and misunderstanding of the engine and not a bug.

So I'm using XNA and I have a screen that "rains" primative shapes from top to bottom, they all end up in a big pile.  The shapes i wanted "raining" are squares circles and triangles.  For squares and circles i made use of the static factory methods the physice engine has and it was easy peasy.  However, for the triangle i tried to use the "CreatePolygonBody()" Factory method, which i clearly don't understand.

My triangle makes use of a simple texture, and is 34px by 34px.  Imagine a diagonal line running from the bottom left to the top right of the image.  The pixels underneath and to the right of that line are the ones that are opaque and make up the graphics the rest are transparent.  So i noticed the CreatePolygonBody takes a "Vertices" object as a parameter, which looks to be just a posh list that stores 2 dimensional vectors.  This is the bit i think im screwing up, I'm adding these vectors to my list (0,0) , (34,34) and (0,34) and using my 34 x 34 texture to render it in the draw method.  The geometry object i create after it takes the same list of vectors, it is not meant to be offset or anything.  I found that the texture didn't at all match it's assigned geometry when i ran it, and the triangles end up intersecting and hovering 10 or so pixels above  or underneath other objects etc.

Feeling like a fool i downloaded the advanced samples.  I noticed the texture to polygon example and copied the code for my project.  This appeared to work at first.  The triangles looked like they matched their geometries now and bounced around quite nicely.  However, very occasionaly two traingles seem to overlap each other  as if they have merged.  If a box then hits them they seperate and then behave normally.

I'm not sure where to go from here.  Any help massively appreciated.

 

 

 

 

 

 

 

 

Sep 1, 2009 at 11:46 PM

Sometimes, when there is too much pressure or when objects are moving too quickly for the physics simulator (with its current configuration) to create an accurate response to a collision, objects will pass through each other (tunneling), or potentially pass into each other and become stuck. Using SAT rather than DistanceGrid for a narrow phase collider significantly helps prevent objects from becoming stuck within one another. It's likely a one-line change, so I recommend you try that first.

Sep 2, 2009 at 8:58 AM

Thankyou very much for the quick response.  I will try this when i get home later.  Just out of interest if i switch to SAT should i expect a performance decrease?  Also, my triangles are just getting dropped from the top of the screen, the gravity vector is only set to (0,200), would that be enough to see object merge?

Sep 2, 2009 at 9:03 AM

With a gravity of 200 small to medium objects or thin objects may converge. SAT will eliminate this...

Sat will run much faster than distance grid if there are no concave polygons in your game and it loves simple objects like boxes and such. Things like circles and complex shapes will run a bit worse however... but they also have a lower creation time.

Sep 2, 2009 at 5:13 PM

Hi I'm having a little trouble switching to SAT.
The example from the manual shows:

PhysicsSimulator.NarrowPhaseCollider = new SAT(PhysicsSimulator);

However trying this will result in:

'FarseerGames.FarseerPhysics.Collisions.SAT' does not contain a constructor that takes '1' arguments

So i had a little dig into the SAT class, and noticed that it appears to be a singleton, that i can grab or instantiate with SAT.Instance.  However, the SAT class implements an interface INarrowPhaseCollider.  If i try to set it like this:

PhysicsSimulator.NarrowPhaseCollider = SAT.Instance;

I get:

Cannot implicitly convert type 'FarseerGames.FarseerPhysics.Collisions.SAT' to 'FarseerGames.FarseerPhysics.NarrowPhaseCollider'

Bleh!  I think this is probably because I am using XNA game studio 3.1 so had to get the latest Farseer source using Tortoise SVN.  How can i switch to SAT with the up to date engine?

Coordinator
Sep 2, 2009 at 5:23 PM

The SAT implementation was changed to simplify the structure.

Now you simply do this:

PhysicsSimulator.NarrowPhaseCollider = NarrowPhaseCollider.SAT;

Matthew was the one that wrote the SAT stuff in the manual and he has not been around to update it. I will not be updating it either as I'm concentrating on 3.0 and its manual.

Sep 2, 2009 at 8:54 PM

Thanks for this.
That certainly has fixed my problem too!