hw4 prototype
seek
by j91157j91157
HTML
<div id="info">hw4 helper</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r78/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.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 agents = [];
var pickables = [];
var pickplane;
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
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);
else
this.pos.set (200*Math.random(), 0, 200*Math.random());
this.vel = new THREE.Vector3();
this.force = new THREE.Vector3();
this.target = new THREE.Vector3();
this.angle = 0;
this.mesh = mesh.clone(); scene.add (this.mesh);
this.maxSpeed = 60;
this.maxForce = 60;
this.setTarget = function(target) {
this.target.copy(target);
}
this.update = function(dt) {
// compute force
if (this.target) {
this.force = this.target.clone().sub(this.pos).setLength(this.maxSpeed).sub(this.vel);
}
// group steering
groupSteer (this);
// 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.target && this.pos.distanceTo(this.target) < 2) {
this.vel.set(0, 0, 0);
}
}
}
function groupSteer (myself) {
// find agents in my neighborhood
var nbhd = [];
var R = 10;
for (var i = 0; i < agents.length; i++) {
if (agents[i] === myself) {
continue;
}
if (myself.pos.distanceToSquared (agents[i].pos) < R*R) {
nbhd.push (agents[i]);
}
}
// find separation force ... the most important one !
for (var i = 0; i < nbhd.length; i++) {
var r =...