Created
October 21, 2013 13:26
-
-
Save thomasdegry/7083847 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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