MCPcopy Create free account
hub / github.com/SuboptimalEng/three-js-tutorials / App

Function App

15-physics-part-01/src/App.jsx:9–113  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

7import SceneInit from './lib/SceneInit';
8
9function App() {
10 useEffect(() => {
11 const test = new SceneInit('myThreeJsCanvas');
12 test.initialize();
13 test.animate();
14
15 // part 0 - add axis helper
16 const axesHelper = new THREE.AxesHelper(8);
17 test.scene.add(axesHelper);
18
19 // const boxGeometry = new THREE.BoxGeometry(4, 4, 4);
20 // const boxMaterial = new THREE.MeshNormalMaterial();
21 // const boxMesh = new THREE.Mesh(boxGeometry, boxMaterial);
22 // test.scene.add(boxMesh);
23
24 // part 1 - set up world physics with a few objects
25 // note - code is from the cannon-es documentation
26 const physicsWorld = new CANNON.World({
27 gravity: new CANNON.Vec3(0, -9.82, 0),
28 });
29
30 // create a ground body with a static plane
31 const groundBody = new CANNON.Body({
32 type: CANNON.Body.STATIC,
33 // infinte geometric plane
34 shape: new CANNON.Plane(),
35 });
36 // rotate ground body by 90 degrees
37 groundBody.quaternion.setFromEuler(-Math.PI / 2, 0, 0);
38 physicsWorld.addBody(groundBody);
39
40 // create a sphere and set it at y=10
41 const radius = 1;
42 const sphereBody = new CANNON.Body({
43 mass: 5,
44 shape: new CANNON.Sphere(radius),
45 });
46 sphereBody.position.set(0, 7, 0);
47 physicsWorld.addBody(sphereBody);
48
49 // run the physics simulation on each animation frame
50 // const animate = () => {
51 // physicsWorld.fixedStep();
52 // window.requestAnimationFrame(animate);
53 // };
54 // animate();
55
56 // part 2 - bind cannon debugger to the three.js scene + physics world
57 const cannonDebugger = new CannonDebugger(test.scene, physicsWorld, {
58 // color: 0xff0000,
59 });
60
61 // const animate = () => {
62 // physicsWorld.fixedStep();
63 // cannonDebugger.update();
64 // window.requestAnimationFrame(animate);
65 // };
66 // animate();

Callers

nothing calls this directly

Calls 3

initializeMethod · 0.95
animateMethod · 0.95
animateFunction · 0.70

Tested by

no test coverage detected