three.js icon indicating copy to clipboard operation
three.js copied to clipboard

RapierPhysics: Rapier physics example Improvement

Open Neosoulink opened this issue 2 years ago • 10 comments

ℹ️ 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 a mesh
  • 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 addToWorld instead of addSceneToWorld | Example preview.
  • Set OrbitControl to auto-rotate | Example preview.
  • Add a Window keyboard event to apply impulse to objects (to shake) | Example preview.
  • Add theaddToWorld() method to add only one object (and return a bunch of Rapier properties).
  • Remove the integrated timer inside RapierPhysics but expose the RapierPhysics.step() method;
  • Migrate the RapierPhysics function implementation to the class version.
  • Exposed more Rapier properties (such rigidBody, collider, ...). accessible from the WeakMap store
  • Commented methods (with types support) using JsDoc.

Here's the updated version coming with this PR

Screenshot

🔖 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 threejs but just a small example improvement. Hoping that will help someone in the future

Neosoulink avatar Apr 24 '24 01:04 Neosoulink

Love the shake!

mrdoob avatar Apr 24 '24 17:04 mrdoob

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

Neosoulink avatar May 05 '24 10:05 Neosoulink

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

Neosoulink avatar May 12 '24 02:05 Neosoulink

Live link: https://raw.githack.com/Neosoulink/three.js/feat/rapier-physic-improvement/examples/index.html?q=rapier#physics_rapier_instancing

mrdoob avatar May 13 '24 06:05 mrdoob

Instead of using a keyboard event for the shake do you mind adding a button instead?

Screenshot 2024-05-13 at 3 03 06 PM

That way it'll work on mobile too.

mrdoob avatar May 13 '24 06:05 mrdoob

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

Neosoulink avatar May 13 '24 10:05 Neosoulink

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

Neosoulink avatar May 13 '24 14:05 Neosoulink

Don't forget this! Perhaps do a Ctrl+H to replace hysic with hysics and hysicss with hysics to unify the naming scheme?

zalo avatar Jun 05 '24 22:06 zalo

I just realized this PR was delayed from r165 to r167 😆

Neosoulink avatar Jul 09 '24 21:07 Neosoulink

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

Neosoulink avatar Jul 21 '24 20:07 Neosoulink