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

Stability issues when adding removing bodies

Topics: Developer Forum, User Forum
Apr 18, 2010 at 10:50 PM
Edited Apr 18, 2010 at 11:22 PM

I see the big effort you all guys are making on bringing us that great thing that fp3 will be,

yet i'm stuck with the latest release version and I'm having big issues with the last feature i implemented in my wannabe great game : War Company.

Here's a video to show the problem, but i will explain it anyway:

the game is an arcade style game, the level is made by relatively small fixed tiles that i now want to be destroyed with rokets and grenades.

I obtain this by subtracting a circle geom from the ground polygon. So far so good, even if i found quite a major bug in the subtract polygon method, of wich we may discuss later on...

The problem appears always when i add/remove important geometries to the game world(bullets and rockets can be added and removed without a hint).

So, until now, i always took great care to never remove geoms to the simulator, disactivating all unwanted bodies and keeping all of them in the simulation, even if this was very unefficient performance-wise.

With the new destructible floor feature i stepped again on the same problem: i actually need to remove the old geom from the simulation to add the new, subtracted, geom (or geoms as the floor tile can be completely cut with enought firepower).

And clearly here it comes again: geoms start jumping around like in a superalcholic party...

Now i'm using the selective sweep algorithm for the broadphase collision, and the distance grid for the narrow phase.

What I would like to know is why there is such a problem, if there is some documentation about it, and if possible how to fix it.

Apr 20, 2010 at 5:27 PM

Since nobody replied and i was in big trouble ( i tried every single collision algorithm present and it appears to have something to do with the collision grid narrow phase collider), i'm trying to translate my game into farseer 3.

I was really impressed by the testbed, but my enthusiasm was fastly lowered by the amount of rework asked to the developer. The worst thing is the scaling... I love to have everything in metric units, but having to scale every single draw call is probably worst than having to so some math on defining masses and such. I guess this goes by to avoid nonsense masses and such.

I probably need to get used to the new farseer way as this is my first day...

Btw, i see the new way of building geoms (fixtures) from textures is a little troublesome for a old user. Can anybody help me fix that?

This is the old code:

//Create an array to hold the data from the texture
uint[] data = new uint[collisionTex.Width * collisionTex.Height];

//Transfer the texture data to the array
//Calculate the vertices from the array
List<Vertices> vertsList = PolygonTools.CreatePolygon(data, collisionTex.Width, collisionTex.Height,10,128,true,false);

//Make sure that the origin of the texture is the centroid (real center of geometry)
geoms = new List<Fixture>();
for(int i=0;i<vertsList.Count;i++)
Vector2 Delta_center = vertsList[i].GetCentroid() - _center;
//Use the body factory to create the physics body
geoms.Add(FixtureFactory.CreatePolygon(physicsSimulator,  vertsList[i], 1));


Apr 20, 2010 at 11:05 PM

Here is a snippet from TextureVerticesTest.cs -

public override void Initialize()
            //load texture that will represent the physics body
            _polygonTexture = GameInstance.Content.Load<Texture2D>("Texture");

            //Create an array to hold the data from the texture
            uint[] data = new uint[_polygonTexture.Width * _polygonTexture.Height];

            //Transfer the texture data to the array

            Vertices verts = PolygonTools.CreatePolygon(data, _polygonTexture.Width, _polygonTexture.Height, true);
            Vector2 scale = new Vector2(0.07f, 0.07f);
            verts.Scale(ref scale);

            _vertices = verts.ToArray();

            verts = BooleanTools.Simplify(verts);

            list = BayazitDecomposer.ConvexPartition(verts);

            if (list != null)
                colors = new Color[list.Count];
                Random random = new Random((int) DateTime.Now.Ticks);

                for (int i = 0; i < list.Count; i++)
                    colors[i] = new Color((byte) random.Next(100, 255), (byte) random.Next(100, 255),
                                          (byte) random.Next(100, 255));

            _polygonBody = World.Add();
            _polygonBody.BodyType = BodyType.Dynamic;
            _polygonBody.Position = new Vector2(0, 0);

            foreach (Vertices vert in list)
                if (!vert.IsConvex())
                    throw new Exception("eh..");

                PolygonShape shape = new PolygonShape(vert, 1);


This is how it is done now. I suggest writing a function that can load any texture and create a Body and automatically decompose the textures vertices as is done above.

Please let me know if this helps you.

Apr 21, 2010 at 11:34 AM
I have a higher level API for the texture-to-vertices tools on my todo list. I'll see if I can implement it this weekend.
Apr 21, 2010 at 1:01 PM
Well that would be really great, let me know if I may be of help in this direction.