Skip to content

Instantly share code, notes, and snippets.

@thomasdegry
Created October 21, 2013 13:26
Show Gist options
  • Save thomasdegry/7083847 to your computer and use it in GitHub Desktop.
Save thomasdegry/7083847 to your computer and use it in GitHub Desktop.
var CollisionDetection = (function() {
function CollisionDetection() {
}
CollisionDetection.checkCollision = function(shapeA, shapeB) {
var vX = (shapeA.x + (shapeA.width/2)) - (shapeB.x + (shapeB.width / 2));
var vY = (shapeA.y + (shapeA.height/2)) - (shapeB.y + (shapeB.height / 2));
var hWidths = (shapeA.width/2) + (shapeB.width/2);
var hHeights = (shapeA.height/2) + (shapeB.height/2);
var colDir = "";
if(Math.abs(vX) < hWidths && Math.abs(vY) < hHeights) {
//Botsing langs de zijkanten
var oX = hWidths - Math.abs(vX);
var oY = hHeights - Math.abs(vY);
if(oX >= oY) {
//top bottom
if(vY > 0) {
colDir = "t";
} else {
colDir = "b";
}
} else {
//left right
if(vX > 0) {
colDir = "l";
} else {
colDir = "r";
}
}
console.log(colDir);
}
}
return CollisionDetection;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment