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 cylinder;
var raycaster;
var mouse = new THREE.Vector2();
var pickables = [];
var puck;
var agent;
var pos, vel, force;
var angle = 0;
var mass = 1;
var clock = new THREE.Clock();
var target = new THREE.Vector3();
var Agent = function (mesh) {
this.pos = new THREE.Vector3();
this.vel = new THREE.Vector3();
this.force = new THREE.Vector3();
this.target = new THREE.Vector3();
this.angle = 0
this.mesh = mesh;
this.maxSpeed = 20;
this.maxForce = 20;
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;
}
}
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);
// add my cylinder
var geometry = new THREE.BoxGeometry(15, 2, 5);
var material = new THREE.MeshNormalMaterial();
var cylinder = new THREE.Mesh(geometry, material);
scene.add(cylinder);
agent = new Agent(cylinder);
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);
...