Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance by replacing planck.js under the hood? #305

Open
quinton-ashley opened this issue Feb 3, 2024 · 4 comments
Open

Improve performance by replacing planck.js under the hood? #305

quinton-ashley opened this issue Feb 3, 2024 · 4 comments
Assignees
Milestone

Comments

@quinton-ashley
Copy link
Owner

quinton-ashley commented Feb 3, 2024

I originally picked planck.js because it's a pure JS port of Box2D's physics simulation and the results it produces are objectively superior to matter.js (used by Phaser) and rapier.

Later I found out about box2d-wasm, a much more performant and direct port of Box2D v2.4.1 from C++ to wasm via emscripten. I figured that for v4 of p5play I should use box2d-wasm.
https://github.com/Birch-san/box2d-wasm

But planck.js v1 was recently released and it performs significantly better than the alpha versions. JavaScript has also gotten really quick at doing math nowadays. Try this head to head test with emscripten compiled C code running in wasm, the results are basically the same!
https://jtiscione.github.io/webassembly-wave/

I upgraded this benchmark that compares the latest version of planck with other box2d web ports. box2d.js is basically the older version of box2d-wasm.
https://quinton-ashley.github.io/box2d_benchmarks/

Here are the results for different devices:

MacBook 2020 with M1 chip
image

iPhone 13 Pro
image

iPhone 6S
image

On more powerful devices like my sister's iPhone 13 Pro, the performance difference is negligible. But on my iPhone 6S, planck can only get 48fps. Perhaps the tests are simply not rigorous enough to parse out performance differences on modern devices.

But let's keep in mind that these an artificial performance benchmarks just for the physics simulation and p5play aims to be a general purpose game development framework. I'm not sure how much using box2d-wasm would actually improve p5play's overall performance, especially on older devices, since graphics performance can be the bottleneck.

On good hardware, p5.play (v1) maxed out at less than 100 sprites. p5play v3 can have up to 500 sprites, which is plenty for most games. But several users have expressed interest in pushing beyond those limits. Also if all sprites are contacting at once there can be significant peak simulation demand performance degradation to a handful of fps, then back to 60.

One other advantage of replacing planck with box2d-wasm would be that it's a port of v2.4.1 of Box2D and supports the liquidfun extension.

@quinton-ashley quinton-ashley changed the title Keep using planck.js under the hood? Yes! Keep using planck.js under the hood? Feb 4, 2024
@quinton-ashley quinton-ashley changed the title Keep using planck.js under the hood? Improve performance by replacing planck.js under the hood? Mar 11, 2024
@quinton-ashley quinton-ashley self-assigned this Mar 11, 2024
@ShiMeiWo
Copy link

My PC's result:

Browser: Chrome 123

Name avg ms/frame 5th %ile 95th %ile Ratio
box2d.js 5.84 6 4.600000001490116 1.00
box2d-web 15.77 15.399999998509884 13.300000000745058 2.70
planck.js 22.50 23.799999997019768 22.399999998509884 3.85
@box2d/core 23.14 33.399999998509884 14.399999998509884 3.96
box2d-html5 25.84 25.300000000745058 23.399999998509884 4.42
@flyover/box2d 43.65 63.400000002235174 34.900000002235174 7.47

@quinton-ashley
Copy link
Owner Author

@ShiMeiWo wow! Big difference there. Could you include details about your PC's CPU, GPU, and memory?

@ShiMeiWo
Copy link

ShiMeiWo commented Apr 1, 2024

@quinton-ashley

wow! Big difference there. Could you include details about your PC's CPU, GPU, and memory?

Please take a look below:

Item Detail
OS Windows 10 Home 64-bit 22H2
RAM 8.00GB
CPU Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz x64
GPU GeForce GT 650M

@quinton-ashley quinton-ashley added this to the v4 milestone Apr 7, 2024
@quinton-ashley
Copy link
Owner Author

quinton-ashley commented Sep 7, 2024

On August 12, 2024 Erin Catto released v3 of Box2D, it's a big improvement over v2 and I'd love for p5play users to be able to use it.

I've decided to wait on starting p5play v4's development until there is a port of it to JS or WASM. For now, I'm too busy working on q5's webgpu renderer anyway.
https://q5js.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants