hw4 prototype
by Rebecca Chen
HTML
<div id="info">hw4 helper</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
<script src="https://dl.dropboxusercontent.com/u/3587259/Code/Threejs/OrbitControls.js">
</script>
<script src="https://jyunming-chen.github.io/tutsplus/js/KeyboardState.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 keyboard = new KeyboardState();
var raycaster;
var mouse = new THREE.Vector2();
var pickables = [];
var puck;
var agent;
var angle = 0;
var targetCatch = false;
var targetOmega = 0.3;
var mass = 1;
var clock = new THREE.Clock();
var target = new THREE.Vector3();
var Agent = function(mesh, initPos) {
this.pos = new THREE.Vector3();
if (initPos)
this.pos.copy(initPos);
this.vel = new THREE.Vector3();
this.force = new THREE.Vector3();
this.target = new THREE.Vector3();
this.angle = 0
this.mesh = mesh;
this.maxSpeed = 60;
this.maxForce = 60;
this.setTarget = function(target) {
this.target.copy(target);
}
this.catch = function() {
if (this.pos.distanceTo(this.target) < 10) {
targetCatch = true;
this.vel.set(0, 0, 0);
return true;
} else {
return false;
}
}
this.update = function(dt) {
// compute force
// implement pursuit
var D = this.target.distanceTo (this.pos);
var eta = D/this.maxSpeed;
var pursuitTarget = new THREE.Vector3(100*Math.cos(angle+eta*targetOmega), 0, 100*Math.sin(angle+eta*targetOmega));
this.force = pursuitTarget.sub(this.pos).setLength(this.maxSpeed).sub(this.vel);
// force clamping
if (this.force.length() > this.maxForce)
this.force.setLength(this.maxForce);
this.vel.add(this.force.clone().multiplyScalar(dt));
// velocity clamping
if (this.vel.length() > this.maxSpeed)
this.vel.setLength(this.maxSpeed);
this.pos.add(this.vel.clone().multiplyScalar(dt));
if (this.vel.length() > 0.001) {
this.angle = Math.atan2(-this.vel.z, this.vel.x);
}
this.mesh.position.copy(this.pos);
this.mesh.rotation.y = this.angle;
}
}
init();
animate();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 500;
scene.add(camera);
var gridXZ = new...