DodgeBall6_add_lambda

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>
<script src="https://yuang2tank.github.io/Dodgeball/DodgeBall001.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 theta, theta2;
var clock, line;
var keyboard = new KeyboardState();
var Attack_IF, d_ball, Keeper, Catcher;
var Outs = [], Ins = [];
var Victims = [];
var targetC;

class Agent {
  constructor(pos, mesh) {
    this.pos = pos.clone();
    this.vel = new THREE.Vector3();
    this.force = new THREE.Vector3();
    this.target = new THREE.Vector3();
    this.size = 3;
    this.mesh = mesh;
    this.MAXSPEED = 50;
    this.ARRIVAL_R = 30;
    this.nbhd = [];
  }
  update(dt) {
    this.accumulateForce();
    this.vel.add(this.force.clone().multiplyScalar(dt));
    // velocity modulation
    let diff = this.target.clone().sub(this.pos)
    let dst = diff.length();
    if (dst < this.ARRIVAL_R) {
      this.vel.setLength(dst)
    }
    this.pos.add(this.vel.clone().multiplyScalar(dt))
    this.mesh.position.copy(this.pos)
  }
  distanceToTarget() {
    return this.pos.distanceTo(this.target)
  }
  distanceTo(otherAgent) {
    return this.pos.distanceTo(otherAgent.pos)
  }
  addNbr(otherAgent) {
    this.nbhd.push(otherAgent)
  }
  setTarget(target) {
    this.target.copy(target)
  }
  targetInducedSeekForce(targetPos) {
    return targetPos.clone().sub(this.pos).normalize().multiplyScalar(this.MAXSPEED).sub(this.vel)
  }
  targetInducedFleeForce(targetPos) {
    return targetPos.clone().sub(this.pos).normalize().multiplyScalar(-this.MAXSPEED).sub(this.vel)
  }

  accumulateForce(){
    // flee if close enough
    if (this.distanceToTarget() < 50) {
    	console.log ('getting close ...')
      this.force.copy(this.targetInducedFleeForce(this.target));
    } else
      this.force.set(0, 0, 0)
      
   // implement drag
   this.force.add (this.vel.clone().multiplyScalar (-1.5))
   

    // containment
    let ct = this.setContainmentTarget();
    if (ct !== null)
			this.force.add (this.targetInducedSeekForce (ct));
    
    // coherence
    if (this.nbhd.length > 0) {
      let sum = new THREE.Vector3();
      for (let i = 0; i <...