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

do you have a sense how hard it would be to adapt this for liquidfun? #17

Closed
danbri opened this issue Feb 21, 2021 · 8 comments
Closed

Comments

@danbri
Copy link

danbri commented Feb 21, 2021

Liquidfun (an interesting fork of box2d that adds particle/fluid capabilities) has some pre-WASM emscripten tooling, but it seems out of date. I was wondering if your approach here would be more maintainable, and if you'd have any interest in trying it...

@Birch-san
Copy link
Owner

Hm, I agree that the approach used in this repository (consume C++ via git submodule, bind via WebIDL) would be more maintainable.

Might not be too difficult. Some observations:

Based on file dates, it kinda looks like Liquidfun leaves most of the Box2D source untouched; the fork contribution is primarily the Particle directory?

The approach used for binding in this repository would not be interface-compatible with the manual bindings used in Liquidfun currently, so any testbed examples would need tweaking in order to work.

The path of least resistance may be to fork Box2D at its latest commit, and add the Particle folder back in, (and confront any breaking API changes). then consume that in this repository, update the WebIDL bindings to include Particle classes.

@danbri
Copy link
Author

danbri commented Feb 21, 2021

Totally agree with your observations (it is old and under-maintained), and the clever trickery to speed up calls looked both like a maintenance burden, and unnecessary in 2021. I believe the original intent was gamedev but these days box2d is getting a lot of usage as a simple physics engine for machine learning / AI. Much of that is in a Python-led world e.g. openai environments like BipedalWalker; but also increasingly in JS (example, see also [tensorflow.js]).(https://www.tensorflow.org/js/demos).
I thought a WASM build could be a path towards drumming up attention/interest for it (or something similar) in an ML context. But yeah it should also be fun for web games, with WASM maturing and everything being faster than 2013...

Based on file dates, it kinda looks like Liquidfun leaves most of the Box2D source untouched; the fork contribution is primarily the Particle directory?

AFAIK that sounds plausible

The approach used for binding in this repository would not be interface-compatible with the manual bindings used in Liquidfun currently, so any testbed examples would need tweaking in order to work.

Noted. Personally I don't see that as a problem (it's possible others have other plans, but things seem quiet...).

The path of least resistance may be to fork Box2D at its latest commit, and add the Particle folder back in, (and confront any breaking API changes). then consume that in this repository, update the WebIDL bindings to include Particle classes.

I had the impression from comments in the liquidfun repo that this would be a substantial undertaking, definitely beyond me, though I'd love to see it!

@Birch-san
Copy link
Owner

some things that really made it challenging like some git settings that affect the recognition of newline differences, \r\n, vs \n

I've made a commit of "all non-whitespace changes to C++ source", based on Box2D v2.3.0:
https://github.com/erincatto/box2d/compare/master...Birch-san:liquidfun?expand=1

Looks like it's more than just the Particle folder that changed. Quite a few files are touched, but it's mostly additions rather than contradictions.

Not sure how much the world moved in the years since then (i.e. Box2D v2.4.1). All files were renamed (i.e. to eliminate capital letters), which will add some challenge to a rebase. I think there's a lot of changes, but most are so light-touch that it'd be easy to figure out where to insert them in v2.4.1. May need manual attention though.

@Birch-san
Copy link
Owner

I've had a stab at manually copying all those changes onto Box2D v2.4.1:

https://github.com/erincatto/box2d/compare/master...Birch-san:liquidfun-rebase?expand=1

Haven't tried compiling or running it. There weren't many errors in my IDE, except that I'll need to change all float32s into floats.

Bost of the changes don't look like they'd cause problems, but I saw substantial rewrites to timers and to memory allocation.

@Birch-san
Copy link
Owner

Updated WebIDL bindings:
https://github.com/Birch-san/box2d-wasm/compare/liquidfun?expand=1

Fixed other compile errors (e.g. missing symbols that Emscripten noticed at link-time):
https://github.com/erincatto/box2d/compare/master...Birch-san:liquidfun-rebase?expand=1

It's successfully compiled to WASM locally on my computer, and my original examples still work (haven't regressed).

Various new symbols (i.e. the ones that I exposed via WebIDL) are accessible via JS:

b2ParticleBodyContact
b2ParticleColor
b2ParticleContact
b2ParticleDef
b2ParticleGroup
b2ParticleGroupDef
b2ParticleHandle
b2ParticlePair
b2ParticleSystem
b2ParticleSystemDef
b2ParticleTriad

Next step would be to build a demo to see if they work.

@Birch-san
Copy link
Owner

Works. Released:
https://github.com/Birch-san/box2d-wasm/releases/tag/v4.0.0-liquidfun.0

@danbri
Copy link
Author

danbri commented Feb 24, 2021

I've only had a little time to poke at this, but it looks great - can confirm it builds fine, the integration-test app works and shows particles, the demos still function etc.

@danbri
Copy link
Author

danbri commented Feb 26, 2021

I've (as a temporary step for testing/exploration) put a copy of the generated .js and .wasm on my site, so it can be loaded from elsewhere (please nobody make anything that will break if this goes away!).

I did this to see how the WASM approach might look in other environments. I only got this far in the online P5 editor: https://editor.p5js.org/danbri/sketches/hWpLvdEB5 - for some reason it only works for me when using an incognito window, but fails while logged in. It could also be interesting to adapt the backend examples and try a python-wasm runtime e.g. via Colab. The P5 example doesn't try using particles yet.

curl --head https://danbri.org/2021/lf-wasm/demo/Box2D.wasm
HTTP/1.1 200 OK
Date: Fri, 26 Feb 2021 13:10:23 GMT
Server: Apache/2.4.25 (Debian)
Last-Modified: Fri, 26 Feb 2021 11:38:56 GMT
ETag: "164aeb-5bc3bb6a495c4"
Accept-Ranges: bytes
Content-Length: 1460971
Cache-Control: max-age=31536000, public
Access-Control-Allow-Origin:*
Content-Type: application/wasm

See also Twitter thread

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

No branches or pull requests

2 participants