Created
January 14, 2015 00:22
-
-
Save gblnovaes/10aa46327c047539a0d3 to your computer and use it in GitHub Desktop.
Teste de colisão em JS
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
function colCheck(shapeA,shapeB){ | |
var vX = (shapeA.x + (shapeA.width/2)) - (shapeB.x + (shapeB.width/2)), | |
vY = (shapeA.y + (shapeA.height/2)) - (shapeB.y + (shapeB.height/2)), | |
hWdiths = (shapeA.width/2) + (shapeB.width/2), | |
hHeights =(shapeA.height/2) + (shapeB.height/2), | |
colDir = null; | |
if(Math.abs(vX) < hWdiths && Math.abs(vY) < hHeights){ | |
var oX = hWdiths - Math.abs(vX), | |
oY = hHeights - Math.abs(vY); | |
if(oX >= oY){ | |
if(vY > 0){ | |
colDir = "t"; | |
shapeA.y += oY; | |
}else{ | |
colDir = "b"; | |
shapeA.y -= oY; | |
} | |
}else { | |
if(vX > 0){ | |
colDir = "l"; | |
shapeA.x += oX; | |
}else{ | |
colDir = "r"; | |
shapeA.x -= oX; | |
} | |
} | |
} | |
return colDir; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment