MultiThreaded Physics Engine

So I’m currently working on a cross platform ish (so far just Linux and Mac tested) game engine and for the physics engine i currently have it running on a separate thread. I’m currently in the process of making the physics engine multi threaded, hopefully for better performance with lots of high physics detail objects, or just a crap tone of basic physics objects

My main question is it better for performance to have the physics engine treat each branch as its own thread or if i should have 1 thread follow every collision branch all objects and the higher detailed collision event should have helper threads that can do portions of the higher resolution collision. ( I’m more than open to other options)

You will need to use a profiler to determine that for yourself, since without code to reference what your actually doing we are just going to be guessing.

Can always take a peek at how other engines handle it to derive what will work well in your case.

Something like Pymunk — pymunk 6.2.0 documentation or http://chipmunk-physics.net/ is very easy to digest and should help steer you in the right direction.

1 Like

github /steel99xl/kaijuGL/tree/ObjectDevelopment
So in general most of this is bad but i plan on improving most of it, lol
( i cant include links)

I call it the granularity problem.

Thread synchronization is fairly expensive.

If you grain concurrency too fine, threads start to get in each other’s way. So instead of a gain, you get a performance hit. The coarser the better is the rule of thumb.

Most physics implementations don’t go beyond a single dedicated thread to it really. And for a good reason.

It would be quite challenging to design a good multi-threaded physics engine, and you will have to optimize everything top to bottom. Forget about OOP, object oriented design is your enemy - it is a terrible cache polluter, you need to go all out data orientation, which can make incredible difference.

I’d probably go for a space partition solution - split the world into independently processed segments, set a timeout to find physics heavy segments, split or size them down to hit your latency targets. You will also have to take care of fringe entities that involve interaction between objects from different segments.

You have to balance between trying to avoid premature optimizations, which are quite bad, but also avoid substituting them with design limitations, that are even more tedious to work out later on.

1 Like