Skip to content

Commit 083a11c

Browse files
authored
RapierPhysics: Add applyImpulse(). (#33459)
1 parent 6b313e6 commit 083a11c

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

examples/jsm/physics/RapierPhysics.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,20 @@ async function RapierPhysics() {
281281

282282
}
283283

284+
function applyImpulse( mesh, impulse, index = 0 ) {
285+
286+
let { body } = meshMap.get( mesh );
287+
288+
if ( mesh.isInstancedMesh ) {
289+
290+
body = body[ index ];
291+
292+
}
293+
294+
body.applyImpulse( impulse, true );
295+
296+
}
297+
284298
function addHeightfield( mesh, width, depth, heights, scale ) {
285299

286300
const shape = RAPIER.ColliderDesc.heightfield( width, depth, heights, scale );
@@ -414,7 +428,7 @@ async function RapierPhysics() {
414428

415429
/**
416430
* Adds a heightfield terrain to the physics simulation.
417-
*
431+
*
418432
* @method
419433
* @name RapierPhysics#addHeightfield
420434
* @param {Mesh} mesh - The Three.js mesh representing the terrain.
@@ -427,7 +441,18 @@ async function RapierPhysics() {
427441
* @param {number} scale.z - Scale factor for depth.
428442
* @returns {RigidBody} The created Rapier rigid body for the heightfield.
429443
*/
430-
addHeightfield: addHeightfield
444+
addHeightfield: addHeightfield,
445+
446+
/**
447+
* Applies an impulse to the given mesh which is part of the physics simulation.
448+
*
449+
* @method
450+
* @name RapierPhysics#applyImpulse
451+
* @param {Mesh} mesh - The mesh to apply the impulse to.
452+
* @param {Vector3} impulse - The impulse to apply.
453+
* @param {number} [index=0] - If the mesh is instanced, the index represents the instanced ID.
454+
*/
455+
applyImpulse: applyImpulse
431456

432457
};
433458

examples/physics_rapier_instancing.html

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
<body>
1010

1111
<div id="info">
12-
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> physics - <a href="https://github.com/dimforge/rapier.js" target="_blank">rapier</a> instancing
12+
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> physics - <a href="https://github.com/dimforge/rapier.js" target="_blank">rapier</a> instancing<br />
13+
<button id="shake" style="margin-top: 25px;">SHAKE</button>
1314
</div>
1415

1516
<script type="importmap">
@@ -140,6 +141,28 @@
140141
controls.target.y = 0.5;
141142
controls.update();
142143

144+
document.querySelector( 'button#shake' ).addEventListener( 'click', () => {
145+
146+
const impulse = new THREE.Vector3();
147+
148+
for ( let i = 0; i < spheres.count; i ++ ) {
149+
150+
impulse.set( ( Math.random() - 0.5 ) * 5, Math.random() * 5, ( Math.random() - 0.5 ) * 5 );
151+
152+
physics.applyImpulse( spheres, impulse, i );
153+
154+
}
155+
156+
for ( let i = 0; i < boxes.count; i ++ ) {
157+
158+
impulse.set( ( Math.random() - 0.5 ) * 5, Math.random() * 5, ( Math.random() - 0.5 ) * 5 );
159+
160+
physics.applyImpulse( boxes, impulse, i );
161+
162+
}
163+
164+
} );
165+
143166
setInterval( () => {
144167

145168
let index = Math.floor( Math.random() * boxes.count );

0 commit comments

Comments
 (0)