Last active
August 5, 2021 03:51
-
-
Save TrishZwei/a3bbb340ea8bd2635d60 to your computer and use it in GitHub Desktop.
circleHit: a formulaic hit detection for two circles. Pass in the two objects you want to check against to see if they are in collision.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function circleHit(circle1, circle2) { | |
//circle1 is first parameter, circle2 is second parameter | |
//in bullet vs. enemy circle1 is the current bullet, circle2 is the current enemy | |
var c1 = $(circle1); //assigns to new local variable | |
var c2 = $(circle2); | |
c1.r = c1.width() / 2; //r stands for radius | |
c1.x = c1.position().left + c1.r; | |
c1.y = c1.position().top + c1.r; | |
c2.r = c2.width() / 2; | |
c2.x = c2.position().left + c2.r; | |
c2.y = c2.position().top + c2.r; | |
distx = c1.x - c2.x; | |
disty = c1.y - c2.y; | |
squaredist = (distx * distx) + (disty * disty) | |
return squaredist <= (c1.r + c2.r) * (c1.r + c2.r) | |
}//end circleHit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment