-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw2.html
277 lines (231 loc) · 7.96 KB
/
hw2.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<html>
<head>
<style>
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
color: #ffff00
}
</style>
</head>
<body>
<div id="info">HW3</div>
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<audio id="collisionsound" style="display:none"><source src="./sounds/collision3.wav" type='audio/wav'></audio>
<script>
class Puck {
constructor(color1, color2, id, x, z, a, b){
this.pos = new THREE.Vector3(a, 0, b);
this.vel = new THREE.Vector3(x, 0, z);//方向
this.force = new THREE.Vector3(0, 0, 0);//Vector3 = 三維向量
this.dt = 0.5;
this.eps = 1e-3;//0.001
this.cR = 1;
//mesh
this.mesh = new THREE.Mesh(new THREE.CylinderGeometry(15, 15, 4, 32), new THREE.MeshBasicMaterial(color2));
//meshlight
this.light = new THREE.PointLight(color1, 1, 50);
this.id = id;
}
Scene(){
scene.add(this.mesh);
scene.add(this.light);
this.mesh.position.y = 2;
}
position(pucks, walls, i, dt){
//Euler's method
this.vel.add(this.force.clone().multiplyScalar(this.dt));
this.pos.add(this.vel.clone().multiplyScalar(this.dt));
this.mesh.position.copy(this.pos);
this.mesh.position.y += 2;
//pucklight position
this.light.position.copy(this.mesh.position);
this.light.position.y += 10;
//wall check
for(; i<walls.length; i++){
let wall = walls[i];
//console.log(wall.getPoint);
if(this.pos.clone().sub(wall.getPoint).dot(wall.getNormal) < 15){
//console.log('collide');
var vN = wall.getNormal.clone().multiplyScalar(this.vel.dot(wall.getNormal));
var vT = this.vel.clone().sub(vN);
//vel = vT * (-CR).vN
//this.pos.x;
this.vel.copy(vT.add(vN.multiplyScalar(-this.cR)));
collisionsound.play();
}
}
//puck check
var j = 0;
for(; j < pucks.length ;j++){
if( this.id == pucks[j].id ) continue;
if(this.pos.distanceTo(pucks[j].pos)<=30){
let v1 = this.vel;
let v2 = pucks[j].getVel;
let x1 = this.pos;
let x2 = pucks[j].getPos;
let v1subv2 = v1.clone().sub(v2);
let v2subv1 = v2.clone().sub(v1);
let x1subx2 = x1.clone().sub(x2);
let x2subx1 = x2.clone().sub(x1);
let dot1 = v1subv2.clone().dot(x1subx2.clone());
let dot2 = v2subv1.clone().dot(x2subx1.clone());
let d1to2 = x1.distanceTo(x2);
let d2to1 = x2.distanceTo(x1);
let res1 = dot1/(d1to2*d1to2);
let res2 = dot2/(d2to1*d2to1);
var t = x2subx1.clone().normalize();
let d = (30-d2to1)/2;
this.setPos(this.pos.clone().sub(t.clone().multiplyScalar(d)));
pucks[j].setPos(pucks[j].pos.clone().add(t.clone().multiplyScalar(d)));
this.setVel(v1.clone().sub(x1subx2.clone().multiplyScalar(res1)));
pucks[j].setVel(v2.clone().sub(x2subx1.clone().multiplyScalar(res2)));
collisionsound.play();
//collisionSound.play();
}
}
}
get getPos(){
return this.pos;
}
get getVel(){
return this.vel;
}
setPos(pos){
this.pos.copy(pos)
}
setVel(vel){
this.vel.copy(vel)
}
}
class Wall{
constructor(xL, yL, zL, px, py, pz, mat){
this.mesh=new THREE.Mesh(new THREE.BoxGeometry(xL,yL,zL),mat);
//this.mesh = new THREE.Mesh(new THREE.BoxGeometry(xL, yL, zL), new THREE.MeshPhongMaterial(wallMaterial));
scene.add(this.mesh);
this.mesh.position.set(px, py, pz);
}
Location(pointX, pointY, pointZ, nx, ny, nz){
this.mesh.point = new THREE.Vector3(pointX, pointY, pointZ);
this.mesh.normal = new THREE.Vector3(nx, ny, nz);
}
get getPoint(){
return this.mesh.point;
}
get getNormal(){
return this.mesh.normal;
}
}
var p1, wall;
var camera, scene, renderer;
var pos, vel, force;
var puck;
var walls = [];
var pucks = [];
var pucklight;
var collisionSound, soundTrack;
var isDimming = false, soundVal = 1.0, sign = 1.0;
init();
animate();
function init() {
//initialization
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x888888);
document.body.appendChild(renderer.domElement);
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 500;
let controls = new THREE.OrbitControls(camera, renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
////////////////////////////////////////////////////////////////
/*格線地板
var gridXZ = new THREE.GridHelper(200, 20, 'red', 'white');
scene.add(gridXZ);
*/
var floor = new THREE.Mesh(new THREE.PlaneGeometry(200, 200), new THREE.MeshPhongMaterial({color:0xffffff}));
scene.add(floor);
floor.rotation.x = -Math.PI/2;
// collision suond
collisionSound = document.getElementById('collisionSound');
// puck set
var x = Math.floor(Math.random()*9);
var z = Math.floor(Math.random()*9);
p = new Puck(0x5cf2f2, {color:0x5cf2f2}, 0, x, z, 0, 0);
p.Scene();
pucks.push(p);
x = Math.floor(Math.random()*9);
z = Math.floor(Math.random()*9);
p = new Puck(0xc7cf3a, {color:0xc7cf3a}, 1, x, z, 50, 50);
p.Scene();
pucks.push(p);
x = Math.floor(Math.random()*9);
z = Math.floor(Math.random()*9);
p = new Puck(0x32e352, {color:0x32e352}, 2, x, z, -50, 50);
p.Scene();
pucks.push(p);
x = Math.floor(Math.random()*9);
z = Math.floor(Math.random()*9);
p = new Puck(0x3c37db, {color:0x3c37db}, 3, x, z, 50, -50);
p.Scene();
pucks.push(p);
x = Math.floor(Math.random()*9);
z = Math.floor(Math.random()*9);
p = new Puck(0xd12e1f, {color:0xd12e1f}, 4, x, z, -50, -50);
p.Scene();
pucks.push(p);
//wall set
var wallMaterial = new THREE.MeshLambertMaterial ({color: 0x385fc9,opacity:0.2,transparent:true});
//wall (0)
wall = new Wall(10, 20, 200, -105, 10, 0, wallMaterial);
wall.Location(-100, 0, 0, 1, 0, 0);
walls.push(wall);
//wall (1)
wall = new Wall(10, 20, 200, 105, 10, 0, wallMaterial);
wall.Location(100, 0, 0, -1, 0, 0);
walls.push(wall);
//wall (2)
wall = new Wall(200, 20, 10, 0, 10, 105, wallMaterial);
wall.Location(0, 0, -100, 0, 0, 1);
walls.push(wall);
//wall (3)
wall = new Wall(200, 20, 10, 0, 10, -105, wallMaterial);
wall.Location(0, 0, 100, 0, 0, -1);
walls.push(wall);
//Euler' method set
force = new THREE.Vector3(0, 0, 0);//Vector3 = 三維向量
vel = new THREE.Vector3(-10, 0, 0.5);
pos = new THREE.Vector3();
//light set
var light = new THREE.AmbientLight( 0x202020 ); // soft white light
scene.add( light );
//var light = new THREE.PointLight( 0xffffff, 1, 100 );
//light.position.set( 50, 50, 50 );
//scene.add( light );
}
function animate() {
soundVal += sign*0.01;
soundVal = THREE.Math.clamp (soundVal, 0, 1);
for(i = 0;i<pucks.length; i++){
p = pucks[i];
p.position(pucks, walls, 0, 0.5);
}
requestAnimationFrame(animate);
render();
}
function render() {
renderer.render(scene, camera);
}
function onWindowResize(){
//window resize
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
</script>
</body>
</html>