hw7.1
by Leoooo
HTML
<div id="info">hw7 many Pucks</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js">
</script>
CSS
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
color: #000000
}
body {
overflow: hidden
}
JavaScript
var camera, scene, renderer, mesh, controls;
var clock;
var raycaster, pickables = [];
var allpuck = [];
var puck;
var wallLength = 200;
var mat=new THREE.MeshNormalMaterial();
var mouse = new THREE.Vector2();
var r=10;
document.addEventListener('mousedown', onDocumentMouseDown, false);
class Puck
{
constructor(point)
{
point.z = 0;
this.pos = point.clone();
this.vel = new THREE.Vector3(0, 800, 0);
this.puck = buildpuck();
this.puck.rotation.x = Math.PI / 2;
scene.add(this.puck);
}
checkWall()
{
if (this.pos.x >= wallLength / 2 - 1 - r && this.vel.x > 0) {
this.pos.x = wallLength / 2 - 1 - r;
this.vel.x *= -0.9; //right wall
}
if (this.pos.y >= wallLength / 2 - 1 - r && this.vel.y > 0) {
this.pos.y = wallLength / 2 - 1 - r;
this.vel.y *= -0.9; //up wall
}
if (this.pos.x <= -wallLength / 2 + 1 + r && this.vel.x < 0) {
this.pos.x = -wallLength / 2 + 1 + r;
this.vel.x *= -0.9; //left wall
}
if (this.pos.y <= -wallLength / 2 + 1 + r && this.vel.y < 0) {
this.pos.y = -wallLength / 2 + 1 + r;
this.vel.y *= -0.9; // down wall
}
}
checkCollision(manypucks)
{
if (this.pos.distanceTo(manypucks.pos) < 2*r) //黏住
{
var delta = this.pos.clone().sub(manypucks.pos);
var d = delta.length();
var diff = (d - 2*r) / d;
delta.multiplyScalar(0.5 * diff);
this.pos.sub(delta);
manypucks.pos.add(delta);
var V1 = new THREE.Vector3().copy(this.vel);
var V2 = new THREE.Vector3().copy(manypucks.vel);
var x1x2 = this.pos.clone().sub(manypucks.pos);
var x2x1 = manypucks.pos.clone().sub(this.pos);
var...