Foros del Web » Programación para mayores de 30 ;) » Java »

problemas para pasar esto a java

Estas en el tema de problemas para pasar esto a java en el foro de Java en Foros del Web. buenas, llevo todo el día buscando formulas para que una pelota colisione con un triangulo y he encontrado esta formula, llevo todo el día tratando ...
  #1 (permalink)  
Antiguo 19/12/2014, 09:29
dr4
 
Fecha de Ingreso: noviembre-2014
Mensajes: 28
Antigüedad: 9 años, 5 meses
Puntos: 0
problemas para pasar esto a java

buenas, llevo todo el día buscando formulas para que una pelota colisione con un triangulo y he encontrado esta formula, llevo todo el día tratando de que funcione pero no hay manera(el true me salta aunque la pelota este lejísimos del triangulo) pondré la formula original y como la puse yo en java con la esperanza de que me digáis que he hecho mal:
-----------------------------------------------------------------------------------------
La original
CheckIntersection
//xc, yc - are the circle's center coords
//x1,y1 - x2,y2
if(x1 - x2 == 0) //if the incline is 0 we need diff equation
for i = y1 to y2
if(sqrt((xc - x1)^2 + (yc - i)^2) <= R) return true;
for i = x1 to x2
curry = ((y1 - y2)/(x1 - x2))*(i - x1) + y1
if(sqrt((xc - i)^2 + (yc - curry)^2) <= R) return true;

//x2,y2 - x3,y3
if(x2 - x3 == 0) //incline 0
for i = y1 to y2
if(sqrt((xc - x2)^2 + (yc - i)^2) <= R) return true;
for i = x2 to x3
curry = ((y2 - y3)/(x2 - x3))*(i - x2) + y2
if(sqrt((xc - i)^2 + (yc - curry)^2) <= R) return true;

//x3,y3 - x1,y1
if(x3 - x1 == 0) //incline 0
for i = y1 to y2
if(sqrt((xc - x1)^2 + (yc - i)^2) <= R) return true;
for i = x3 to x1
curry = ((y3 - y1)/(x3 - x1))*(i - x3) + y3
if(sqrt((xc - i)^2 + (yc - curry)^2) <= R) return true;

return false; //if no such point found return false


-------------------------------------------------------------------------------------------------
como la puse en java

Código:
public boolean triangulocho(Pelota redonda,Triangulo tria){
        
//xc, yc - are the circle's center coords
//x1,y1 - x2,y2
if(tria.x1 - tria.x2 == 0) {  //if the incline is 0 we need diff equation
   for (int i=tria.y1;i<tria.y2;i++){ 
      if (sqrt((redonda.centrox - tria.x1)^2 + (redonda.centroy - i)^2) <= redonda.radio){return true;}}
}
      
for (int i=tria.x1;i<tria.x2;i++){
   int curry = ((tria.y1 - tria.y2)/(tria.x1 - tria.x2))*(i - tria.x1) + tria.y1;
   if(sqrt((redonda.centrox - i)^2 + (redonda.centroy - curry)^2) <= redonda.radio) {return true;}
}
//x2,y2 - x3,y3
if(tria.x2 - tria.x3 == 0) {  //incline 0
   for(int i=tria.y1;i<tria.y2;i++){
      if(sqrt((redonda.centrox - tria.x2)^2 + (redonda.centroy - i)^2) <= redonda.radio) {return true;}
   }}
for (int i=tria.x2;i<tria.x3;i++){
   int curry = ((tria.y2 - tria.y3)/(tria.x2 - tria.x3))*(i - tria.x2) + tria.y2;
   if(sqrt((redonda.centrox - i)^2 + (redonda.centroy - curry)^2) <= redonda.radio) {return true;}
}
//x3,y3 - x1,y1
if(tria.x3 - tria.x1 == 0) { //incline 0
   for (int i=tria.y1;i<tria.y2;i++){
      if(sqrt((redonda.centrox - tria.x1)^2 + (redonda.centroy - i)^2) <= redonda.radio) {return true;}
   }
for (int i=tria.x3;i<tria.x1;i++) {
   int curry = ((tria.y3 - tria.y1)/(tria.x3 - tria.x1))*(i - tria.x3) + tria.y3;
   if(sqrt((redonda.centrox - i)^2 + (redonda.centroy- curry)^2) <= redonda.radio) {return true;}
}}
return false;   //if no such point found return false

    }
  #2 (permalink)  
Antiguo 19/12/2014, 09:59
Avatar de chuidiang
Colaborador
 
Fecha de Ingreso: octubre-2004
Mensajes: 3.774
Antigüedad: 19 años, 6 meses
Puntos: 454
Respuesta: problemas para pasar esto a java

El operador ^ en java es un OR exclusivo, no un "elevado a" . Usa Math.pow(valor, potencia) en su lugar

sqrt() tal cual lo tienes no existe en java, es Math.sqrt(), supongo que te has hecho un método con ese nombre....

Se bueno.
__________________
Apuntes Java
Wiki de Programación
  #3 (permalink)  
Antiguo 19/12/2014, 12:23
dr4
 
Fecha de Ingreso: noviembre-2014
Mensajes: 28
Antigüedad: 9 años, 5 meses
Puntos: 0
Respuesta: problemas para pasar esto a java

muchas gracias por contestar, tienes razón, ni me fijé, y es tal como dices, aunque no sé si seguiré teniendo algo mal, a la hora de chocar contra la hipotenusa la pelota la ignora totalmente y no actúa hasta alcanzar la base del triangulo (salta el segundo for) los otros true no salen nunca :S ,aunque al menos ahora estoy teniendo una respuesta en alguna parte.

Última edición por dr4; 19/12/2014 a las 12:30

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:02.