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

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

Topics: User Forum
Jul 31, 2012 at 7:02 PM
Edited Jul 31, 2012 at 7: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 )
FixtureFactory.AttachCircle(w / 2, Density, this.mySprite.body);

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

Aug 7, 2012 at 3: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 6: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.

Aug 28, 2012 at 12:49 AM

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.