sábado, 23 de abril de 2011

Formula para verificar colisão

Na aula vimos como verificar se NÃO estão colidindo dois retangulos logicos... abaixo a formula para verificar se dois retangulos logicos estão colidindo:

Onde "x" e "y" são as coordenadas do primeiro retangulo, "height1" e "width1" são altura e largura do primeiro retangulo... os mesmos atributos são comparados com o segundo retangulo...



Basicamente a formula verifica se algum dos vertices entrou dentro da area outro retangulo e se um retangulo está dentro do outro...

if(((x < x2) && (x2 < x+width1)
&& ((y < y2) && (y2 < y+height1)))
|| (((x < x2+width2) && (x2+width2 < x+width1))
&& ((y < y2) && (y2 < y+height1)))
|| (((x < x2) && (x2 < x+width1))
&& ((y < y2+height2) && (y2+height2 < y+height1)))
|| (((x < x2+width2) && (x2+width2 < x+width1))
&& ((y < y2+height2) && (y2+height2 < y+height1)))
|| (((x2 < x) && (x < x2+width2))
&& ((y2 < y) && (y < y2+height2)))
|| (((x2 < x+width1) && (x+width1 < x2+width2))
&& ((y2 < y) && (y < y2+height2)))
|| (((x2 < x) && (x < x2+width2))
&& ((y2 < y+height1) && (y+height1 < y2+height2)))
|| (((x2 < x+width1) && (x+width1 < x2+width2))
&& ((y2 < y+height1) && (y+height1 < y2+height2)))){
return true;
}

Nenhum comentário:

Postar um comentário