Pushing Box2D to the max

Hello,


I am working on a project that is using Box2d as physics middleware. I was wondering if there is any way to (realistically) increase the amount of entities to about 10.000. The objects are spread over a huge map and most don't touch.

If the answer is no, how can I turn that into a yes? If it means adjusting the Box2d code a lot, I am currently requesting it as a feature. We'd like to remain focussed on the game.


Thanks a lot,

Antoon Groenewoud
As far as I'm aware, there is no physical limit to the number of entities in Box2D. There is a fixed amount (in b2Settings.h) that you can adjust in v2.0.1. The practical limit however, would be far less than 10,000... unless you have a extremely chunky processor, and don't care about framerate too much.

For me, the frame rate starts to drop after around 1,600-1,800 bodies with fairly simple collision polygons.

You might want to try setting sleep mode if the objects that aren't doing much are stationary (sleep is activated when the angular and linear velocity fall below a threshold), then it would be conceivable.
I expect that 66% of the objects are static, and the rest dynamic and of those 30% are connected with joints (they could still be sleeping, however).
The latest version of Box2D in Google SVN has no body or shape limits. Performance should be dominated by the number of awake bodies, not the total number of bodies.
I did some performance benchmarks for my game a few months ago. I had 1000 sensor circles flying around with a fair amount of separation and it was taking about 3-4 ms per physic step on my Core 2 Duo 6400. Results may be worse with joints and non-sensor objects, but it should give you a ballpark estimate of the possible performance of Box2D.
Hi xilliah,

Here is what I done to have an unlimited amount of objects in a simulation of Box2d 2.0.1.

1- I have a manager that wrap Box2d
2- This manager have Models (a model is the complete huge map with a LOT of objects)
3- Models have Sectors (sectors are chunk of maximum 512 objects of your huge map (512 is the max number of proxies by default and I suggest to leave it at that for better performance))
4- Sectors have objects and joints
5- my object class in the sector is a wraper of all box2d object type, same thing goes for my joint class

So when I have to go from one sector to the other, I put sensors on the way to deactivate the sector I'm comming from and activate the sector I'm going to.

Deactivation : when a sector is deactivated, I destroy every objects and joints it have in the box simulation data, but I keep the data (position, velocity etc...) of these objects in a list of the sector! (the objects are only destroyed for Box, not for my sector, there are just "deactivated".)
Activation : when a sector is activated, I go through the sector object list and recreate them in the box simulation with the data I kept since the last deactivation. (they come back exactly the way they where when deactivated)

This is very fast by the way, the activation and deactivation is instantaneous even in a race game the size of GTA on a slow computer I don't see any framerate hit 😉

This is only one technique but I hope it will give you some ideas about how to achieve your goal with box!
So far I've gotten 40.000 polygon shapes into box2d with about 20-50 contacts running at real time. I have not tested it with 3000 active shapes. We are probably going to mod the engine with a quadtree.

Another idea is to use a full blown 3d engine that has all of that build in already, but I prefer to stick to box2d because it will probably give a better performance in the end.
xilliah,

I ported the Chipmunk spatial hashing algorithm to Farseer (2d engine for XNA, unrelated to box2d afaik) and helped a friend go from 2 fps to maxed out at 60 in a large scale asteroids game. You should either port spatial hashing to your project and tune, or we could set up a very small contract if you are interested. I think that it would take me a day to port and test and cleanup the spatial hashing algorithm and you too if you feel confident. Spatial hashing is essentially just that you add a shape's id to each bucket in a 2d grid that the shape's AABB covers. The hashing part is that you don't actually have this grid, just a hash table where each hash bucket may have multiple x,y buckets that corrospond to one hash table bucket.

-Tim Kerchmar
pTymN wrote:
xilliah,
I ported the Chipmunk spatial hashing algorithm to Farseer (2d engine for XNA, unrelated to box2d afaik) and helped a friend go from 2 fps to maxed out at 60 in a large scale asteroids game. You should eith..
-Tim Kerchmar


A late response from me lol..

It seems to run quite well at the moment actually, but if we get problems I will use the technique you just suggested.

We decided to go for even more static bodies, so the problem is reduced.
Attachment:
File comment: How to enable SSE in MSVC
msvc_enablesse.png
msvc_enablesse.png [ 38.54 KiB | Viewed 563 times ]
Attachment:
File comment: How to enable SSE in MSVC
msvc_enablesse.png
msvc_enablesse.png [ 38.54 KiB | Viewed 563 times ]
Depending on your platform and compiler you may want to check out the math options:

With msvc try:
right click on the Box2D project
select: Project properties (alt-F7) -> configuration Properties -> Code Generation

set Enable Enhanced Instruction Set to: Streaming SIMD Extensions 2 (/arch:SSE2)
set Floating Point Model to: Fast (/fp:fast)

see attached picture

Note that this will likely cause the compiler to emit sse serial assembly instructions. Although not simd, this is still faster than x87.

The cmake generated project files didn't turn those optimizations on for the release mode. So i thought i'd mention it here.

Using a profiler (parallel studio) on a larger version of the pyramid test/demo, i noticed that most of the physics time is spent within the solver b2Island::Solve() and functions it calls. The collision detection less.

I did notice that sometimes the 'inline' keyword was sometimes being ignored. I tend to use __forceinline myself.

Unless i'm mistaken, I noticed box2d was using the virtual function call approach for dealing with each joint type and then the contacts were all put together in the efficient b2ContactSolver instead of calling a virtual function for each contact. easy to follow and efficient. clever architecture.

Original thread: https://box2d.org/forum/viewtopic.php?t=3956