Ver Mensaje Individual
  #3 (permalink)  
Antiguo 09/04/2012, 21:31
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 22 años, 5 meses
Puntos: 834
Respuesta: Colision en arco

Fijate si te sirve:
Código PHP:
<!DOCTYPE html>
<
html>
<
head>
<
title></title>
<
style>
*{ 
margin:0; padding:0;}
canvas{border:1px dashed #CCC}
</style>
</
head>
<
body>
<
canvas id="c" width="500" height="500"></canvas>
<
script>
var 
ctx,x=10,y=10,dx=3,dy=2,w=500,h=500,o={x:250,y:250,r:100},b={r:10};
function 
init(){
    
ctx=document.getElementById('c').getContext('2d')
    
    
setInterval(draw, 10);
}
function 
draw(){
    
ctx.clearRect(0,0,w,h);
    
ctx.fillStyle = "blue";
    
ctx.beginPath();
    
ctx.arc(o.x, o.y, o.r, 0, Math.PI*2, true); 
    
ctx.closePath();
    
ctx.fill();
    
ctx.fillStyle = "red";
    
ctx.beginPath();
    
ctx.arc(x, y, b.r, 0, Math.PI*2, true); 
    
ctx.closePath();
    
ctx.fill();
    if (
x + dx > w || x + dx < 0)
        
dx = -dx;
     if (
y + dy > h || y + dy < 0)
        
dy = -dy;
    var 
collisionX = (o.x - x);
    var 
collisionY = (o.y - y);
    var 
distanceSquared = (collisionX * collisionX) + (collisionY * collisionY);
    var 
sumRadius = (b.r + o.r);
    var 
sumRadiusSquared = sumRadius * sumRadius;
    if(
distanceSquared <= sumRadiusSquared){
            var 
collisionVectorLength = Math.sqrt(distanceSquared);
            if(
collisionVectorLength == 0) collisionVectorLength = 1;
            var 
collisionNormalX = collisionX / collisionVectorLength;
            var 
collisionNormalY = collisionY / collisionVectorLength;
            var 
Acvx = (dx * collisionNormalX) + (dy * collisionNormalY);
            var 
Acvy = (dx * collisionNormalX) - (dy * collisionNormalY);
            var 
Afvx = -2;
            
dx = (Afvx * collisionNormalX) - (Acvy * collisionNormalY);
            
dy = (collisionNormalX * Acvy) + (Afvx * collisionNormalY);
    }
    
x += dx;
      
y += dy;
}
init();

</script>
</body>
</html> 
Si lo razónás un poco vas a ver que de lo único que se trata es de usar el famoso Teorema de Pitágoras. Básicamente, asumiendo que ambos son círculos, te fijás en 2 puntos: el centro del obstáculo y el centro del móvil. Si la distancia entre ellos es menor o igual a la suma de los 2 radios, quiere decir que chocaron, si es mayor, no chocaron. Y es la distancia entre ellos la que calculás con el teorema de Pitágoras.

Última edición por Panino5001; 09/04/2012 a las 21:49