Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat | Rapier physics example Improvement #28203

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from

Conversation

Neosoulink
Copy link

ℹ️ 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

rapier-instancing.-.Made.with.Clipchamp.mp4

🔖 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

### Description

- Migrate the **old function** implementation to the **class** version
- `Expose` more Rapier properties (`rigidBody`, `collider`, ...)
  - Store more `RAPIER` props in the `WeakMap`
- Add comments (with types support) using `JsDoc`
### Description

- Use `addToWorld` instead of `addSceneToWorld`
- Add Window `keyboard` event to apply impulse to objects (to shake)
@Neosoulink Neosoulink changed the title Feat | Rapier physic example Improvements Feat | Rapier physics example Improvement Apr 24, 2024
@Neosoulink Neosoulink force-pushed the feat/rapier-physic-improvement branch from afcd62e to 5a2d9fb Compare April 24, 2024 17:55
@mrdoob
Copy link
Owner

mrdoob commented Apr 24, 2024

Love the shake!

@Neosoulink
Copy link
Author

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

const radius = parameters.radius !== undefined ? parameters.radius : 1;
return RAPIER.ColliderDesc.ball( radius );

export class Physic {
Copy link
Owner

@mrdoob mrdoob May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be "Physics" instead of "Physic"?

There are a few other instances of "Physic" in other variable names...

@Neosoulink
Copy link
Author

matrix_scale.mp4

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

I think I could help others by being able to control the scale...

cc: @mrdoob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants