/** Quick check whether the given {@link Ray} and {@link BoundingBox} intersect. * * @param ray The ray * @param box The bounding box * @return Whether the ray and the bounding box intersect. */ static public boolean intersectRayBoundsFast (Ray ray, BoundingBox box) { return intersectRayBoundsFast(ray, box.getCenter(tmp1), box.getDimensions(tmp2)); }
/** Intersects the given ray with list of triangles. Returns the nearest intersection point in intersection * * @param ray The ray * @param triangles The triangles, each successive 3 elements are the 3 vertices of a triangle * @param intersection The nearest intersection point (optional) * @return Whether the ray and the triangles intersect. */ public static boolean intersectRayTriangles (Ray ray, List<Vector3> triangles, Vector3 intersection) { float min_dist = Float.MAX_VALUE; boolean hit = false; if (triangles.size() % 3 != 0) throw new RuntimeException("triangle list size is not a multiple of 3"); for (int i = 0; i < triangles.size(); i += 3) { boolean result = intersectRayTriangle(ray, triangles.get(i), triangles.get(i + 1), triangles.get(i + 2), tmp); if (result) { float dist = ray.origin.dst2(tmp); if (dist < min_dist) { min_dist = dist; best.set(tmp); hit = true; } } } if (!hit) return false; else { if (intersection != null) intersection.set(best); return true; } }
/** Returns the distance between the given segment and point. */ public static float distanceSegmentPoint (float startX, float startY, float endX, float endY, float pointX, float pointY) { return nearestSegmentPoint(startX, startY, endX, endY, pointX, pointY, v2tmp).dst(pointX, pointY); }
e.set(floatArray2.get(j), floatArray2.get(j + 1)); if (Intersector.pointLineSide(ep2, ep1, e) > 0) { if (!(Intersector.pointLineSide(ep2, ep1, s) > 0)) { Intersector.intersectLines(s, e, ep1, ep2, ip); if (floatArray.size < 2 || floatArray.get(floatArray.size - 2) != ip.x || floatArray.get(floatArray.size - 1) != ip.y) { } else if (Intersector.pointLineSide(ep2, ep1, s) > 0) { Intersector.intersectLines(s, e, ep1, ep2, ip); floatArray.add(ip.x); floatArray.add(ip.y);
public void processDragPan(Ray dragCurrentRay, Ray lastDragProcessedRay) { followTarget = null; // TODO: // Can probably be optimized, but simply storing worldDragLast.set(worldDragCurrent) // caused jitter for some reason. Intersector.intersectRayPlane(dragCurrentRay, worldGroundPlane, worldDragCurrent); Intersector.intersectRayPlane(lastDragProcessedRay, worldGroundPlane, worldDragLast); tmp1.set(worldDragLast).sub(worldDragCurrent); tmp1.y = 0; ray.origin.set(camera.targetPosition).add(tmp1); ray.direction.set(camera.targetDirection); if (Intersector.intersectRayBoundsFast(ray, worldBoundingBox)) { camera.targetPosition.add(tmp1); worldGroundTarget.add(tmp1); } }
private static void splitEdge (float[] vertices, int s, int e, int stride, Plane plane, float[] split, int offset) { float t = Intersector.intersectLinePlane(vertices[s], vertices[s + 1], vertices[s + 2], vertices[e], vertices[e + 1], vertices[e + 2], plane, intersection); split[offset + 0] = intersection.x; split[offset + 1] = intersection.y; split[offset + 2] = intersection.z; for (int i = 3; i < stride; i++) { float a = vertices[s + i]; float b = vertices[e + i]; split[offset + i] = a + t * (b - a); } }
/** * {@link #intersectSegmentRectangle(float, float, float, float, Rectangle)} */ public static boolean intersectSegmentRectangle (Vector2 start, Vector2 end, Rectangle rectangle) { return intersectSegmentRectangle(start.x, start.y, end.x, end.y, rectangle); }
/** Removes all triangles with a centroid outside the specified hull, which may be concave. Note some triangulations may have * triangles whose centroid is inside the hull but a portion is outside. */ public void trim (ShortArray triangles, float[] points, float[] hull, int offset, int count) { short[] trianglesArray = triangles.items; for (int i = triangles.size - 1; i >= 0; i -= 3) { int p1 = trianglesArray[i - 2] * 2; int p2 = trianglesArray[i - 1] * 2; int p3 = trianglesArray[i] * 2; GeometryUtils.triangleCentroid(points[p1], points[p1 + 1], points[p2], points[p2 + 1], points[p3], points[p3 + 1], centroid); if (!Intersector.isPointInPolygon(hull, offset, count, centroid.x, centroid.y)) { triangles.removeIndex(i); triangles.removeIndex(i - 1); triangles.removeIndex(i - 2); } } } }
/** Determines whether the given rectangle and segment intersect * @param startX x-coordinate start of line segment * @param startY y-coordinate start of line segment * @param endX y-coordinate end of line segment * @param endY y-coordinate end of line segment * @param rectangle rectangle that is being tested for collision * @return whether the rectangle intersects with the line segment */ public static boolean intersectSegmentRectangle (float startX, float startY, float endX, float endY, Rectangle rectangle) { float rectangleEndX = rectangle.x + rectangle.width; float rectangleEndY = rectangle.y + rectangle.height; if (intersectSegments(startX, startY, endX, endY, rectangle.x, rectangle.y, rectangle.x, rectangleEndY, null)) return true; if (intersectSegments(startX, startY, endX, endY, rectangle.x, rectangle.y, rectangleEndX, rectangle.y, null)) return true; if (intersectSegments(startX, startY, endX, endY, rectangleEndX, rectangle.y, rectangleEndX, rectangleEndY, null)) return true; if (intersectSegments(startX, startY, endX, endY, rectangle.x, rectangleEndY, rectangleEndX, rectangleEndY, null)) return true; return rectangle.contains(startX, startY); }
if (MathUtils.isZero(det)) { p.set(t1, t2, t3); if (p.testPoint(ray.origin) == PlaneSide.OnPlane && Intersector.isPointInTriangle(ray.origin, t1, t2, t3)) { if (intersection != null) intersection.set(ray.origin); return true;
tmpPoly.setVertices(verts); Intersector.intersectPolygons(polygon, tmpPoly, intersectionPoly); verts = intersectionPoly.getVertices(); if(verts.length > 0) {
e.set(floatArray2.get(j), floatArray2.get(j + 1)); if (Intersector.pointLineSide(ep2, ep1, e) > 0) { if (!(Intersector.pointLineSide(ep2, ep1, s) > 0)) { Intersector.intersectLines(s, e, ep1, ep2, ip); if (floatArray.size < 2 || floatArray.get(floatArray.size - 2) != ip.x || floatArray.get(floatArray.size - 1) != ip.y) { } else if (Intersector.pointLineSide(ep2, ep1, s) > 0) { Intersector.intersectLines(s, e, ep1, ep2, ip); floatArray.add(ip.x); floatArray.add(ip.y);
private static void splitEdge (float[] vertices, int s, int e, int stride, Plane plane, float[] split, int offset) { float t = Intersector.intersectLinePlane(vertices[s], vertices[s + 1], vertices[s + 2], vertices[e], vertices[e + 1], vertices[e + 2], plane, intersection); split[offset + 0] = intersection.x; split[offset + 1] = intersection.y; split[offset + 2] = intersection.z; for (int i = 3; i < stride; i++) { float a = vertices[s + i]; float b = vertices[e + i]; split[offset + i] = a + t * (b - a); } }
/** * {@link #intersectSegmentRectangle(float, float, float, float, Rectangle)} */ public static boolean intersectSegmentRectangle (Vector2 start, Vector2 end, Rectangle rectangle) { return intersectSegmentRectangle(start.x, start.y, end.x, end.y, rectangle); }
/** Removes all triangles with a centroid outside the specified hull, which may be concave. Note some triangulations may have * triangles whose centroid is inside the hull but a portion is outside. */ public void trim (ShortArray triangles, float[] points, float[] hull, int offset, int count) { short[] trianglesArray = triangles.items; for (int i = triangles.size - 1; i >= 0; i -= 3) { int p1 = trianglesArray[i - 2] * 2; int p2 = trianglesArray[i - 1] * 2; int p3 = trianglesArray[i] * 2; GeometryUtils.triangleCentroid(points[p1], points[p1 + 1], points[p2], points[p2 + 1], points[p3], points[p3 + 1], centroid); if (!Intersector.isPointInPolygon(hull, offset, count, centroid.x, centroid.y)) { triangles.removeIndex(i); triangles.removeIndex(i - 1); triangles.removeIndex(i - 2); } } } }
/** Determines whether the given rectangle and segment intersect * @param startX x-coordinate start of line segment * @param startY y-coordinate start of line segment * @param endX y-coordinate end of line segment * @param endY y-coordinate end of line segment * @param rectangle rectangle that is being tested for collision * @return whether the rectangle intersects with the line segment */ public static boolean intersectSegmentRectangle (float startX, float startY, float endX, float endY, Rectangle rectangle) { float rectangleEndX = rectangle.x + rectangle.width; float rectangleEndY = rectangle.y + rectangle.height; if (intersectSegments(startX, startY, endX, endY, rectangle.x, rectangle.y, rectangle.x, rectangleEndY, null)) return true; if (intersectSegments(startX, startY, endX, endY, rectangle.x, rectangle.y, rectangleEndX, rectangle.y, null)) return true; if (intersectSegments(startX, startY, endX, endY, rectangleEndX, rectangle.y, rectangleEndX, rectangleEndY, null)) return true; if (intersectSegments(startX, startY, endX, endY, rectangle.x, rectangleEndY, rectangleEndX, rectangleEndY, null)) return true; return rectangle.contains(startX, startY); }
if (MathUtils.isZero(det)) { p.set(t1, t2, t3); if (p.testPoint(ray.origin) == PlaneSide.OnPlane && Intersector.isPointInTriangle(ray.origin, t1, t2, t3)) { if (intersection != null) intersection.set(ray.origin); return true;
tmpPoly.setVertices(verts); Intersector.intersectPolygons(polygon, tmpPoly, intersectionPoly); verts = intersectionPoly.getVertices(); if(verts.length > 0) {
/** Intersects the given ray with list of triangles. Returns the nearest intersection point in intersection * * @param ray The ray * @param triangles The triangles, each successive 3 elements are the 3 vertices of a triangle * @param intersection The nearest intersection point (optional) * @return Whether the ray and the triangles intersect. */ public static boolean intersectRayTriangles (Ray ray, List<Vector3> triangles, Vector3 intersection) { float min_dist = Float.MAX_VALUE; boolean hit = false; if (triangles.size() % 3 != 0) throw new RuntimeException("triangle list size is not a multiple of 3"); for (int i = 0; i < triangles.size(); i += 3) { boolean result = intersectRayTriangle(ray, triangles.get(i), triangles.get(i + 1), triangles.get(i + 2), tmp); if (result) { float dist = ray.origin.dst2(tmp); if (dist < min_dist) { min_dist = dist; best.set(tmp); hit = true; } } } if (!hit) return false; else { if (intersection != null) intersection.set(best); return true; } }
/** Returns the distance between the given segment and point. */ public static float distanceSegmentPoint (Vector2 start, Vector2 end, Vector2 point) { return nearestSegmentPoint(start, end, point, v2tmp).dst(point); }