So, I’ve managed to get the rapier3d physics engine working together with three.js. Things are in the very early stages so I don’t have a lot to report yet. I was able to bounce an object off the terrain
One big challenge was just getting the WASM bundle to load. I was using Snowpack to bundle my app, but this doesn’t support WASM resources currently. Webpack 5 also has serious difficulties with WASM at the moment. I ended up having to downgrade to Webpack 4 to get everything to work, which made me sad. (I believe that it may also be possible to get it working with Rollup as well, but I haven’t tried.)
Eventually what I may do - since my project is a large monorepo - is have one subpackage that builds a library bundle with webpack 4 or rollup, and then import that library into a Snowpack build for the whole app.
One other thing I wanted to mention - the rapier TypeScript APIs are pretty flexible about the data types they accept. So for example, their
Vector type doesn’t have to be specifically their vector class, it can be any object that has an x/y/z coordinate. That means you can pass in a three.js
Vector3 without having to convert it to another data type.
Unfortunately, it doesn’t work in the reverse direction. One thing that would be a nice improvement for the three.js TypeScript type defininitions is to define
IVector3 interface types, and then make any method that accepts a Vector3 accept these interfaces - so for example
Vector3.copy() would take a
Readonly<IVector3> as input. This would then allow you to things like
meshObject.position.copy(rigidBody.translation()) and not have to convert or typecast anything. Similar interfaces would be defined for Eulers, Quaternions and other simple object types that are used as parameters.