libgdx API

com.badlogic.gdx.math
Class Intersector

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

public final class Intersector
extends java.lang.Object

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

Author:
badlogicgames@gmail.com, jan.stria

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 getLowestPositiveRoot(float a, float b, float c)
          Returns the lowest positive root of the quadric equation given by a* x * x + b * x + c = 0.
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 intersectLines(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2 intersection)
          Intersects the two lines and returns the intersection point in intersection.
static boolean intersectRayBoundsFast(Ray ray, BoundingBox box)
          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, java.util.List<Vector3> triangles, Vector3 intersection)
          Intersects the given ray with list of triangles.
static boolean intersectRectangles(Rectangle a, Rectangle b)
          Returns whether the two rectangles intersect
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 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(java.util.List<Vector2> polygon, Vector2 point)
          Checks whether the given point is in the polygon.
static boolean isPointInTriangle(Vector3 point, Vector3 t1, Vector3 t2, Vector3 t3)
          Returns whether the given point is inside the triangle.
static void main(java.lang.String[] args)
           
static boolean overlapCircleRectangle(Circle c, Rectangle r)
           
static boolean overlapCircles(Circle c1, Circle c2)
           
static boolean overlapConvexPolygons(float[] verts1, float[] verts2, 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 overlapRectangles(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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Intersector

public Intersector()
Method Detail

getLowestPositiveRoot

public static float getLowestPositiveRoot(float a,
                                          float b,
                                          float c)
Returns the lowest positive root of the quadric equation given by a* x * x + b * x + c = 0. If no solution is given Float.Nan is returned.

Parameters:
a - the first coefficient of the quadric equation
b - the second coefficient of the quadric equation
c - the third coefficient of the quadric equation
Returns:
the lowest positive root or Float.Nan

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

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(java.util.List<Vector2> polygon,
                                       Vector2 point)
Checks whether the given point is in the polygon.

Parameters:
polygon - The polygon vertices
point - The point
Returns:
true if the point is in the polygon

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.


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
center - The center of the sphere
radius - The radius of the sphere
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.

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,
                                            java.util.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.

intersectRectangles

public static boolean intersectRectangles(Rectangle a,
                                          Rectangle b)
Returns whether the two rectangles intersect

Parameters:
a - The first rectangle
b - The second rectangle
Returns:
Whether the two rectangles 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

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

overlapCircles

public static boolean overlapCircles(Circle c1,
                                     Circle c2)

overlapRectangles

public static boolean overlapRectangles(Rectangle r1,
                                        Rectangle r2)

overlapCircleRectangle

public static boolean overlapCircleRectangle(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 (optional).
Returns:
Whether polygons overlap.

overlapConvexPolygons

public static boolean overlapConvexPolygons(float[] verts1,
                                            float[] verts2,
                                            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 (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(java.lang.String[] args)

libgdx API

Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)