com.badlogic.gdx.math
Class Intersector

java.lang.Object
  extended by com.badlogic.gdx.math.Intersector

public final class Intersector
extends Object

Class offering various static methods for intersection testing between different geometric objects.

Author:
[email protected], jan.stria, Nathan Sweet

Nested Class Summary
static class Intersector.MinimumTranslationVector
           
static class Intersector.SplitTriangle
           
 
Constructor Summary
Intersector()
           
 
Method Summary
static float distanceLinePoint(float startX, float startY, float endX, float endY, float pointX, float pointY)
          Returns the distance between the given line and point.
static float distanceLinePoint(Vector2 start, Vector2 end, Vector2 point)
          Returns the distance between the given line segment and point.
static float distanceSegmentPoint(float startX, float startY, float endX, float endY, float pointX, float pointY)
          Returns the distance between the given segment and point.
static float distanceSegmentPoint(Vector2 start, Vector2 end, Vector2 point)
          Returns the distance between the given segment and point.
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.
static boolean intersectLinePolygon(Vector2 p1, Vector2 p2, Polygon polygon)
          Check whether the given line and Polygon intersect.
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.
static boolean intersectLines(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2 intersection)
          Intersects the two lines and returns the intersection point in intersection.
static boolean intersectRayBounds(Ray ray, BoundingBox box, Vector3 intersection)
          Intersects a Ray and a BoundingBox, returning the intersection point in intersection.
static boolean intersectRayBoundsFast(Ray ray, BoundingBox box)
          Quick check whether the given Ray and BoundingBox intersect.
static boolean intersectRayBoundsFast(Ray ray, Vector3 center, Vector3 dimensions)
          Quick check whether the given Ray and BoundingBox intersect.
static boolean intersectRayPlane(Ray ray, Plane plane, Vector3 intersection)
          Intersects a Ray and a Plane.
static boolean intersectRaySphere(Ray ray, Vector3 center, float radius, Vector3 intersection)
          Intersects a Ray and a sphere, returning the intersection point in intersection.
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.
static boolean intersectRayTriangles(Ray ray, float[] vertices, short[] indices, int vertexSize, Vector3 intersection)
          Intersects the given ray with list of triangles.
static boolean intersectRayTriangles(Ray ray, float[] triangles, Vector3 intersection)
          Intersects the given ray with list of triangles.
static boolean intersectRayTriangles(Ray ray, List<Vector3> triangles, Vector3 intersection)
          Intersects the given ray with list of triangles.
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.
static boolean intersectSegmentCircle(Vector2 start, Vector2 end, Vector2 center, float squareRadius)
          Returns whether the given line segment intersects the given circle.
static float intersectSegmentCircleDisplace(Vector2 start, Vector2 end, Vector2 point, float radius, Vector2 displacement)
          Checks whether the line segment and the circle intersect and returns by how much and in what direction the line has to move away from the circle to not intersect.
static boolean intersectSegmentPlane(Vector3 start, Vector3 end, Plane plane, Vector3 intersection)
           
static boolean intersectSegmentPolygon(Vector2 p1, Vector2 p2, Polygon polygon)
          Check whether the given line segment and Polygon intersect.
static boolean intersectSegments(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, Vector2 intersection)
           
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.
static boolean isPointInPolygon(Array<Vector2> polygon, Vector2 point)
          Checks whether the given point is in the polygon.
static boolean isPointInPolygon(float[] polygon, int offset, int count, float x, float y)
          Returns true if the specified point is in the polygon.
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.
static boolean isPointInTriangle(Vector2 p, Vector2 a, Vector2 b, Vector2 c)
          Returns true if the given point is inside the triangle.
static boolean isPointInTriangle(Vector3 point, Vector3 t1, Vector3 t2, Vector3 t3)
          Returns whether the given point is inside the triangle.
static void main(String[] args)
           
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.
static Vector2 nearestSegmentPoint(Vector2 start, Vector2 end, Vector2 point, Vector2 nearest)
          Returns a point on the segment nearest to the specified point.
static boolean overlapConvexPolygons(float[] verts1, float[] verts2, Intersector.MinimumTranslationVector mtv)
           
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.
static boolean overlapConvexPolygons(Polygon p1, Polygon p2)
          Check whether specified convex polygons overlap.
