Happy Martin Luther King Jr. Day!
Source Code: docs.zip (3.6 MB)
The demo above is trying to compare 3 physics implementations:
- Pure Babylon implementation using
PhysicsImpostor
,AmmoJSPlugin
, and other Babylon API’s - Separated physics script (not nearly as comprehensive as Babylon’s API) running in a
WebWorker
- Separated physics script running in the main thread (no
WebWorker
)
This was inspired by @RaananW 's article, @yannick 's video demo, @Raggar 's Playground, and more!
Comparisons (1000 physics boxes):
1. Pure Babylon:
14 FPS, Physics step compute time: 16.6 ms
2. Separated Physics (No WebWorker
):
74 FPS, Physics step compute time: 12.1 ms
3. Separated Physics (WebWorker
):
199 FPS, Physics step compute time: 22.0 ms
Using physics in a WebWorker
can give high FPS, but seems to be unstable. As mentioned by @sebavan, @Cedric, and @Raggar in Using web workers for the physics, world and networking?, Running physics in a webworker, and Questions on Updating Heightmap Physics Impostor - #3 by Cedric, there seems to be a delay in the messages between the main thread and the worker (which grows over time as more and more messages are sent). This may be related to the high 22 ms physics step compute time, which gradually grew from 12 ms and stabilized at 22 ms. Also, triggering events (e.g. mouse click and drag events) seems to temporarily block worker messages, causing visual pauses in the physics
We could maybe improve the stability of the worker solution by limiting the number of messages. Currently, assuming 200 FPS, 200 messages are sent to the worker per second. ~60 messages (with data about the 1000 physics boxes) are sent from the worker per second regardless of FPS. This may be too much data being transferred
For an interactive, real-time game, perhaps physics in a WebWorker
is currently not ideal. But, if you want to just display many physics objects at a crazy high FPS, maybe it’s worth looking into