DanboardStory_BasketballGameByTouch

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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<script src="https://jyunming-chen.github.io/tutsplus/js/KeyboardState.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.force = new THREE.Vector3();
    this.count = 0;

    this.obj = new THREE.Object3D();
    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 < 38/Math.PI) {

      if(this.count >2){ 
        tmp = !tmp;
        nready = !nready;
        //console.log(tmp)
        this.obj.position.copy(target.getWorldPosition());
        this.pos.copy(this.obj.position);
        this.vel.copy(v0);
      }

      if(this.count ===0) {
        this.vel.y *= -0.7;
        this.vel.z *= 0.7;
        this.vel.x *=0.7;
      }
      this.count ++
    } 
    else this.count = 0;
  }

  // useless API
  /* moveTo(x,y,z) {
  	this.pos.set (x,y,z)
  }*/

  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 Board  {
  constructor () {
    this.type = 'board'
    this.normal = new THREE.Vector3(0,0,1)
    this.pc = new THREE.Vector3()
    this.obj = new THREE.Object3D();
    //this.width = width;  // for display purpose, should be infinite
    var black = new THREE.MeshLambertMaterial({
      map: loader.load( "https://i.imgur.com/dBsfkR6.jpg" )});
    var boardTexture...