RapierPhysics: Rapier physics example Improvement
ℹ️ No issue associated with this PR
Description
I was working on one of my projects based on the Rapier3D.js, and I noticed that @mrdoob recently added an example using Rapier and InstancedMesh which were exactly what I was working on.
Unfortunately, the existing Rapier addon was a bit restrictive to me at some point (and in the example page as well).
- We don't have access to the
collider,colliderDesc, ...; - It comes with its timer, what if I want to branch it to my timer?
- The getter returns only the
rigidBody, and we're necessarily retrained to use it to get amesh - The current implementation is mainly designed for
meshes - etc...
Because I updated it on my side and where using it, I decided to share my version based and following the original version of the RapierPhysics.
What are the changes
- Use the new
addToWorldinstead ofaddSceneToWorld| Example preview. - Set
OrbitControlto auto-rotate | Example preview. - Add a Window
keyboardevent to apply impulse to objects (to shake) | Example preview. - Add the
addToWorld()method to add only one object (and return a bunch of Rapier properties). - Remove the integrated timer inside
RapierPhysicsbut expose theRapierPhysics.step()method; - Migrate the
RapierPhysicsfunction implementation to the class version. -
Exposed more Rapier properties (such
rigidBody,collider, ...). accessible from theWeakMapstore - Commented methods (with types support) using
JsDoc.
Here's the updated version coming with this PR
🔖 This is my first contribution here, I've read the code of conduct and the Contribution guidelines before pushing my changes. I may forget something some details, if it's the case, please let me know.
And this is a small change not related to the core of
threejsbut just a small example improvement. Hoping that will help someone in the future
Love the shake!
I just pushed a small fix for the removeFromWorld method, I wasn't removing the collider and the rigidBody, which were accumulating them in the physic world
https://github.com/mrdoob/three.js/assets/44310540/bc87ebb5-dfbf-48ba-b5de-e7e26d10919c
Recently, I used the physics helper to build the above example.
Unfortunately in the helper, I was blocked when I wanted to scale Matrixs.
I had to update the code a little on my end to be able to scale each matrix.
In the update method, I added the following line:
for (let j = 0; j < bodies.length; j++) {
const physicProperties = bodies[j];
const position = new Vector3().copy(physicProperties.rigidBody.translation());
this._quaternion.copy(physicProperties.rigidBody.rotation());
const scale = (physicProperties.rigidBody.userData as { scale?: Vector3 })?.scale ?? this._scale; // <--- This line
this._matrix.compose(position, this._quaternion, scale);
this._matrix.toArray(array, j * 16);
}
I passed a RAPIER.Vector3 to the userData property of the RigidBody, otherwise I'm using the common _scale property.
By doing that, I was able to update the scale of individual instance Matrix
It could help others by being able to control the scale!
cc: @mrdoob
Live link: https://raw.githack.com/Neosoulink/three.js/feat/rapier-physic-improvement/examples/index.html?q=rapier#physics_rapier_instancing
Instead of using a keyboard event for the shake do you mind adding a button instead?
That way it'll work on mobile too.
Instead of using a keyboard event for the shake do you mind adding a button instead?
[IMAGE]
That way it'll work on mobile too.
Agree and it makes sense! I'm updating it
Hey @mrdoob, I just update it
I added a button to shake.
And I updated the update method to not fix the scale at 1 1 1
So now we can update the matrix scale and the physicHelper will use it.
We can now do the following:
boxeMatrix.scale( new THREE.Vector3( 2,2,2 ) ); // Selected box matrix
boxes.setMatrixAt( i, mat );
And the helper will use the 2 2 2 for that specific Matrix
Don't forget this! Perhaps do a Ctrl+H to replace hysic with hysics and hysicss with hysics to unify the naming scheme?
I just realized this PR was delayed from r165 to r167 😆
I just realized it was also used in the xr_ballshooter example. I pushed the fix/update. sorry for not noticing it at the beginning.
cc: @mrdoob @zalo
https://github.com/user-attachments/assets/20c91037-6067-4e5d-9e45-82fae77fc4d9