edges and performance

Topics: User Forum
Dec 18, 2012 at 10:04 PM



I'm trying to make a 2d sidescroller racing game. I generate a fairly large scenario using edges, however the computer crawls. I'm using 8192 edges to generate a 2d scenario. Is that too much?


In order to avoid this problem, I decided to create bodies with "a pack of edges", so that the scenario is divided i.e. in 128 bodies having 32 edges everyone. To my surpise, the speed is around the same.


It's also curious that the time that takes to generate the edges seems to increase exponentially with the numbre of edges inserted (with FarseerPhysics.Factories.BodyFactory.CreateEdge) . It takes less than 1/4 sec to introduce 1024 edges, but introducing 8192 edges takes more than 15 seconds. No matter if I use edges alone, or blocks of 32 edges "grouped by" bodies.


Is there any other solution before I try to generate them on the fly as I advance ? (I'll be having problems with the garbage collector then)


Thanks a lot!


Dec 19, 2012 at 8:00 PM

I answered a question similar to this not so long ago.

The 15 seconds you see are probably because the broad phase is not that happy about large batch inserts.

Dec 20, 2012 at 10:04 PM



thanks for your answer Genbox! :)


The library was compiled in debug, and that speeded up things a little bit :) However it's still quite slow:

I'm following the tips in "One Body, Multiple Fixtures" which is the one that should probably apply to my problem, but I'm not getting good results. My code is simply this:

    body=new Body(g.world);

    for (int i=0;i<vertices.Count-1;i++)
        FixtureFactory.AttachEdge (vertices[i],vertices[i+1],body);


where vertices contains 8192 vertices. It still takes around 6 seconds in a 2.7Ghz dual core, and forever on windows phone 7. Maybe I'm doing something wrong?

Just in case it's useful to you, using the profiler it seems the hot path is:



simulation is also quite slow (specially in WP7)


thanks!! :)


Dec 21, 2012 at 4:10 AM

Yep, as I said, it is caused by the broad phase. It is a known problem and it happens when you insert large amount of objects in a structured manner. You have 2 ways of fixing it at the moment:

1. Copy the files "DynamicTree.cs" and "DynamicTreeBroadPhase.cs" from the Farseer Physics Engine 3.5 distribution. It is in the source control, which you can view here.

2. Create the edges in smaller batches and do a World.Update() in between inserts.

Dec 22, 2012 at 9:20 AM

I just replaced the two files and the performance increase is absolutely amazing! I had to debug through my code just to check that I didn't manually limit the amount of edges to debug :)


Thanks a lot! :)