agent
seek, arrival
by jmcjc5u
HTML
<div id="info">Agent (Pursuit)</div>
CSS
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
color: #ffff00
}
body {
overflow: hidden
}
JavaScript
// from Three.JS Installation note:
// https://threejs.org/docs/#manual/en/introduction/Installation
// also, Q&A from discourse.threejs.org
// https://discourse.threejs.org/t/failed-installation-from-cdn/35227
import * as THREE from 'https://cdn.skypack.dev/[email protected]';
import { OrbitControls } from 'https://cdn.skypack.dev/[email protected]/examples/jsm/controls/OrbitControls.js';
( function( ) {
Math.clamp = function(val,min,max) {
return Math.min(Math.max(val,min),max);
}
} )();
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 = 60;
this.ARRIVAL_R = 30;
// for orientable agent
this.angle = 0;
}
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)
}
let speed = this.vel.length()
this.vel.setLength(Math.clamp (speed, 0, this.MAXSPEED))
this.pos.add(this.vel.clone().multiplyScalar(dt))
this.mesh.position.copy(this.pos)
// for orientable agent
// non PD version
if (this.vel.length() > 0.1) {
this.angle = Math.atan2 (-this.vel.z, this.vel.x)
this.mesh.rotation.y = this.angle
}
}
setTarget(target) {
this.target.copy(target)
}
targetInducedForce(targetPos) {
return targetPos.clone().sub(this.pos).normalize().multiplyScalar(this.MAXSPEED).sub(this.vel)
}
accumulateForce() {
// seek
this.force.copy(this.targetInducedForce(this.target));
}
}
////////////////////
var camera, scene, renderer;
var target;
var pickables;
var agent;
var animate = function () {
var pursuit = new THREE.Vector3();
console.log ('pursuit newed ...')
return function...