@Override public int collideWith(Collidable other) { if (other instanceof Ray) { Ray ray = (Ray) other; return collideWithRay(ray); } else if (other instanceof Triangle) { Triangle t = (Triangle) other; if (intersects(t.get1(), t.get2(), t.get3())) { return 1; } return 0; } else if (other instanceof BoundingVolume) { return intersects((BoundingVolume) other) ? 1 : 0; } else { throw new UnsupportedCollisionException("With: " + other.getClass().getSimpleName()); } }
protected boolean checkTriangles(float gridX, float gridY, Ray pick, Vector3f intersection, TerrainPatch patch, Triangle store) { if (!getTriangles(gridX, gridY, patch)) return false; if (pick.intersectWhere(gridTriA, intersection)) { store.set(gridTriA.get1(), gridTriA.get2(), gridTriA.get3()); return true; } else { if (pick.intersectWhere(gridTriB, intersection)) { store.set(gridTriB.get1(), gridTriB.get2(), gridTriB.get3()); return true; } } return false; }
/** * Gets the triangle vertex positions at the given triangle index * and stores them into the {@link Triangle} argument. * Also sets the triangle index to the <code>index</code> argument. * * @param index The index of the triangle. * Should be between 0 and {@link #getTriangleCount()}. * * @param tri The triangle to store the positions in */ public void getTriangle(int index, Triangle tri){ getTriangle(index, tri.get1(), tri.get2(), tri.get3()); tri.setIndex(index); tri.setNormal(null); }
/** * Get the triangle that the point is on. * * @param x coordinate in local space to the geomap * @param z coordinate in local space to the geomap * @return triangle in local space to the geomap */ protected Triangle getTriangleAtPoint(float x, float z) { Triangle[] triangles = getGridTrianglesAtPoint(x, z); if (triangles == null) { //System.out.println("x,z: " + x + "," + z); return null; } Vector2f point = new Vector2f(x, z); Vector2f t1 = new Vector2f(triangles[0].get1().x, triangles[0].get1().z); Vector2f t2 = new Vector2f(triangles[0].get2().x, triangles[0].get2().z); Vector2f t3 = new Vector2f(triangles[0].get3().x, triangles[0].get3().z); if (0 != FastMath.pointInsideTriangle(t1, t2, t3, point)) { return triangles[0]; } t1.set(triangles[1].get1().x, triangles[1].get1().z); t1.set(triangles[1].get2().x, triangles[1].get2().z); t1.set(triangles[1].get3().x, triangles[1].get3().z); if (0 != FastMath.pointInsideTriangle(t1, t2, t3, point)) { return triangles[1]; } return null; }
uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1]; uvCoordinates[pointIndex++] = (triangle.get2().z - min.z) / ext[2]; uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1]; uvCoordinates[pointIndex++] = (triangle.get3().z - min.z) / ext[2]; } else {// discard Z-coordinate uvCoordinates[pointIndex++] = (triangle.get1().x - min.x) / ext[0]; uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0]; uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1]; uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0]; uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1]; uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0]; uvCoordinates[pointIndex++] = (triangle.get2().z - min.z) / ext[2]; uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0]; uvCoordinates[pointIndex++] = (triangle.get3().z - min.z) / ext[2]; } else {// discard Z-coordinate uvCoordinates[pointIndex++] = (triangle.get1().x - min.x) / ext[0]; uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0]; uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1]; uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0]; uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1];
float sgn1 = Math.signum(triangle.get1().x - cx); float sgn2 = Math.signum(triangle.get2().x - cx); float sgn3 = Math.signum(triangle.get3().x - cx); float xSideFactor = sgn1 + sgn2 + sgn3; float ySideFactor = Math.signum(triangle.get1().z - cz) + Math.signum(triangle.get2().z - cz) + Math.signum(triangle.get3().z - cz); if ((xSideFactor > -3 || xSideFactor < 3) && ySideFactor < 0) {// the triangle is on the splitting plane if (sgn1 == 1.0f) {
@Override public int collideWith(Collidable other, CollisionResults results) { if (other instanceof Ray) { Ray ray = (Ray) other; return collideWithRay(ray, results); } else if (other instanceof Triangle) { Triangle t = (Triangle) other; if (intersects(t.get1(), t.get2(), t.get3())) { CollisionResult r = new CollisionResult(); results.addCollision(r); return 1; } return 0; } else if (other instanceof BoundingVolume) { if (intersects((BoundingVolume) other)) { CollisionResult r = new CollisionResult(); results.addCollision(r); return 1; } return 0; } else if (other instanceof Spatial) { return ((Spatial)other).collideWith(this, results); } else { throw new UnsupportedCollisionException("With: " + other.getClass().getSimpleName()); } }
float sgn1 = Math.signum(triangle.get1().x - cx); float sgn2 = Math.signum(triangle.get2().x - cx); float sgn3 = Math.signum(triangle.get3().x - cx); float xSideFactor = sgn1 + sgn2 + sgn3; float ySideFactor = Math.signum(triangle.get1().y - cy) + Math.signum(triangle.get2().y - cy) + Math.signum(triangle.get3().y - cy); if ((xSideFactor > -3 || xSideFactor < 3) && ySideFactor < 0) {// the triangle is on the splitting plane if (sgn1 == 1.0f) {
/** * Get a representation of the underlying triangle at the given point, * translated to world coordinates. * * @param x local x coordinate * @param z local z coordinate * @return a triangle in world space not local space */ protected Triangle getTriangleAtPoint(float x, float z, Vector3f scale, Vector3f translation) { Triangle tri = getTriangleAtPoint(x, z); if (tri != null) { tri.get1().multLocal(scale).addLocal(translation); tri.get2().multLocal(scale).addLocal(translation); tri.get3().multLocal(scale).addLocal(translation); } return tri; }
/** * Get the two triangles that make up the grid section at the specified point, * translated to world coordinates. * * @param x local x coordinate * @param z local z coordinate * @param scale * @param translation * @return two triangles in world space not local space */ protected Triangle[] getGridTrianglesAtPoint(float x, float z, Vector3f scale, Vector3f translation) { Triangle[] tris = getGridTrianglesAtPoint(x, z); if (tris != null) { tris[0].get1().multLocal(scale).addLocal(translation); tris[0].get2().multLocal(scale).addLocal(translation); tris[0].get3().multLocal(scale).addLocal(translation); tris[1].get1().multLocal(scale).addLocal(translation); tris[1].get2().multLocal(scale).addLocal(translation); tris[1].get3().multLocal(scale).addLocal(translation); } return tris; }
Vector3f c = tri.get3().subtract(center, tvars.vect3);
public int collideWith(Collidable other, CollisionResults results) { if (other instanceof Ray) { Ray ray = (Ray) other; return collideWithRay(ray, results); } else if (other instanceof Triangle) { Triangle t = (Triangle) other; if (intersects(t.get1(), t.get2(), t.get3())) { CollisionResult r = new CollisionResult(); results.addCollision(r); return 1; } return 0; } else { throw new UnsupportedCollisionException("With: " + other.getClass().getSimpleName()); } }
private static void addTriangle(Triangle t, Vector3f normal, int index, float[] vertexArray, float[] normalArray, float[] uvArray, short[] indexArray) { addCord(t.get1(), normal, index, vertexArray, normalArray, uvArray, indexArray); addCord(t.get2(), normal, index + 1, vertexArray, normalArray, uvArray, indexArray); addCord(t.get3(), normal, index + 2, vertexArray, normalArray, uvArray, indexArray); }
protected boolean checkTriangles(float gridX, float gridY, Ray pick, Vector3f intersection, TerrainPatch patch, Triangle store) { if (!getTriangles(gridX, gridY, patch)) return false; if (pick.intersectWhere(gridTriA, intersection)) { store.set(gridTriA.get1(), gridTriA.get2(), gridTriA.get3()); return true; } else { if (pick.intersectWhere(gridTriB, intersection)) { store.set(gridTriB.get1(), gridTriB.get2(), gridTriB.get3()); return true; } } return false; }
private static void addTriangle(Triangle t, Vector3f normal, int index, float[] vertexArray, float[] normalArray, float[] uvArray, short[] indexArray) { addCord(t.get1(), normal, index, vertexArray, normalArray, uvArray, indexArray); addCord(t.get2(), normal, index + 1, vertexArray, normalArray, uvArray, indexArray); addCord(t.get3(), normal, index + 2, vertexArray, normalArray, uvArray, indexArray); }
/** * Gets the triangle vertex positions at the given triangle index * and stores them into the {@link Triangle} argument. * Also sets the triangle index to the <code>index</code> argument. * * @param index The index of the triangle. * Should be between 0 and {@link #getTriangleCount()}. * * @param tri The triangle to store the positions in */ public void getTriangle(int index, Triangle tri){ getTriangle(index, tri.get1(), tri.get2(), tri.get3()); tri.setIndex(index); tri.setNormal(null); }
/** * Gets the triangle vertex positions at the given triangle index * and stores them into the {@link Triangle} argument. * Also sets the triangle index to the <code>index</code> argument. * * @param index The index of the triangle. * Should be between 0 and {@link #getTriangleCount()}. * * @param tri The triangle to store the positions in */ public void getTriangle(int index, Triangle tri){ getTriangle(index, tri.get1(), tri.get2(), tri.get3()); tri.setIndex(index); tri.setNormal(null); }