Last active
March 21, 2018 10:05
-
-
Save netgfx/8887917 to your computer and use it in GitHub Desktop.
Object collision detection
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
// 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