Skip to content

Instantly share code, notes, and snippets.

@gblnovaes
Created January 14, 2015 00:22
Show Gist options
  • Save gblnovaes/10aa46327c047539a0d3 to your computer and use it in GitHub Desktop.
Save gblnovaes/10aa46327c047539a0d3 to your computer and use it in GitHub Desktop.
Teste de colisão em JS
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