korlibs / korge-box2d

Box2D Physics implementation for KorGE Game Engine
4 stars 2 forks source link

Replace JBox2D with native implementation / JNI wrappers for some applicable targets #1

Open smelfungus opened 1 year ago

smelfungus commented 1 year ago

Hello! It would be nice to rely on a first-party Box2D implementation in korge. That would allow keeping up-to-speed with Box2D updates (without relying on an outdated complete Box2D Kotlin/Java rewrite) and resolving JBox2D compute/memory performance considerations.

References: https://stackoverflow.com/questions/31954012/whats-the-difference-between-jbox2d-and-libgdx-box2d https://github.com/libgdx/gdx-box2d https://box2d.org/posts/2023/01/starting-box2d-3.0/ https://gilspot.net/blog/m2z0uhrb5t9vgymuo8z14lrjfwyx6z

soywiz commented 1 year ago

To use native libraries, there would be an automated effort for generating wrappers on all the targets: autogenerate a common binding, then implement each one in each supported platform. JS could use WASM, for the case of Java, ideally using a WASM loader to not have to provide a binary for every single target, and K/N should be able to compile code for all the targets.

Not trivial to do it manually. To assume that effort should be something automated, and this could be used for other things like supporting ffmpeg, custom image formats like webp or heif, etc.

Alternatively if someone wants to do it manually, it could be a separate integration altenrative to korge-box2d.

Is there a specific non-performance feature you are missing here?

smelfungus commented 1 year ago

@soywiz, thank you for your answer! Let me play around with profiling & benchmarking a bit so that I can share some actual comparisons. Apart from that, what do you think about future Box2D versions support? It will require re-porting now :( An automated native solution sounds nice, but I suppose it will require some complex engineering.

smelfungus commented 1 year ago

As for non-performance issues, it seems like Box2D introduced some valuable changes starting from 2.3.1: https://github.com/erincatto/box2d/releases Some of them are especially nice, like ghost collision resolution, broad-phase optimizations, static bodies & joints refinements.

soywiz commented 1 year ago

Yeah, unfortunately that effort is not trivial indeed and rather long and complicated. But maybe at some point it is reasonable, so let's keep this open, and let's reconsider once the automatic generation for all the targets is there 👍 alternatively maybe someone wants to do the effort manually, so this is also something anyone can do and make a PR or have a korge-native-box2d repo