Nphysics on the web - calling nphysics from JavaScript

Hi,

I have been writing a browser-based 3d game in TypeScript using Three JS as my rendering engine. I’ve been trying to settle on a physics solution for the game and came across nphysics, and it interests me very much as it can be compiled down to WASM.

I would like to initialize and call nphyiscs functions from my JavaScript code (as opposed to the examples where the client code is written purely in Rust). Would I need to write my own wrapper/bindings in Rust and expose via #[js_export] as explained in the stdweb README, or are all the nphysics functions exposed out of the box?

Do you see this as a viable approach? Any tips and pointers would be much appreciated!

Thanks

Hi!

I’ve never attempted this myself, but I believe you will need to write some Rust wrapper for the features you need and expose via #[js_export] as you suggest.

Also note you could use wasm-bindgen instead of stdweb (by enabling the use-wasm-bindgen feature of nphysics) and follow the wasm-bindgen doc.

But, yeah, in both cases you will likely need to write some wrappers to get something usable from the JS side.

1 Like

Thanks for the reply!

I’ll have to have a go at this when I get a spare weekend. If successful I’ll write up a blog post about how to do it, as I can’t find any examples of anyone currently doing it this way.

1 Like

Thanks for your work!

Looking forward to your sharing!!