document.addEventListener('keydown',function(event) {
varcode = event.keyCode;
if(code == 37) { // left
marker.position.x = marker.position.x-5;
is_moving_left = true;
}
if(code == 38) { // up
marker.position.z = marker.position.z-5;
is_moving_forward = true;
}
if(code == 39) { // right
marker.position.x = marker.position.x+5;
is_moving_right = true;
}
if(code == 40) { // down
marker.position.z = marker.position.z+5;
is_moving_back = true;
}
if(code == 67) is_cartwheeling = !is_cartwheeling;// C
if(code == 70) is_flipping = !is_flipping; // F
if(detectCollisions()) {
if(is_moving_left) marker.position.x = marker.position.x+5;
if(is_moving_right) marker.position.x = marker.position.x-5;
if(is_moving_forward) marker.position.z = marker.position.z+5;
if(is_moving_back) marker.position.z = marker.position.z-5;
}
});
document.addEventListener('keyup',function(event) {
varcode = event.keyCode;
if(code == 37) is_moving_left = false;
if(code == 38) is_moving_forward = false;
if(code == 39) is_moving_right = false;
if(code == 40) is_moving_back = false;
});
functiondetectCollisions() {
varvector =newTHREE.Vector3(0, -1, 0);
varray =newTHREE.Ray(marker.position, vector);
varintersects = ray.intersectObjects(not_allowed);
if(intersects.length > 0)returntrue;
returnfalse;
}
</script>
report erratum • discuss
Code: Collisions • 233
Prepared exclusively for Michael Powell