Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
a formulaic hit detection. Pass in the two objects you want to check against to see if they are in collision.
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