c-frame / physx

A-Frame physics using PhysX
MIT License
22 stars 4 forks source link

Extending Physx API Bindings - exploration of options. #15

Open diarmidmackenzie opened 1 year ago

diarmidmackenzie commented 1 year ago

I've been looking at extending the available PhysX API bindings.

My immediate goal was to experiment with Projection on Fixed Joints to solve a problem where walls of bricks joined together with fixed joints become wobbly above a certain size.

For future reference, this issue documents my progress on a couple of fronts.

  1. Rebuilding the existing WASM build, using the approach described here.

This is the "obvious" approach, and should have been straightforward.

I made some minor updates to enable a couple of new API calls here

Rebuilding should be pretty straightforward, but I had a few issues

  1. Migrating to physx-js-webidl

This is used by PHY, a Physics library for Three.js that I've been looking at.

Benefits of using this WASM build include:

I've been making some changes on this branch to try to get this working. Unfortunately it's not staightforward.

The way the PhysX CPP API gets translated into a JS API is quite different for this alternative build.

Working through the differences has basically been a matter of:

If made a bunch of fixes & hit a number of errors, but still hitting problems inside the physics simulation:

physx-js-webidl.wasm:0x11b817 Uncaught RuntimeError: null function or function signature mismatch
    at physx-js-webidl.wasm:0x11b817
    at physx-js-webidl.wasm:0x11bc19
    at physx-js-webidl.wasm:0x9ff13
    at eval (physx-js-webidl.js:969:22)
    at PxScene.simulate.PxScene.simulate (physx-js-webidl.js:30889:50)
    at i.tock (physics.js:746:16)
    at bind.js:12:17
    at i.tock (index.js:127:7)
    at v.tock (a-scene.js:724:36)
    at e.object3D.onAfterRender (a-scene.js:52:34)

Presumably something has been initialized incorrectly due to the change in API, but it's not straightforward to find out what.

I'm wrapping up this investigation for now. While I had wanted to experiment with projection of Fixed Joints, I only thought there was a small chance of it solving my problems, and think a better solution is likely to be by following the advice in the API docs and use a single actor with multiple shapes...

Note All joints are enforced by the dynamics solver, so although under ideal conditions the objects will maintain their spatial relationship, there may be some drift. A common alternative, which is cheaper to simulate and does not suffer from drift, is to construct a single actor with multiple shapes. However fixed joints are useful, for example, when a joint must be breakable or report its constraint force.

Nevertheless, it's likely that we'll want to extend the exposed parts of the PhysX API and/or move to 5.1+ at some point, so with that in mind, I've documented my investigations so far here.

kfarr commented 1 year ago

Wow thanks for this writeup and sharing this repo! https://github.com/fabmax/physx-js-webidl

enzofrancescaHM commented 3 weeks ago

Sorry to write on this thread 1 year after last communications, but it would be nice to have Physx migrated to physx-js-webidl because it have support for vehicles and this is a big point, useful for a-frame + physx. So, just a manifestation of interest!