Skip to content

Commit 1cf7f7c

Browse files
committed
feat: add keyDown interactions
### Description - Use `addToWorld` instead of `addSceneToWorld` - Add Window `keyboard` event to apply impulse to objects (to shake)
1 parent ef37a25 commit 1cf7f7c

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

examples/physics_rapier_instancing.html

Lines changed: 30 additions & 4 deletions
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+
Press any <strong>key</strong> to shake.
1314
</div>
1415

1516
<script type="importmap">
@@ -28,10 +29,11 @@
2829
import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js';
2930
import Stats from 'three/addons/libs/stats.module.js';
3031

31-
let camera, scene, renderer, stats;
32+
let camera, controls, scene, renderer, stats;
3233
let physics, position;
3334

3435
let boxes, spheres;
36+
let physicBoxes = [], physicSpheres = [];
3537

3638
init();
3739

@@ -110,7 +112,9 @@
110112

111113
}
112114

113-
physics.addSceneToWorld( scene );
115+
physics.addToWorld( floor, 0 );
116+
physicBoxes = physics.addToWorld( boxes, 1 );
117+
physicSpheres = physics.addToWorld( spheres, 1 );
114118

115119
//
116120

@@ -125,8 +129,28 @@
125129

126130
//
127131

128-
const controls = new OrbitControls( camera, renderer.domElement );
132+
window.onkeydown = () =>{
133+
134+
[ ...physicBoxes, ...physicSpheres ].map( ( item ) => {
135+
136+
item.rigidBody.applyImpulse(
137+
{
138+
x: ( Math.random() - 0.5 ) * 5,
139+
y: Math.random() * 5,
140+
z: ( Math.random() - 0.5 ) * 5
141+
},
142+
true
143+
);
144+
145+
} );
146+
147+
};
148+
149+
//
150+
151+
controls = new OrbitControls( camera, renderer.domElement );
129152
controls.target.y = 0.5;
153+
controls.autoRotate = true;
130154
controls.update();
131155

132156
animate();
@@ -153,6 +177,8 @@
153177

154178
physics.step();
155179

180+
controls.update();
181+
156182
requestAnimationFrame( animate );
157183

158184
renderer.render( scene, camera );

0 commit comments

Comments
 (0)