Skip to content

Instantly share code, notes, and snippets.

@netgfx
Last active Mar 21, 2018
Embed
What would you like to do?
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