hw4 prototype
seek
by tom0000p
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 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.update = function(dt) {
// compute force
this.force = this.target.clone().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;
// catch handling
if (this.pos.distanceTo(this.target) < 2) {
this.vel.set(0, 0, 0);
angle = Math.random() * Math.PI * 2;
}
}
}
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 THREE.GridHelper(100, 10);
gridXZ.setColors(new THREE.Color(0xff0000), new THREE.Color(0xffffff));
scene.add(gridXZ);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x888888);
controls = new THREE.OrbitControls(camera, renderer.domElement);
document.body.appendChild(renderer.domElement);
...