-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
119 lines (103 loc) · 2.87 KB
/
index.html
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Physics World Demo with Orbit Controls</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
}
canvas {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three/build/three.module.min.js",
"three/addons/": "https://unpkg.com/three/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import * as SmallPhysics from './PhysicsEngine.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
// Create a new Three.js scene
const scene = new THREE.Scene();
// Create a new camera
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = 0;
camera.position.y = 35;
camera.position.x = 35;
// Create a new renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add orbit controls
const controls = new OrbitControls(camera, renderer.domElement);
// Create a new physics world
const world = new SmallPhysics.PhysicsWorld(scene);
// Create a ground plane
const ground = new SmallPhysics.PhysicsPlane({
width: 50,
height: 50,
color: 0x999999,
mass: 0,
rotation: new THREE.Euler(-Math.PI / 2, 0, 0),
});
// Create a sphere
const sphere = new SmallPhysics.PhysicsSphere({
radius: 2,
mass: 10,
bounciness: 0.1,
color: 0xff0000,
position: new THREE.Vector3(0, 10, 0),
});
// Create a box
const box = new SmallPhysics.PhysicsBox({
width: 2,
height: 2,
depth: 2,
mass: 1,
color: 0x00ff00,
position: new THREE.Vector3(20, 18, 5),
});
const box2 = new SmallPhysics.PhysicsBox({
width: 2,
height: 2,
depth: 2,
mass: 1,
color: 0x00ff00,
position: new THREE.Vector3(10, 30, 1),
});
const box3 = new SmallPhysics.PhysicsBox({
width: 2,
height: 2,
depth: 2,
mass: 1,
color: 0x00ff00,
position: new THREE.Vector3(10, 50, 1),
});
world.add(ground).add(sphere).add(box).add(box2).add(box3);
// Render loop
function animate() {
requestAnimationFrame(animate);
world.update();
renderer.render(scene, camera);
}
animate();
</script>