Skip to content

Instantly share code, notes, and snippets.

@RussellSpitzer
Created April 8, 2016 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RussellSpitzer/a46ec8818c73d84f552352ca76413e03 to your computer and use it in GitHub Desktop.
Save RussellSpitzer/a46ec8818c73d84f552352ca76413e03 to your computer and use it in GitHub Desktop.
AutoDrive v1
if(! (!this.collisionDetection || !this.collisionMap || !this.collisionMap.loaded)) {
if (this.speed < 7) {
this.key.forward = true
} else {
this.key.forward = false
}
var x = Math.round(this.collisionMap.pixels.width / 2 + this.dummy.position.x * this.collisionPixelRatio);
var z = Math.round(this.collisionMap.pixels.height / 2 + this.dummy.position.z * this.collisionPixelRatio);
var pos = new THREE.Vector3(x, 0, z);
var detectionBounds = 4
this.key.right=false
this.key.left=false
this.key.ltrigger = false
this.key.rtrigger = false
// Detection
this.repulsionVLeft.set(0.9, 0, 0);
this.repulsionVRight.set(-0.9, 0, 0);
this.dummy.matrix.rotateAxis(this.repulsionVLeft);
this.dummy.matrix.rotateAxis(this.repulsionVRight);
this.repulsionVLeft.multiplyScalar(detectionBounds);
this.repulsionVRight.multiplyScalar(detectionBounds);
var lPos = this.repulsionVLeft.addSelf(pos);
var rPos = this.repulsionVRight.addSelf(pos);
var lCol = this.collisionMap.getPixel(Math.round(lPos.x), Math.round(lPos.z)).r;
var rCol = this.collisionMap.getPixel(Math.round(rPos.x), Math.round(rPos.z)).r;
if (rCol > lCol) {// Turn right
this.key.right = true
if (this.speed > 2) {
this.key.rtrigger = true
}
}
else if (rCol < lCol) {// Turn left
this.key.left = true
if (this.speed > 2) {
this.key.ltrigger = true
}
}
// Look a short distance directly ahead, are we about to hit something? Turn HARD
detectionBounds = 11 + this.speed * 0.2
this.repulsionVLeft.set(0.1, 0, 0);
this.repulsionVRight.set(-0.1, 0, 0);
this.dummy.matrix.rotateAxis(this.repulsionVLeft);
this.dummy.matrix.rotateAxis(this.repulsionVRight);
this.repulsionVLeft.multiplyScalar(detectionBounds);
this.repulsionVRight.multiplyScalar(detectionBounds);
lPos = this.repulsionVLeft.addSelf(pos);
rPos = this.repulsionVRight.addSelf(pos);
lCol = this.collisionMap.getPixel(Math.round(lPos.x), Math.round(lPos.z)).r;
rCol = this.collisionMap.getPixel(Math.round(rPos.x), Math.round(rPos.z)).r;
if (rCol > lCol && this.speed > 4.5){
this.key.forward = false
this.key.right = true
this.key.rtrigger = true
}
else if (rCol < lCol && this.speed > 4.5) {
this.key.forward = false
this.key.ltrigger = true
this.key.left = true
}
if (rCol > 10 && lCol > 10 && this.speed > 5){
this.key.forward = false
this.key.ltrigger = true
this.key.rtrigger = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment