Multhreaded collision

If I try to calculate collisions with the scene outside the LWJGL thread it cause strange flickerings and strange relocating of models in the scene. I have tracked it down to checkDoTransformUpdate() inside Geometry.computeWorldMatrix() .



Can’t we assume the geomerty transforms are up to date at this point already, because they’ve been updated by the update phase?

If you want to compute collisions in another thread you have to pause the rendering thread otherwise you will have data corruption due to concurrent access

That defeats the purpose of performing the collision in another thread, as I want the rendering to perform as fast as possible and not be slowed down due to the collision calculations.



Why update the transforms twice? Aren’t they already being updated once every frame?

In order to perform collision detection you depend on the transforms of the objects, and the transforms are changed by the render thread. If you do the collision checking in another thread and the transforms are changed then your collision checks are no longer valid.

It is possible to perform collision detection in another thread while pausing the render thread, this is how the jbullet support in jME3 does it and it works fine.

Actually jbullet does it more advanced when I’m not wrong.

It synchronzises Render&Collision Thread at one point, after that both run independent till next synchronisation.

Just consider the whole update loop as one self contained thread and only hook into it as described in the multithreading example in the wiki, we will multithread those parts where it makes sense (e.g. particle update etc) at some point and you will be able to continue using your code and have the benefit of additional (not related to your own code) multithreading if you just concentrate on getting your own code efficiently parted in threads. If you mess with dividing this all up yourself you will have problems later when the “official” solution is introduced and not be able to improve it with feedback or benefit from it.

Collision detection is one part that is a bit difficult to multi-thread since the current API expects results instantly. One solution that might work is creating a clone of the scene graph that collision detection threads could use, and when they complete you aggregate the results and analyse them in the render thread.