[Solved] Farseer Dev, Please Chime In: Body.FixtureList.Clear vs. Body.DestroyFixture(fixture)

Topics: User Forum
Jul 31, 2012 at 6:02 PM
Edited Jul 31, 2012 at 6:04 PM

Could someone please advise which method will work/should work for swapping out fixture/shape of a body.

Currently I am using:

mySprite.Body.FixtureList.Clear() ;
FixtureFactory.AttachCircle(w / 2, Density, mySprite);

but my frame rates drastically starts to slow down. I've narrowed down that drop in frame rates are happening after I start remove old/add new fixture/shape to my body.

I am planing on changing it to:

int count = 0;
while ( count < mySprite.FixtureList.Count )
{
    mySprite.body.DestroyFixture(mySprite.body.FixtureList[count]);
    ++count;
}
FixtureFactory.AttachCircle(w / 2, Density, this.mySprite.body);

Please advise if this will work or if there is another way of doing this.

Coordinator
Aug 7, 2012 at 2:16 AM

Do you have a lot of fixtures attached? If so, it might be the garbage collector that comes in and tries to clean up - that can cause a considerable lag.

If you are switching the shape out with another type, the only way is to create a new fixture with the new shape. If you change the shape (a polygon), you can take a look at what I wrote here.

You might want to take a look at my blog post regarding optimizing games for the Xbox 360. It covers object pooling, something you might want to take a look at.

Aug 7, 2012 at 5:17 AM

@Genbox - I probably was not as concise in my initial writing. Let me try that again.

I was able to resolve the issue, the solution to my issue was using the DestroyFixture(fixture) method to remove the fixture.

All my game objects use primitive shapes(circles, triangles, rectangles), that is one fixture per body. I do have a scale data member in my parent Sprite class, and maybe I could use that since I use primitive shapes but I am a newbie Farseer user and wanted to test the API calls to get familiar with its workings.

My question was from C# Collection perspective, FixtureList is a List<T>, therefore using the fixtureList.Clear() should clear the list, i.e.: all fixtures and it's dependencies, but from my experience that is not true. From a Farseer standpoint, the fixtureList.DestroyFixture(fixture) is the correct method that destroys the fixture(s) and all its dependencies.

 Correct me if I'm stating something incorrectly.

Coordinator
Aug 27, 2012 at 11:49 PM

That is correct. Clearing the list will only remove the reference to the object, and not destroy the actual object from the world. DestroyFixture() removes contacts and cleans up after itself, I would recommend that you use that whenever you need to remove a fixture from the world.