Saturday, 20 February 2010

Engine Update: Multi-threading & BEPU

Integrated BEPU's method for easy physics Multi-Threading. The Physics class has the following code for Multi-Threading the physics.

if (Environment.ProcessorCount > 1)


for (int i = 0; i < Environment.ProcessorCount; i++)




Space.useMultithreadedUpdate = true;


What this does is run through all the processors available on your machine and add it to the thread-manager to spread load across all available CPUs. Thanks to BEPU the integration is VERY easy. A pdf + sample is available on their site.

Multi-threading on the Xbox is slightly different, however it shouldn't be a problem considering the available documentation and sample by BEPU.

I still had some time left to test the performance gains using a small demo scene I made yesterday. It's basically a dominos scene with 350+ domino bricks in 3 lines getting knocked over by three boxes slinging through the air. With the debugger still attached the performance drops to 2 FPS at its lowest. Activating Multi-threading on two cores (my laptop has a Core2Duo (T9400) CPU, without Hyper-Threading) the lowest FPS recorded is 31. That is an amazing difference in performance. Another test without the debugger attached didn't get the demo-scene below 60.

Another thing I worked on today are trigger-volumes. These can be used for a number of tasks (audio triggers, portals, level events etc.) BEPU has a very nice EventManager attached to every Entity. With this manager you can easily add events to your game objects. There is not much to see so far, but I hope to have some cool demo scene ready very soon...I'd like to get rid of the boring cubes and show some actual cool stuff :)

Entity.eventManager.addEventHook(new EventHandlerInitialCollisionDetected(initialCollision));

This will fire the attached method only once when the Entity is colliding with another Entity...The fired method could be something like this:

public void initialCollision(Entity sender, Entity other, CollisionPair pair)


other.moveTo(new Vector3(0, 10, 0));


This will move the colliding Entity to the Vector3 position...More on this in a later video ;)

Image of the current demo scene:


  1. Hi Tom,

    I read elsewhere that you're using BEPU's character controller. Is using a physics library for scene collisions preferred over something like the BoxCollision library?


  2. Hi,

    I quess if you're not going to use any kind of physics that a library like BEPU might create some overhead compared to a much simpler collision-only library.

    I personally prefer full physics libraries so I have the ability to use physics whenever I want. They're really not that hard to implement into your game either.

    An alternative to BEPU would be JigLibX. A sample is available HERE.