Skip to content

Instantly share code, notes, and snippets.

@jason-enstedt
Created March 10, 2020 00:19
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 jason-enstedt/48edf20c7617a368413b7362db6e0314 to your computer and use it in GitHub Desktop.
Save jason-enstedt/48edf20c7617a368413b7362db6e0314 to your computer and use it in GitHub Desktop.
function circleCollison(p1x, p1y, r1, p2x, p2y, r2){
let radiusSum;
let xDiff;
let yDiff;
radiusSum = r1 + r2;
xDiff = p1x - p2x;
yDiff = p1y - p2y;
if(radiusSum > Math.sqrt((xDiff * xDiff) + (yDiff * yDiff)) ){
console.log('Player Coord: ' + p1x + ':' + p1y);
console.log('Enemy Coord: ' + p2x + ':' + p2y);
return true;
}else{
return false;
}
}
if(asteroids.length !== 0){
for(let k = 0; k < asteroids.length; k++){
if(circleCollison(ship.x, ship.y, 25, asteroids[k].x, asteroids[k].y, asteroids[k].collisionRadius)){
ship.x = canvasWidth / 2;
ship.y = canvasHeight / 2;
ship.velX = 0;
ship.velY = 0;
lives -= 1;
console.log(lives);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment