Feb 17, 2014 at 7:14 PM
Edited Feb 18, 2014 at 7:15 AM
i would really love to program my game in C# and use Farseer Physics as my physics engine. But i can't do it because i have big issues with the performance of Farseer. Maybe i'm doing something wrong ( which i probably do ) but i hope you can help me resolve
the problems. I have an application that i ported from C#+Farseer+XNA to Java+Box2D(native libGDX wrapper)+libGDX. It is essential the game "Pong" but im testing the performance by trying to spawn 4000 dynamic bodies. In Farseer this works "ok"
up until 100 dynamic bodies that move simultanously and it "breaks" at about 400 dynamic bodies on my configuration ( world.step takes way over 40ms ). Even when all bodies start sleeping the step time stays at about 8ms. in my libgdx test application
( which is essentially the same but also has added features ) i can have up to 4000 ( fourTHOUSAND, this is not a typo )dynamic bodies, all moving at the same time, and i only have ~50ms per step and when they all sleep it stays under 1ms.
and tested, Farseer should have similiar or even better performance than native Box2D and so im quite confused. I do have 3 computers ( 1. win xp, 2. win vista, 3. win 7 ) and tested the apps on all of these and its always the same: I
can have 10 times more dynamic moving bodies in java than i can have in c# and the c# version has unreasonable high step times even with 10 times less bodies. So what should i now expect of Farseer? I'm expecting similiar performance.
You can reproduce my "problem" with the Farseer XNA Sample app. Modify the simple sample 4 to let the pyramid consist of 140 instead of 14 blocks and in the pyramid prefab change the size of the rectangles to 0.125f instead of 0.5f ( so it fits into
the scene ). This is a lagfest on my computer but probably would be piece of cake in my libGDX native Box2D app.
So what do i want from you? Please could you verify that when you change simple sample 4 to something between 100 and 300 rectangles, that you also have a LAGFEST or tell me how many bodies you could spawn. Then, if you can spawn more than 2000, please try
to help me to change my implementation (which is identical to original box2d except for the slight differences of using the factories etc. ).
Also please dont ask why i want 4000 dynamic bodies ( this is just for testing purposes ). This is not the question. The question is why farseer it 10 times slower than native box2d or what could be the reason that i am having these problems.
All running .net 4 ( farseer 3.5 ) or java 7 x64 server compiler ( dont know the box2d version, newest libgdx )
- Win xp
- win vista
core i7 920 ( first generation i7 )
nvidia gtx 560
- win 7
Intel® Core™2 Quad Processor Q6600
4 gb ram
nvidia gtx 560
// Physics2D is just a wrapper class that wraps the body with some additional information
public Physics2D GetPhysics(Vector2 origin, Shape worldScaledShape)
origin = origin * WorldScale;
Physics2D physics = this.GetComponent();
physics.Body = BodyFactory.CreateBody(World, physics);
physics.Body.LinearDamping = 0.1f;
physics.Body.AngularDamping = 0.1f;
physics.Body.SleepingAllowed = true;
physics.Body.IsStatic = false;
physics.Body.BodyType = BodyType.Dynamic;
physics.Body.Enabled = true;
physics.Body.Position = origin;
physics.Body.IsBullet = false;
var fix = physics.Body.CreateFixture(worldScaledShape, physics);
fix.Restitution = 1;
fix.Friction = 0.1f;
physics.AABBSize = GetAABBSize(worldScaledShape);
// because AABBSize is in box2d scale we need to normalize it back
// its only used for drawing
// stepping method, usual business
public override void Process(Microsoft.Xna.Framework.GameTime gameTime)
// factory method that spawns balls. There the GetPhysics method is called
public Entity GetCircularBall(float originX, float originY, float radius)
Entity e = new Entity();
var tex = engine.Content.Load<Texture2D>(GlobalValues.BallTexture);
var drawable = renderer.GetActorDrawable(tex, null, Color.OrangeRed, 1);
var phys = physics.GetPhysics(new Vector2(originX,originY), physics.CreateCircular(radius));
var actor = actors.GetActor(phys, drawable);
The world is scaled by a factor so the balls size lies between 0.1 and 10
I also would like to say that i program for about 12 years and that i usually know what i am doing. Also my libgdx native box2d version is running like a charm. So either i am just overlooking something or c# jit doesn't compile to "good optimized"
native code or maybe i found a performance bug in Farseer. I'm tending to number 2 as my other tests show that C# is just unreasonable slow compared to c++ or java -server compiler.
Edit: Besides all that i also converted my java app with ikvm to a .net app and it then has the same speed as the java app. This could also mean that farseer isn't as fast as native box2D because essentially libgdx uses JNI all over the place to use box2d and
i would assume that ikvm uses P/Invoke to do the same then and that the problem then really is that native code runs fast but c# managed code is the heck slower