I'm adding more robust physics to my game engine. I already had two physics engines incorporated into my engine for evaluation, one of which is Chipmunk. There are a few advantages that Box2D has over Chipmunk that prompted me to also incorporate it into my engine to do a direct comparison against Chipmunk (Continuous Collision Detection, Sleep state for non-moving bodies, and from anecdotal sources, better performance). The problem is that Box2D is performing substantially worse than Chipmunk, both under Windows and iPhone, under the exact same testing conditions within the exact same framework (4 static rectangular shapes forming a box, which is filled with 50 dynamic bodies with circle shapes).
The reason I'm posting is to be certain that I'm not doing anything wrong that is negatively impacting Box2D performance. I'm hoping that those familiar with Box2D can peruse my source to see if anything looks out of place. I would really like to use Box2D because of the CCD and sleeping, but performance trumps all.
World creation:
Code:
b2AABB worldAABB;
worldAABB.lowerBound.Set(-10000.0f, -10000.0f);
worldAABB.upperBound.Set(10000.0f, 10000.0f);
// Define the gravity vector.
b2Vec2 gravity(0.0f, 500.0f);
b2World* myWorld = new b2World(worldAABB, gravity, true);
Body creation. 50 bodies with circular shapes are created, 4 with rectangular shapes:
Code:
b2BodyDef bodyDef;
bodyDef.massData.mass=100.0f;
bodyDef.massData.center.SetZero();
bodyDef.massData.I=1.0f;
bodyDef.position.Set(x, y);
bodyDef.angle=rot;
bodyDef.allowSleep=true;
m_pBody=m_pWorld->CreateBody(&bodyDef);
if (circleParticle) {
b2CircleDef def;
def.radius=12.5f;
def.localPosition.Set(0,0);
def.density=1.0f; //Make it dynamic
def.friction=0.4f;
m_pBody->CreateShape(&def);
} else {
b2PolygonDef def;
def.vertexCount=poly.m_nVerts;
for (int i=0; i<poly.m_nVerts; i++)
def.vertices[i].Set(poly.m_pVerts[i].m_x,poly.m_pVerts[i].m_y);
def.density=0.0f;
def.friction=0.5f;
}
World update:
Code:
myWorld->Step(1/60.0f, 1);
Each frame I update each object to retrieve its position and rotation for rendering with simple calls to GetPosition() and GetAngle().
At this time I'm not doing any collision events / processing whatsoever in my framework. What you see here is all that's going on.
Now one thing I do note is when the bodies finally all settle down and stop moving, FPS jumps way up, so obviously Box2D is able to sleep many of the bodies. I'll do a test with more complex shapes to see if Box2D improves in that area. However my engine will mainly be dealing with simplistic objects.