Skip to content

Multithreading Bullet Physics in jme3

Introduction

The jBullet BulletAppState can run its physics update on a separate thread in parallel with rendering. This setting describes scheduling of the physics space, not a guarantee that the underlying solver itself uses multiple cores or the GPU.

How is it handled in jme3 and bullet?

A SimpleApplication with a BulletAppState allows setting the threading type via

setThreadingType(ThreadingType type);

Here ThreadingType is either SEQUENTIAL or PARALLEL; the default is SEQUENTIAL.

You can activate PARALLEL threading in the simpleInitApp() method:

bulletAppState = new BulletAppState();
bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
stateManager.attach(bulletAppState);

Now the physics update happens in parallel to render(), that is, after the user’s changes in the update() call have been applied. During update() the physics update loop pauses. This way the loop logic is still maintained: the user can set and change values in physics and scenegraph objects before render() and physicsUpdate() are called in parallel. Coordinate access from other threads and physics callbacks carefully; this scheduling does not make arbitrary concurrent scene-graph or physics access safe.

PARALLEL SEQUENTIAL
  1. update(), 2. render() and physics update().

  1. update(), 2. render(), 3. physics update().

Physics Debug View is rendered inaccurately (out of sync)

Physics Debug View is rendered accurately.

Tip

You can add more physics spaces by using multiple PARALLEL bulletAppStates. You would do that if you have sets physical objects that never collide (for example, underground boulders and flying cannon balls above ground), so you put those into separate physics spaces, which may reduce collision checks. Separate spaces cannot interact, and extra threads and synchronization may cost more than they save: profile your application.