|
9 | 9 | <body> |
10 | 10 |
|
11 | 11 | <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. |
13 | 14 | </div> |
14 | 15 |
|
15 | 16 | <script type="importmap"> |
|
28 | 29 | import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js'; |
29 | 30 | import Stats from 'three/addons/libs/stats.module.js'; |
30 | 31 |
|
31 | | - let camera, scene, renderer, stats; |
| 32 | + let camera, controls, scene, renderer, stats; |
32 | 33 | let physics, position; |
33 | 34 |
|
34 | 35 | let boxes, spheres; |
| 36 | + let physicBoxes = [], physicSpheres = []; |
35 | 37 |
|
36 | 38 | init(); |
37 | 39 |
|
|
110 | 112 |
|
111 | 113 | } |
112 | 114 |
|
113 | | - physics.addSceneToWorld( scene ); |
| 115 | + physics.addToWorld( floor, 0 ); |
| 116 | + physicBoxes = physics.addToWorld( boxes, 1 ); |
| 117 | + physicSpheres = physics.addToWorld( spheres, 1 ); |
114 | 118 |
|
115 | 119 | // |
116 | 120 |
|
|
125 | 129 |
|
126 | 130 | // |
127 | 131 |
|
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 ); |
129 | 152 | controls.target.y = 0.5; |
| 153 | + controls.autoRotate = true; |
130 | 154 | controls.update(); |
131 | 155 |
|
132 | 156 | animate(); |
|
153 | 177 |
|
154 | 178 | physics.step(); |
155 | 179 |
|
| 180 | + controls.update(); |
| 181 | + |
156 | 182 | requestAnimationFrame( animate ); |
157 | 183 |
|
158 | 184 | renderer.render( scene, camera ); |
|
0 commit comments