Repository navigation
Expand file tree
/
Copy pathstandalone.js
More file actions
54 lines (39 loc) · 1.45 KB
/
Copy pathstandalone.js
File metadata and controls
54 lines (39 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// DOCUSAURUS: NoBundler start
import RAPIER from 'https://cdn.skypack.dev/@dimforge/rapier3d-compat';
RAPIER.init().then(() => {
// Run the simulation.
_run_simulation(RAPIER);
});
// OR using the await syntax:
async function run_simulation() {
await RAPIER.init();
// Run the simulation.
_run_simulation(RAPIER);
}
// DOCUSAURUS: NoBundler stop
run_simulation()
function _run_simulation(RAPIER) {
// Use the RAPIER module here.
let gravity = { x: 0.0, y: -9.81, z: 0.0 };
let world = new RAPIER.World(gravity);
// Create the ground
let groundColliderDesc = RAPIER.ColliderDesc.cuboid(10.0, 0.1, 10.0);
world.createCollider(groundColliderDesc);
// Create a dynamic rigid-body.
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic()
.setTranslation(0.0, 1.0, 1.0);
let rigidBody = world.createRigidBody(rigidBodyDesc);
// Create a cuboid collider attached to the dynamic rigidBody.
let colliderDesc = RAPIER.ColliderDesc.cuboid(0.5, 0.5, 0.5);
let collider = world.createCollider(colliderDesc, rigidBody);
// Game loop. Replace by your own game loop system.
let gameLoop = () => {
// Step the simulation forward.
world.step();
// Get and print the rigid-body's position.
let position = rigidBody.translation();
console.log("Rigid-body position: ", position.x, position.y, position.z);
setTimeout(gameLoop, 16);
};
gameLoop();
}