Skip to content

Instantly share code, notes, and snippets.

@TrishZwei
Last active August 5, 2021 03:52
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 TrishZwei/82be6c70b2d87189e118 to your computer and use it in GitHub Desktop.
Save TrishZwei/82be6c70b2d87189e118 to your computer and use it in GitHub Desktop.
rectHit: A formulaic hit detect for squares and rectangles
function rectHit(rectone, recttwo){
//console.log('recthit called');
var r1 = $(rectone);
var r2 = $(recttwo);
var r1x = r1.offset().left;
var r1w = r1.width();
var r1y = r1.offset().top;
var r1h = r1.height();
var r2x = r2.offset().left;
var r2w = r2.width();
var r2y = r2.offset().top;
var r2h = r2.height();
if( r1y+r1h < r2y ||
r1y > r2y+r2h ||
r1x > r2x+r2w ||
r1x+r1w < r2x ){
// console.log('recthit-false')
return false;
}else{
//console.log('recthit-true')
return true;
}
}//end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment