Class Intersector


  • public final class Intersector
    extends java.lang.Object
    Class offering various static methods for intersection testing between different geometric objects.
    • Method Detail

      • isPointInTriangle

        public static boolean isPointInTriangle​(Vector3 point,
                                                Vector3 t1,
                                                Vector3 t2,
                                                Vector3 t3)
        Returns whether the given point is inside the triangle. This assumes that the point is on the plane of the triangle. No check is performed that this is the case.
        If the Vector3 parameters contain both small and large values, such as one that contains 0.0001 and one that contains 10000000.0, this can fail due to floating-point imprecision.
        Parameters:
        t1 - the first vertex of the triangle
        t2 - the second vertex of the triangle
        t3 - the third vertex of the triangle
        Returns:
        whether the point is in the triangle
      • isPointInTriangle

        public static boolean isPointInTriangle​(Vector2 p,
                                                Vector2 a,
                                                Vector2 b,
                                                Vector2 c)
        Returns true if the given point is inside the triangle.
      • isPointInTriangle

        public static boolean isPointInTriangle​(float px,
                                                float py,
                                                float ax,
                                                float ay,
                                                float bx,
                                                float by,
                                                float cx,
                                                float cy)
        Returns true if the given point is inside the triangle.
      • intersectSegmentPlane

        public static boolean intersectSegmentPlane​(Vector3 start,
                                                    Vector3 end,
                                                    Plane plane,
                                                    Vector3 intersection)
      • pointLineSide

        public static int pointLineSide​(Vector2 linePoint1,
                                        Vector2 linePoint2,
                                        Vector2 point)
        Determines on which side of the given line the point is. Returns 1 if the point is on the left side of the line, 0 if the point is on the line and -1 if the point is on the right side of the line. Left and right are relative to the lines direction which is linePoint1 to linePoint2.
      • pointLineSide

        public static int pointLineSide​(float linePoint1X,
                                        float linePoint1Y,
                                        float linePoint2X,
                                        float linePoint2Y,
                                        float pointX,
                                        float pointY)
      • isPointInPolygon

        public static boolean isPointInPolygon​(Array<Vector2> polygon,
                                               Vector2 point)
        Checks whether the given point is in the polygon.
        Parameters:
        polygon - The polygon vertices passed as an array
        Returns:
        true if the point is in the polygon
      • isPointInPolygon

        public static boolean isPointInPolygon​(float[] polygon,
                                               int offset,
                                               int count,
                                               float x,
                                               float y)
        Returns true if the specified point is in the polygon.
        Parameters:
        offset - Starting polygon index.
        count - Number of array indices to use after offset.
      • intersectPolygons

        public static boolean intersectPolygons​(Polygon p1,
                                                Polygon p2,
                                                Polygon overlap)
        Intersects two convex polygons with clockwise vertices and sets the overlap polygon resulting from the intersection. Follows the Sutherland-Hodgman algorithm.
        Parameters:
        p1 - The polygon that is being clipped
        p2 - The clip polygon
        overlap - The intersection of the two polygons (can be null, if an intersection polygon is not needed)
        Returns:
        Whether the two polygons intersect.
      • intersectPolygons

        public static boolean intersectPolygons​(FloatArray polygon1,
                                                FloatArray polygon2)
        Returns true if the specified poygons intersect.
      • intersectPolygonEdges

        public static boolean intersectPolygonEdges​(FloatArray polygon1,
                                                    FloatArray polygon2)
        Returns true if the lines of the specified poygons intersect.
      • distanceLinePoint

        public static float distanceLinePoint​(float startX,
                                              float startY,
                                              float endX,
                                              float endY,
                                              float pointX,
                                              float pointY)
        Returns the distance between the given line and point. Note the specified line is not a line segment.
      • distanceSegmentPoint

        public static float distanceSegmentPoint​(float startX,
                                                 float startY,
                                                 float endX,
                                                 float endY,
                                                 float pointX,
                                                 float pointY)
        Returns the distance between the given segment and point.
      • distanceSegmentPoint

        public static float distanceSegmentPoint​(Vector2 start,
                                                 Vector2 end,
                                                 Vector2 point)
        Returns the distance between the given segment and point.
      • nearestSegmentPoint

        public static Vector2 nearestSegmentPoint​(Vector2 start,
                                                  Vector2 end,
                                                  Vector2 point,
                                                  Vector2 nearest)
        Returns a point on the segment nearest to the specified point.
      • nearestSegmentPoint

        public static Vector2 nearestSegmentPoint​(float startX,
                                                  float startY,
                                                  float endX,
                                                  float endY,
                                                  float pointX,
                                                  float pointY,
                                                  Vector2 nearest)
        Returns a point on the segment nearest to the specified point.
      • intersectSegmentCircle

        public static boolean intersectSegmentCircle​(Vector2 start,
                                                     Vector2 end,
                                                     Vector2 center,
                                                     float squareRadius)
        Returns whether the given line segment intersects the given circle.
        Parameters:
        start - The start point of the line segment
        end - The end point of the line segment
        center - The center of the circle
        squareRadius - The squared radius of the circle
        Returns:
        Whether the line segment and the circle intersect
      • intersectSegmentCircle

        public static boolean intersectSegmentCircle​(Vector2 start,
                                                     Vector2 end,
                                                     Circle circle,
                                                     Intersector.MinimumTranslationVector mtv)
        Returns whether the given line segment intersects the given circle.
        Parameters:
        start - The start point of the line segment
        end - The end point of the line segment
        circle - The circle
        mtv - A Minimum Translation Vector to fill in the case of a collision, or null (optional).
        Returns:
        Whether the line segment and the circle intersect
      • intersectFrustumBounds

        public static boolean intersectFrustumBounds​(Frustum frustum,
                                                     BoundingBox bounds)
        Returns whether the given Frustum intersects a BoundingBox.
        Parameters:
        frustum - The frustum
        bounds - The bounding box
        Returns:
        Whether the frustum intersects the bounding box
      • intersectFrustumBounds

        public static boolean intersectFrustumBounds​(Frustum frustum,
                                                     OrientedBoundingBox obb)
        Returns whether the given Frustum intersects a OrientedBoundingBox.
        Parameters:
        frustum - The frustum
        obb - The oriented bounding box
        Returns:
        Whether the frustum intersects the oriented bounding box
      • intersectRayRay

        public static float intersectRayRay​(Vector2 start1,
                                            Vector2 direction1,
                                            Vector2 start2,
                                            Vector2 direction2)
        Intersect two 2D Rays and return the scalar parameter of the first ray at the intersection point. You can get the intersection point by: Vector2 point(direction1).scl(scalar).add(start1); For more information, check: http://stackoverflow.com/a/565282/1091440
        Parameters:
        start1 - Where the first ray start
        direction1 - The direction the first ray is pointing
        start2 - Where the second ray start
        direction2 - The direction the second ray is pointing
        Returns:
        scalar parameter on the first ray describing the point where the intersection happens. May be negative. In case the rays are collinear, Float.POSITIVE_INFINITY will be returned.
      • intersectRayPlane

        public static boolean intersectRayPlane​(Ray ray,
                                                Plane plane,
                                                Vector3 intersection)
        Intersects a Ray and a Plane. The intersection point is stored in intersection in case an intersection is present.
        Parameters:
        intersection - The vector the intersection point is written to (optional)
        Returns:
        Whether an intersection is present.
      • intersectLinePlane

        public static float intersectLinePlane​(float x,
                                               float y,
                                               float z,
                                               float x2,
                                               float y2,
                                               float z2,
                                               Plane plane,
                                               Vector3 intersection)
        Intersects a line and a plane. The intersection is returned as the distance from the first point to the plane. In case an intersection happened, the return value is in the range [0,1]. The intersection point can be recovered by point1 + t * (point2 - point1) where t is the return value of this method.
      • intersectPlanes

        public static boolean intersectPlanes​(Plane a,
                                              Plane b,
                                              Plane c,
                                              Vector3 intersection)
        Returns true if the three planes intersect, setting the point of intersection in intersection, if any.
        Parameters:
        intersection - The point where the three planes intersect
      • intersectRayTriangle

        public static boolean intersectRayTriangle​(Ray ray,
                                                   Vector3 t1,
                                                   Vector3 t2,
                                                   Vector3 t3,
                                                   Vector3 intersection)
        Intersect a Ray and a triangle, returning the intersection point in intersection.
        Parameters:
        t1 - The first vertex of the triangle
        t2 - The second vertex of the triangle
        t3 - The third vertex of the triangle
        intersection - The intersection point (optional)
        Returns:
        True in case an intersection is present.
      • intersectRaySphere

        public static boolean intersectRaySphere​(Ray ray,
                                                 Vector3 center,
                                                 float radius,
                                                 Vector3 intersection)
        Intersects a Ray and a sphere, returning the intersection point in intersection.
        Parameters:
        ray - The ray, the direction component must be normalized before calling this method
        center - The center of the sphere
        radius - The radius of the sphere
        intersection - The intersection point (optional, can be null)
        Returns:
        Whether an intersection is present.
      • intersectRayBounds

        public static boolean intersectRayBounds​(Ray ray,
                                                 BoundingBox box,
                                                 Vector3 intersection)
        Intersects a Ray and a BoundingBox, returning the intersection point in intersection. This intersection is defined as the point on the ray closest to the origin which is within the specified bounds.

        The returned intersection (if any) is guaranteed to be within the bounds of the bounding box, but it can occasionally diverge slightly from ray, due to small floating-point errors.

        If the origin of the ray is inside the box, this method returns true and the intersection point is set to the origin of the ray, accordingly to the definition above.

        Parameters:
        intersection - The intersection point (optional)
        Returns:
        Whether an intersection is present.
      • intersectRayBoundsFast

        public static boolean intersectRayBoundsFast​(Ray ray,
                                                     BoundingBox box)
        Quick check whether the given Ray and BoundingBox intersect.
        Returns:
        Whether the ray and the bounding box intersect.
      • intersectRayBoundsFast

        public static boolean intersectRayBoundsFast​(Ray ray,
                                                     Vector3 center,
                                                     Vector3 dimensions)
        Quick check whether the given Ray and BoundingBox intersect.
        Parameters:
        center - The center of the bounding box
        dimensions - The dimensions (width, height and depth) of the bounding box
        Returns:
        Whether the ray and the bounding box intersect.
      • intersectRayOrientedBoundsFast

        public static boolean intersectRayOrientedBoundsFast​(Ray ray,
                                                             OrientedBoundingBox obb)
        Check whether the given Ray and OrientedBoundingBox intersect.
        Returns:
        Whether the ray and the oriented bounding box intersect.
      • intersectRayOrientedBoundsFast

        public static boolean intersectRayOrientedBoundsFast​(Ray ray,
                                                             BoundingBox bounds,
                                                             Matrix4 transform)
        Check whether the given Ray and Oriented BoundingBox intersect.
        Parameters:
        transform - - the BoundingBox transformation
        Returns:
        Whether the ray and the oriented bounding box intersect.
      • intersectRayOrientedBounds

        public static boolean intersectRayOrientedBounds​(Ray ray,
                                                         OrientedBoundingBox obb,
                                                         Vector3 intersection)
        Check whether the given Ray and OrientedBoundingBox intersect.
        Parameters:
        intersection - The intersection point (optional)
        Returns:
        Whether an intersection is present.
      • intersectRayOrientedBounds

        public static boolean intersectRayOrientedBounds​(Ray ray,
                                                         BoundingBox bounds,
                                                         Matrix4 transform,
                                                         Vector3 intersection)
        Check whether the given Ray and OrientedBoundingBox intersect. Based on code at: https://github.com/opengl-tutorials/ogl/blob/master/misc05_picking/misc05_picking_custom.cpp#L83
        Parameters:
        intersection - The intersection point (optional)
        Returns:
        Whether an intersection is present.
      • intersectRayTriangles

        public static boolean intersectRayTriangles​(Ray ray,
                                                    float[] triangles,
                                                    Vector3 intersection)
        Intersects the given ray with list of triangles. Returns the nearest intersection point in intersection
        Parameters:
        triangles - The triangles, each successive 9 elements are the 3 vertices of a triangle, a vertex is made of 3 successive floats (XYZ)
        intersection - The nearest intersection point (optional)
        Returns:
        Whether the ray and the triangles intersect.
      • intersectRayTriangles

        public static boolean intersectRayTriangles​(Ray ray,
                                                    float[] vertices,
                                                    short[] indices,
                                                    int vertexSize,
                                                    Vector3 intersection)
        Intersects the given ray with list of triangles. Returns the nearest intersection point in intersection
        Parameters:
        indices - the indices, each successive 3 shorts index the 3 vertices of a triangle
        vertexSize - the size of a vertex in floats
        intersection - The nearest intersection point (optional)
        Returns:
        Whether the ray and the triangles intersect.
      • intersectRayTriangles

        public static boolean intersectRayTriangles​(Ray ray,
                                                    java.util.List<Vector3> triangles,
                                                    Vector3 intersection)
        Intersects the given ray with list of triangles. Returns the nearest intersection point in intersection
        Parameters:
        triangles - The triangles, each successive 3 elements are the 3 vertices of a triangle
        intersection - The nearest intersection point (optional)
        Returns:
        Whether the ray and the triangles intersect.
      • intersectBoundsPlaneFast

        public static boolean intersectBoundsPlaneFast​(BoundingBox box,
                                                       Plane plane)
        Quick check whether the given BoundingBox and Plane intersect.
        Returns:
        Whether the bounding box and the plane intersect.
      • intersectBoundsPlaneFast

        public static boolean intersectBoundsPlaneFast​(Vector3 center,
                                                       Vector3 halfDimensions,
                                                       Vector3 normal,
                                                       float distance)
        Quick check whether the given bounding box and a plane intersect. Code adapted from Christer Ericson's Real Time Collision
        Parameters:
        center - The center of the bounding box
        halfDimensions - Half of the dimensions (width, height and depth) of the bounding box
        normal - The normal of the plane
        distance - The distance of the plane
        Returns:
        Whether the bounding box and the plane intersect.
      • intersectLines

        public static boolean intersectLines​(Vector2 p1,
                                             Vector2 p2,
                                             Vector2 p3,
                                             Vector2 p4,
                                             Vector2 intersection)
        Intersects the two lines and returns the intersection point in intersection.
        Parameters:
        p1 - The first point of the first line
        p2 - The second point of the first line
        p3 - The first point of the second line
        p4 - The second point of the second line
        intersection - The intersection point. May be null.
        Returns:
        Whether the two lines intersect
      • intersectLines

        public static boolean intersectLines​(float x1,
                                             float y1,
                                             float x2,
                                             float y2,
                                             float x3,
                                             float y3,
                                             float x4,
                                             float y4,
                                             Vector2 intersection)
        Intersects the two lines and returns the intersection point in intersection.
        Parameters:
        intersection - The intersection point, or null.
        Returns:
        Whether the two lines intersect
      • intersectLinePolygon

        public static boolean intersectLinePolygon​(Vector2 p1,
                                                   Vector2 p2,
                                                   Polygon polygon)
        Check whether the given line and Polygon intersect.
        Parameters:
        p1 - The first point of the line
        p2 - The second point of the line
        Returns:
        Whether polygon and line intersects
      • intersectRectangles

        public static boolean intersectRectangles​(Rectangle rectangle1,
                                                  Rectangle rectangle2,
                                                  Rectangle intersection)
        Determines whether the given rectangles intersect and, if they do, sets the supplied intersection rectangle to the area of overlap.
        Returns:
        Whether the rectangles intersect
      • intersectSegmentRectangle

        public static boolean intersectSegmentRectangle​(float startX,
                                                        float startY,
                                                        float endX,
                                                        float endY,
                                                        Rectangle rectangle)
        Determines whether the given rectangle and segment intersect
        Parameters:
        startX - x-coordinate start of line segment
        startY - y-coordinate start of line segment
        endX - y-coordinate end of line segment
        endY - y-coordinate end of line segment
        rectangle - rectangle that is being tested for collision
        Returns:
        whether the rectangle intersects with the line segment
      • intersectSegmentPolygon

        public static boolean intersectSegmentPolygon​(Vector2 p1,
                                                      Vector2 p2,
                                                      Polygon polygon)
        Check whether the given line segment and Polygon intersect.
        Parameters:
        p1 - The first point of the segment
        p2 - The second point of the segment
        Returns:
        Whether polygon and segment intersect
      • intersectSegments

        public static boolean intersectSegments​(Vector2 p1,
                                                Vector2 p2,
                                                Vector2 p3,
                                                Vector2 p4,
                                                Vector2 intersection)
        Intersects the two line segments and returns the intersection point in intersection.
        Parameters:
        p1 - The first point of the first line segment
        p2 - The second point of the first line segment
        p3 - The first point of the second line segment
        p4 - The second point of the second line segment
        intersection - The intersection point. May be null.
        Returns:
        Whether the two line segments intersect
      • intersectSegments

        public static boolean intersectSegments​(float x1,
                                                float y1,
                                                float x2,
                                                float y2,
                                                float x3,
                                                float y3,
                                                float x4,
                                                float y4,
                                                Vector2 intersection)
        Parameters:
        intersection - May be null.
      • overlaps

        public static boolean overlaps​(Circle c1,
                                       Circle c2)
      • overlapConvexPolygons

        public static boolean overlapConvexPolygons​(Polygon p1,
                                                    Polygon p2)
        Check whether specified convex polygons overlap (clockwise or counter-clockwise wound doesn't matter).
        Parameters:
        p1 - The first polygon.
        p2 - The second polygon.
        Returns:
        Whether polygons overlap.
      • overlapConvexPolygons

        public static boolean overlapConvexPolygons​(Polygon p1,
                                                    Polygon p2,
                                                    Intersector.MinimumTranslationVector mtv)
        Check whether convex polygons overlap (clockwise or counter-clockwise wound doesn't matter). If they do, optionally obtain a Minimum Translation Vector indicating the minimum magnitude vector required to push the polygon p1 out of collision with polygon p2.
        Parameters:
        p1 - The first polygon.
        p2 - The second polygon.
        mtv - A Minimum Translation Vector to fill in the case of a collision, or null (optional).
        Returns:
        Whether polygons overlap.
      • overlapConvexPolygons

        public static boolean overlapConvexPolygons​(float[] verts1,
                                                    int offset1,
                                                    int count1,
                                                    float[] verts2,
                                                    int offset2,
                                                    int count2,
                                                    Intersector.MinimumTranslationVector mtv)
        Check whether polygons defined by the given vertex arrays overlap (clockwise or counter-clockwise wound doesn't matter). If they do, optionally obtain a Minimum Translation Vector indicating the minimum magnitude vector required to push the polygon defined by verts1 out of the collision with the polygon defined by verts2.
        Parameters:
        verts1 - Vertices of the first polygon.
        offset1 - the offset of the verts1 array
        count1 - the amount that is added to the offset1
        verts2 - Vertices of the second polygon.
        offset2 - the offset of the verts2 array
        count2 - the amount that is added to the offset2
        mtv - A Minimum Translation Vector to fill in the case of a collision, or null (optional).
        Returns:
        Whether polygons overlap.
      • hasOverlap

        public static boolean hasOverlap​(Vector3[] axes,
                                         Vector3[] aVertices,
                                         Vector3[] bVertices)
        Returns whether two geometries (defined as an array of vertices) have at least one point of intersection using SAT (separating axis theorem)
        Parameters:
        axes - - Axes to be tested
        aVertices - - Vertices from geometry A
        bVertices - - Vertices from geometry B
        Returns:
        if geometries are intersecting