DodgeBall5

by Bai Shiuan Huang

HTML

<div id="info">Dodege Ball
<br>使用QW選擇方向發射; 
<br>Press D or S to increase power; Release to shoot
<p id='v0value'></p>
<button id='reset'>Reset
</button>
</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>
<script src="https://jyunming-chen.github.io/tutsplus/js/KeyboardState.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>

CSS

#info {
  position: absolute;
  top: 0px;
  width: 100%;
  padding: 10px;
  text-align: center;
  color: #ffff00
}

body {
  overflow: hidden;
}

JavaScript

var camera, scene, renderer, controls;
var h, L0, v0, theta, theta2 = [-Math.PI/6, 0, Math.PI/6], theta2_c;
var clock, force, line, Keeper, aa;
var increaseV0 = false;
var keyboard = new KeyboardState();
var Attack_IF;
var d_ball, keeper;
var Outs = [], Ins = [];
let returnx, returny, returnz;
class OutPlayer{
  constructor(x=0,xref=0,theta=0,thetaRef=0){
  /////////////////////
  this.theta = theta;
    this.thetaRef = thetaRef
    this.x = x;
    this.xref = xref;
    this.v = 0;
    this.KP = 100;   // 'spring constant'
    this.KD = 20;
  //////////////////////
    this.mesh = makePlayer();
    this.pos = new THREE.Vector3();
    this.group;
    this.haveBall = false;
    scene.add(this.mesh);
  }
  updateTheta (dt) {
  	let f = -this.KP * (this.theta-this.thetaRef) - this.KD*this.v;
    this.v += f*dt;
    this.theta += this.v*dt
    return this.theta
  }
   update (dt) {
  	let f = -this.KP * (this.x-this.xref) - this.KD*this.v;
    this.v += f*dt;
    this.x += this.v*dt
    return this.x
  }
  setRef (ref) {
  	this.xref = ref;
  }
  setThetaRef (ref) {
  	this.thetaRef = ref;
  }
  hit(ball){
    if(Math.sqrt((ball.mesh.position.x - this.mesh.position.x)*(ball.mesh.position.x - this.mesh.position.x) + (ball.mesh.position.z - this.mesh.position.z)*(ball.mesh.position.z - this.mesh.position.z))<= 7 && ball.mesh.position.y >= 0 && ball.mesh.position.y < 17){
      theta2_c = 1;
      cleaner();
      this.haveBall = true;
      Keeper.pos = this.mesh.position;
      Keeper.rotationY = this.mesh.rotation.y;
      Keeper.group = "Outs"
      ball.shootStart = false;
      let posF_DB = new THREE.Vector3(5, 10, 0);
      this.mesh.updateMatrixWorld();
      this.mesh.localToWorld(posF_DB);
      ball.mesh.position.copy(posF_DB);
      Attack_IF.visible = false;
    }
  }
}
class InPlayer{
  constructor(){
    this.mesh = makePlayer();
    this.pos = new THREE.Vector3();
    this.haveBall = false;
    scene.add(this.mesh);
  }
  hit(ball){
   ...