Ejemplo n.º 1
0
 private Vector3f terraionCollision(float x, float y, Ray ray) {
   y /= 2f;
   x /= 2f;
   int xInt = (int) x;
   int yInt = (int) y;
   // p1-----p2
   // || ||
   // || ||
   // p3-----p4
   float p1, p2, p3, p4;
   if (terrainData.length == 1) {
     p1 = p2 = p3 = p4 = terrainData[0] / 32f;
   } else {
     int size = (int) Math.sqrt(terrainData.length);
     try {
       p1 = terrainData[(yInt + (xInt * size))] / 32f;
       p2 = terrainData[((yInt + 1) + (xInt * size))] / 32f;
       p3 = terrainData[((yInt) + ((xInt + 1) * size))] / 32f;
       p4 = terrainData[((yInt + 1) + ((xInt + 1) * size))] / 32f;
     } catch (Exception e) {
       return null;
     }
   }
   Vector3f result = new Vector3f();
   if (p1 >= 0 && p2 >= 0 && p3 >= 0) {
     Triangle tringle1 =
         new Triangle(
             new Vector3f(xInt * 2, yInt * 2, p1),
             new Vector3f(xInt * 2, (yInt + 1) * 2, p2),
             new Vector3f((xInt + 1) * 2, yInt * 2, p3));
     if (ray.intersectWhere(tringle1, result)) {
       return result;
     }
   }
   if (p4 >= 0 && p2 >= 0 && p3 >= 0) {
     Triangle tringle2 =
         new Triangle(
             new Vector3f((xInt + 1) * 2, (yInt + 1) * 2, p4),
             new Vector3f(xInt * 2, (yInt + 1) * 2, p2),
             new Vector3f((xInt + 1) * 2, yInt * 2, p3));
     if (ray.intersectWhere(tringle2, result)) {
       return result;
     }
   }
   return null;
 }