Turret(HUD)

by 蔡 育曄

HTML

<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>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>

CSS

body {
  overflow: hidden;
}

JavaScript

class Ball {
  constructor (mass = 1, radius=38/Math.PI, friction = 0) {
    this.type = 'ball';
    this.mass = mass;  // may need to differentiate basketball & bowling ball
    this.radius = radius;
    this.friction = friction;  // for contact

    // for dynamics calculation
    this.pos = new THREE.Vector3();
    this.vel = new THREE.Vector3();
    this.initV =  new THREE.Vector3();
    this.force = new THREE.Vector3();
    this.count = 0;

    this.obj = new THREE.Object3D();
    loader.setCrossOrigin ('');
    let mesh = new THREE.Mesh (new THREE.SphereGeometry(radius, 32, 32), new THREE.MeshLambertMaterial({map:loader.load("https://i.imgur.com/Sw4OGXN.png")}))
    this.obj.add (mesh)
    scene.add (this.obj)
  }

  update(dt) {

    // after GLOBAL collision & contact
    this.vel.add (this.force.clone().multiplyScalar(dt))
    this.pos.add (this.vel.clone().multiplyScalar(dt)) 
    this.obj.position.copy (this.pos);
    if(this.obj.position.y <this.radius) {
       this.pos.set(-20,this.radius,30);
        this.vel.copy(ball.initV);
         update = !update;
    }  
  }

  // useless API
  moveTo(thePos) {
    this.pos.copy (thePos);
    this.obj.position.copy (this.pos);
  }

  rotateTo (theta) {  // abs CCW angle
    this.obj.rotation.z = theta;
    //this.normal.applyEuler (new THREE.Euler (0,0,theta))
  }
}

class Button {
  constructor (size, x, y){
    this.size = size;
    this.centerX = x;
    this.centerY = y;
  }
  d1To (v) { // 1-norm
    return Math.abs (v[0]-this.centerX) + Math.abs(v[1]-this.centerY);
  }
  d2To (v) { // 2-norm
    return Math.sqrt ( (v[0]-this.centerX)*(v[0]-this.centerX) 
                      + (v[1]-this.centerY)*(v[1]-this.centerY) );
  }
  dInfTo (v) { // inf-norm
    return Math.max( Math.abs (v[0]-this.centerX), Math.abs(v[1]-this.centerY) )
  }
}

var renderer, camera, controls, scene, axes, stats;
var sceneHUD, cameraHUD;
var whRatio, halfW, halfH;
var d, v0, theta,tmpV;
var loader = new...