basketballTest

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 < 70/Math.PI) {
    //console.log(this.vel.y)
    if(this.obj.position.y <38/Math.PI) {
    if(this.count >0) {
          tmp = !tmp;
          this.pos.set(1.4,500,-350)//-350
          this.obj.position.copy(this.pos);
          this.vel.set(0,0,0);
          this.force.set(0,-10,0);
          console.log(this.count)
        }
      if(type === 2 && this.count ===0) {
      console.log(this.vel.y)
        this.vel.y *= -0.7;
        this.vel.z *= 0.7;
        this.vel.x *= 0.7;
      }
      this.count++;

    }
    else this.count =0;
    if(type === 1) {
      if(this.pos.z >100||this.pos.z < -500||this.pos.x >900||this.pos.x <-900||this.pos.y >1000){
        tmp = !tmp;
        //console.log(tmp)
        this.pos.set(-393.6,317,-226)
        this.obj.position.copy(this.pos);
        this.vel.set(23.5,0,-10);
      }
    }
  }

	// useless API
 /* moveTo(x,y,z) {
  	this.pos.set (x,y,z)
  }*/
  
  moveTo(thePos) {
  	this.pos.copy (thePos);
    this.obj.position.copy(thePos);
  }
  
  rotateTo (theta) {  // abs CCW angle
  	this.obj.rotation.z = theta;
   ...