Skip to content
Permalink
Browse files
Detect collisions, stop the game on collision
  • Loading branch information
MattyRad committed Feb 8, 2020
1 parent d7daa3e commit 802d3313bfb39c48d09fb8e408f43199e82fe92f
Showing 1 changed file with 15 additions and 1 deletion.
@@ -40,7 +40,21 @@ var animate = function () {
}

if (jumping) {
player.position.y += 0.01;
player.position.y += 0.02;
}

for (var vi = 0; vi < player.geometry.vertices.length; vi++) {
var localVertex = player.geometry.vertices[vi].clone();
var globalVertex = localVertex.applyMatrix4( player.matrix );
var directionVector = globalVertex.sub( player.position );
var originPoint = player.position.clone();

var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() );
var collisionResults = ray.intersectObjects([obstacle]);

if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() ) {
throw Error('game over!');
}
}

renderer.render(scene, camera);

0 comments on commit 802d331

Please sign in to comment.