Reusing objects in Farseer 3

Topics: Developer Forum, User Forum
Aug 8, 2010 at 6:54 PM

Hi,

We've been working on porting a game from our own basic physics to Farseer 3. We used to modify a lot of objects during the physics update which has caused a few issues, other than that everything seemed to work very well while debugging on PC. This weekend I started the game up on my Xbox and noticed that the performance were way down. Most notably when shooting or other times when objects were "added". So my first thought was garbage. When checking I noticed that from very little to no garbage we now had major garbage issues. All objects in the game is reused. I've previously made a few games using Farseer 2.x. When reusing a gameobject with a Farseer 2 body/geom I usually just remove them from the Physics simulator and then re-add them when I activate that gameobject again. When first implementing Farseer 3 I tried to use this method but quickly noticed that when removing a body from the "world" it's disposed. So I started using body.Enabled = false/true instead. This seemed to work great but now I'm not so sure since this is the only place I can think of that would generate all the garbage. Can it be that CreateProxies which is run when Enabling a body allocates memory?

Am I completely off? What's the best way to reuse objects?
I must say that other than my personal incompetence that has created this problem I've had absolutely no issues with Farseer 3. I'm amazed of how solid it feels.

Using build 72634.

Please excuse my poor English.

Coordinator
Aug 11, 2010 at 2:44 PM

The best way to reuse objects is to use the enable/disable property. I've not fully optimized it yet, so there might be some performance problems with it. However, removing and adding bodies from the engine should have little to no overhead. Your objects are removed from the engine and thus you need to keep a reference to the body yourself in order to keep it.

I'll check out the garbage generation inside the engine i the next few days.

Aug 11, 2010 at 8:49 PM

Thank you,

When firing a weapon that enables and disables about 20 body’s the Xbox generates a big bunch of garbage, runs the collector and very noticeably stops for a while. Of course this isn't a problem on PC though. Before the switch we had 60-50 FPS in 4 player split screen on the Xbox, now the game runs very unstable on about 30-20FPS when nothing extra is generated. From what I understand FP3 should be about the same to faster than FP2. But when we tested the game with FP2 we had better performance results than with our own engine(at least on levels with a lot of objects, otherwise about the same, and no garbage). So I'm probably using the engine the wrong way in some other way too. I'm going to try to find more performance issues, but it's hard until I find a way to get rid of all the garbage. All tests on Xbox in Release.

The last time I tried to add/remove bodies the game crashed, hard. I do keep an reference to both body and fixture in the game object itself, but maybe I missed something. I dismissed the idea pretty quickly after I reading through the Box2D manual. I understood it as it wasn’t supposed to work, that the reason that the game crashed was because when removing a body from the engine the attached fixtures and joints was destroyed, which also fitted in when debugging the crashes. In either case it crashed every time I tried to re-add a body. I'm going to test the add/remove again tomorrow and see what I missed.

Once again, thank you for taking the time!

Coordinator
Aug 11, 2010 at 10:04 PM
That is right; attached joints are destroyed when removing the body. If you find code that generates garbage, be sure to let me know. I'm sure there are a few places where we can optimize.
Aug 15, 2010 at 12:47 AM
Edited Aug 15, 2010 at 3:07 PM

I've put down a few more hours on the garbage, but I can't figure out what I'm doing wrong. I've now updated to the latest FP3 build 75526. I let go of the large problem for the moment and started at the bottom, just getting rid of all base garbage. I get garbage every time my Dynamic test object collides(at least with a static one). It doesn't matter if I have any events hooked up or not(like OnCollision or BeginContact). If I add gravity and the objects fall down it's only when they initially hit a static object garbage is generated, not by lying there.

XNA Framework Remote Performance Monitor, Delta results

A Dynamic object lying still on a static sensor:
Managed Bytes Allocated: 5000-6000
Managed Objects Allocated: 200-300

Dynamic object repeatedly forced in to a wall(static):
Managed Bytes Allocated: 2800-3000
Managed Objects Allocated: 100-300

Any idea what I might be doing wrong?

EDIT:
I changed all static objects to dynamic ones. No obvious garbage when objects colliding. But a wake object standing on a sensor still generates gabage, but in difference to if the sensor is static theres only garbage if you first wake the object..

EDIT2:
Still testing, I've been doing the same things as last edit on your new simplesample. I added a static rectangle and a sensor rectangle to sample 1. I was quirous why there was no garbage there. I updated my game so it uses Fixturefactory.Create.... instead of as in the old testbed PolygonTools.Create... -> BodyFactory.CreateBody... -> body.CreateFixture. This change removed all garbage when colliding with objects. But not the sensor issue. So then I changed to a Circle which most our moving objects are in the simplesample. Now the SimpleSample sensor generates garbage to. So it seams like the sensor issue is:
CircleObject(maybe all non rectangle) on sensor.
When testing to change all gameobjects to rectangles almost all garbage disapeared not only onsensor garbage but also the main problem I explained in my first post.

Coordinator
Aug 15, 2010 at 8:04 PM

Good work. I've been digging a little myself and so far I've not found anything significant - I've not tested wake/sleeping bodies and sensors tho. Could you send me a sample that generates garbage in the different scenarios you describe?