float prev_x = box.getX();
float prev_y = box.getY();
box.update(dt);
float box_x=box.getX();
float box_y=box.getY();
float dir_x = box_x-prev_x;
float dir_y = box_y-prev_y;
float min_x= box_x;
float min_y= box_y - box.getHeight();
float max_x = box_x+box.getWidth();
float max_y = box_y;
float tInter = Tools.POSITIVE_INFINITY;
float normal_x=0;
float normal_y=1;
float[] normal_temp=new float[2];
// AABB con cada vertice del piso.
// Tiro un rayo en la dirección contraria al movimiento de la caja
// con origen en los vertices de cada linea y lo intersecto con la caja
for(int i=0; i<lineas.length; i+=2){
float t = Tools.int_AABB_Rayo(min_x , min_y,
max_x , max_y,
lineas[i],lineas[i+1],
-dir_x , -dir_y ,
normal_temp );
if( t!=Tools.POSITIVE_INFINITY && t < tInter ){
tInter = t;
normal_x = normal_temp[0];
normal_y = normal_temp[1];
}
}
// Cada vertice del AABB con las líneas del piso.
// Tiro un rayo en la direccion de movimiento de la caja
// con origen en cada vertice de la caja y lo intersecto con
// las lineas del piso
for(int i=0,c=0; i<lineas.length; i+=4,c++){
float t;
// Vertice top,left
t = Tools.int_Rayo_Linea_Eberly(lineas[i],lineas[i+1],
lineas[i+2],lineas[i+3],
dir_x,dir_y,
box_x,box_y);
if( t!=Tools.POSITIVE_INFINITY && t < tInter){
tInter = t;
normal_x = normales[c][0];
normal_y = normales[c][1];
}
// vertice top,right
t = Tools.int_Rayo_Linea_Eberly(lineas[i],lineas[i+1],
lineas[i+2],lineas[i+3],
dir_x,dir_y,
box_x+box.getWidth(),box_y);
if( t!=Tools.POSITIVE_INFINITY && t < tInter){
tInter = t;
normal_x = normales[c][0];
normal_y = normales[c][1];
}
// vertice bottom,right
t = Tools.int_Rayo_Linea_Eberly(lineas[i],lineas[i+1],
lineas[i+2],lineas[i+3],
dir_x,dir_y,
box_x+box.getWidth(),box_y-box.getHeight());
if( t!=Tools.POSITIVE_INFINITY && t < tInter){
tInter = t;
normal_x = normales[c][0];
normal_y = normales[c][1];
}
// vertice bottom,left
t = Tools.int_Rayo_Linea_Eberly(lineas[i],lineas[i+1],
lineas[i+2],lineas[i+3],
dir_x,dir_y,
box_x,box_y-box.getHeight());
if( t!=Tools.POSITIVE_INFINITY && t < tInter){
tInter = t;
normal_x = normales[c][0];
normal_y = normales[c][1];
}
}
// este es el "sliding". Aquí estoy teniedo problemas :P
if( tInter != Tools.POSITIVE_INFINITY ){
float Q_x = prev_x + dir_x*tInter;
float Q_y = prev_y + dir_y*tInter;
float dot = ((box_x-Q_x)*normal_x) + ((box_y-Q_y)*normal_y);
int final_x = (int)(box_x - normal_x*dot);
int final_y = (int)(box_y - normal_y*dot);
box.setX(final_x);
box.setY(final_y);
//quizas aqui correr de nuevo el algoritmo
return;
}