Skip to content

Instantly share code, notes, and snippets.

@netgfx
Last active March 21, 2018 10:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netgfx/8887917 to your computer and use it in GitHub Desktop.
Save netgfx/8887917 to your computer and use it in GitHub Desktop.
Object collision detection
// jQuery
function doObjectsCollide(a, b) { // a and b are your objects
//console.log(a.offset().top,a.position().top, b.position().top, a.width(),a.height(), b.width(),b.height());
var aTop = a.offset().top;
var aLeft = a.offset().left;
var bTop = b.offset().top;
var bLeft = b.offset().left;
return !(
((aTop + a.height()) < (bTop)) ||
(aTop > (bTop + b.height())) ||
((aLeft + a.width()) < bLeft) ||
(aLeft > (bLeft + b.width()))
);
}
// kineticJS
function doObjectsCollide(a, b) { // a and b are your objects
return !(
((a.getY() + a.getHeight()) < (b.getY())) ||
(a.getY() > (b.y() + b.getHeight())) ||
((a.getX() + a.getWidth()) < b.getX()) ||
(a.getX() > (b.getX() + b.getWidth()))
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment