-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy path02-cannonball3d.html
More file actions
229 lines (180 loc) · 7.08 KB
/
Copy path02-cannonball3d.html
File metadata and controls
229 lines (180 loc) · 7.08 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!--
Copyright 2021 Matthias Müller - Ten Minute Physics
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cannonball 3d</title>
<style>
body {
font-family: verdana;
font-size: 15px;
}
</style>
</head>
<body>
<h1>Cannonball 3d</h1>
<button id = "buttonRun" onclick="run()" class="button">Run</button>
<button onclick="restart()" class="button">Restart</button>
<br><br>
<div id="container"></div>
<script src="https://unpkg.com/three@0.139.2/build/three.min.js"></script>
<script src="https://unpkg.com/three@0.139.2/examples/js/controls/OrbitControls.js"></script>
<script>
var threeScene;
var renderer;
var camera;
var cameraControl;
// ------------------------------------------------------------------
// physics scene
var physicsScene =
{
gravity : new THREE.Vector3(0.0, -10.0, 0.0),
dt : 1.0 / 60.0,
worldSize : { x: 1.5, z : 2.5 },
paused: true,
objects: [],
};
// ------------------------------------------------------------------
class Ball {
constructor(pos, radius, vel, scene)
{
// physics data
this.pos = pos;
this.radius = radius;
this.vel = vel;
// visual mesh
var geometry = new THREE.SphereGeometry( radius, 32, 32 );
var material = new THREE.MeshPhongMaterial({color: 0xff0000});
this.visMesh = new THREE.Mesh( geometry, material );
this.visMesh.position.copy(pos);
threeScene.add(this.visMesh);
}
simulate()
{
this.vel.addScaledVector(physicsScene.gravity, physicsScene.dt);
this.pos.addScaledVector(this.vel, physicsScene.dt);
if (this.pos.x < -physicsScene.worldSize.x) {
this.pos.x = -physicsScene.worldSize.x; this.vel.x = -this.vel.x;
}
if (this.pos.x > physicsScene.worldSize.x) {
this.pos.x = physicsScene.worldSize.x; this.vel.x = -this.vel.x;
}
if (this.pos.z < -physicsScene.worldSize.z) {
this.pos.z = -physicsScene.worldSize.z; this.vel.z = -this.vel.z;
}
if (this.pos.z > physicsScene.worldSize.z) {
this.pos.z = physicsScene.worldSize.z; this.vel.z = -this.vel.z;
}
if (this.pos.y < this.radius) {
this.pos.y = this.radius; this.vel.y = -this.vel.y;
}
this.visMesh.position.copy(this.pos);
}
}
// ------------------------------------------------------------------
function initPhysics(scene)
{
var radius = 0.2;
var pos = new THREE.Vector3(radius, radius, radius);
var vel = new THREE.Vector3(2.0, 5.0, 3.0);
physicsScene.objects.push(new Ball(pos, radius, vel, scene));
}
// ------------------------------------------------------------------
function simulate()
{
if (physicsScene.paused)
return;
for (var i = 0; i < physicsScene.objects.length; i++)
physicsScene.objects[i].simulate();
}
// ------------------------------------------
function initThreeScene()
{
threeScene = new THREE.Scene();
// Lights
threeScene.add( new THREE.AmbientLight( 0x505050 ) );
threeScene.fog = new THREE.Fog( 0x000000, 0, 15 );
var spotLight = new THREE.SpotLight( 0xffffff );
spotLight.angle = Math.PI / 5;
spotLight.penumbra = 0.2;
spotLight.position.set( 2, 3, 3 );
spotLight.castShadow = true;
spotLight.shadow.camera.near = 3;
spotLight.shadow.camera.far = 10;
spotLight.shadow.mapSize.width = 1024;
spotLight.shadow.mapSize.height = 1024;
threeScene.add( spotLight );
var dirLight = new THREE.DirectionalLight( 0x55505a, 1 );
dirLight.position.set( 0, 3, 0 );
dirLight.castShadow = true;
dirLight.shadow.camera.near = 1;
dirLight.shadow.camera.far = 10;
dirLight.shadow.camera.right = 1;
dirLight.shadow.camera.left = - 1;
dirLight.shadow.camera.top = 1;
dirLight.shadow.camera.bottom = - 1;
dirLight.shadow.mapSize.width = 1024;
dirLight.shadow.mapSize.height = 1024;
threeScene.add( dirLight );
// Geometry
var ground = new THREE.Mesh(
new THREE.PlaneBufferGeometry( 20, 20, 1, 1 ),
new THREE.MeshPhongMaterial( { color: 0xa0adaf, shininess: 150 } )
);
ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
ground.receiveShadow = true;
threeScene.add( ground );
var helper = new THREE.GridHelper( 20, 20 );
helper.material.opacity = 1.0;
helper.material.transparent = true;
helper.position.set(0, 0.002, 0);
threeScene.add( helper );
// Renderer
renderer = new THREE.WebGLRenderer();
renderer.shadowMap.enabled = true;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( 0.8 * window.innerWidth, 0.8 * window.innerHeight );
window.addEventListener( 'resize', onWindowResize, false );
container.appendChild( renderer.domElement );
// Camera
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 100);
camera.position.set(0, 1, 4);
camera.updateMatrixWorld();
threeScene.add( camera );
cameraControl = new THREE.OrbitControls(camera, renderer.domElement);
cameraControl.zoomSpeed = 2.0;
cameraControl.panSpeed = 0.4;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function run() {
var button = document.getElementById('buttonRun');
if (physicsScene.paused)
button.innerHTML = "Stop";
else
button.innerHTML = "Run";
physicsScene.paused = !physicsScene.paused;
}
function restart() {
location.reload();
}
// make browser to call us repeatedly -----------------------------------
function update() {
simulate();
renderer.render(threeScene, camera);
cameraControl.update();
requestAnimationFrame(update);
}
initThreeScene();
initPhysics();
update();
</script>
</body>
</html>