static boolean overlapConvexPolygons(Polygon p1, Polygon p2, Intersector.MinimumTranslationVector mtv)
          Check whether specified convex polygons overlap.
static boolean overlaps(Circle c1, Circle c2)
           
static boolean overlaps(Circle c, Rectangle r)
           
static boolean overlaps(Rectangle r1, Rectangle r2)
           
static int pointLineSide(float linePoint1X, float linePoint1Y, float linePoint2X, float linePoint2Y, float pointX, float pointY)
           
static int pointLineSide(Vector2 linePoint1, Vector2 linePoint2, Vector2 point)
          Determines on which side of the given line the point is.
static void splitTriangle(float[] triangle, Plane plane, Intersector.SplitTriangle split)
          Splits the triangle by the plane.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Intersector

public Intersector()
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.

Parameters:
point - the point
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
point - The point
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.

distanceLinePoint

public static float distanceLinePoint(Vector2 start,
                                      Vector2 end,
                                      Vector2 point)
Returns the distance between the given line segment and point.

Parameters:
start - The line start point
end - The line end point
point - The point
Returns:
The distance between the line segment and the point.

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

intersectSegmentCircleDisplace

public static float intersectSegmentCircleDisplace(Vector2 start,
                                                   Vector2 end,
                                                   Vector2 point,
                                                   float radius,
                                                   Vector2 displacement)
Checks whether the line segment and the circle intersect and returns by how much and in what direction the line has to move away from the circle to not intersect.

Parameters:
start - The line segment starting point
end - The line segment end point
point - The center of the circle
radius - The radius of the circle
displacement - The displacement vector set by the method having unit length
Returns:
The displacement or Float.POSITIVE_INFINITY if no intersection is present

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:
ray - The ray
plane - The plane
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.

Parameters:
x -
y -
z -
x2 -
y2 -
z2 -
plane -

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:
ray - The ray
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.

Parameters:
ray - The ray
box - The box
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.

Parameters:
ray - The ray
box - The bounding box
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:
ray - The ray
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.

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:
ray - The ray
triangles - The triangles, each successive 3 elements from a vertex
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:
ray - The ray
vertices - the vertices
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,
                                            List<Vector3> triangles,
                                            Vector3 intersection)
Intersects the given ray with list of triangles. Returns the nearest intersection point in intersection

Parameters:
ray - The ray
triangles - The triangles
intersection - The nearest intersection point (optional)
Returns:
Whether the ray and the triangles 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
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
polygon - The polygon
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

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 line intersects

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 (optional)
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)

overlaps

public static boolean overlaps(Circle c1,
                               Circle c2)

overlaps

public static boolean overlaps(Rectangle r1,
                               Rectangle r2)

overlaps

public static boolean overlaps(Circle c,
                               Rectangle r)

overlapConvexPolygons

public static boolean overlapConvexPolygons(Polygon p1,
                                            Polygon p2)
Check whether specified convex polygons overlap.

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 specified convex polygons overlap. If they do, optionally obtain a Minimum Translation Vector indicating the minimum magnitude vector required to push the polygons out of the collision.

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,
                                            float[] verts2,
                                            Intersector.MinimumTranslationVector mtv)
See Also:
overlapConvexPolygons(float[], int, int, float[], int, int, MinimumTranslationVector)

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. If they do, optionally obtain a Minimum Translation Vector indicating the minimum magnitude vector required to push the polygons out of the collision.

Parameters:
verts1 - Vertices of the first polygon.
verts2 - Vertices of the second polygon.
mtv - A Minimum Translation Vector to fill in the case of a collision, or null (optional).
Returns:
Whether polygons overlap.

splitTriangle

public static void splitTriangle(float[] triangle,
                                 Plane plane,
                                 Intersector.SplitTriangle split)
Splits the triangle by the plane. The result is stored in the SplitTriangle instance. Depending on where the triangle is relative to the plane, the result can be: The input triangle should have the form: x, y, z, x2, y2, z2, x3, y3, y3. One can add additional attributes per vertex which will be interpolated if split, such as texture coordinates or normals. Note that these additional attributes won't be normalized, as might be necessary in case of normals.

Parameters:
triangle -
plane -
split - output SplitTriangle

main

public static void main(String[] args)


Copyright © 2014. All Rights Reserved.