Module org.dyn4j

Class AABB

  • All Implemented Interfaces:
    Translatable

    public class AABB
    extends java.lang.Object
    implements Translatable
    Implementation of an Axis-Align Bounding Box.

    An AABB has minimum and maximum coordinates that define the box.

    An AABB can be unioned or intersected with other AABBs to combine them into another AABB. If an intersection produces no result, a degenerate AABB is returned. A degenerate AABB can be tested by the isDegenerate() methods and is defined as an AABB who's maximum and minimum are equal.

    AABBs can also be tested for overlap and (full) containment using the overlaps(AABB) and contains(AABB) method.

    The expand(double) method can be used to expand the bounds of the AABB by some amount.

    Since:
    3.0.0
    Version:
    3.4.0
    Author:
    William Bittle
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected double maxX
      The maximum extent along the x-axis
      protected double maxY
      The maximum extent along the y-axis
      protected double minX
      The minimum extent along the x-axis
      protected double minY
      The minimum extent along the y-axis
    • Constructor Summary

      Constructors 
      Constructor Description
      AABB​(double radius)
      Full constructor.
      AABB​(double minX, double minY, double maxX, double maxY)
      Full constructor.
      AABB​(AABB aabb)
      Copy constructor.
      AABB​(Vector2 center, double radius)
      Full constructor.
      AABB​(Vector2 min, Vector2 max)
      Full constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean contains​(double x, double y)
      Returns true if the given point's coordinates are contained within this AABB.
      boolean contains​(AABB aabb)
      Returns true if the given AABB is contained within this AABB.
      boolean contains​(Vector2 point)
      Returns true if the given point is contained within this AABB.
      AABB copy()
      Returns a copy of this AABB.
      static AABB createAABBFromPoints​(double point1x, double point1y, double point2x, double point2y)
      Method to create the valid AABB defined by the two points A(point1x, point1y) and B(point2x, point2y).
      static AABB createAABBFromPoints​(Vector2 point1, Vector2 point2)
      Method to create the valid AABB defined by the two points point1 and point2.
      AABB expand​(double expansion)
      Expands this AABB by half the given expansion in each direction and then returns this AABB.
      double getArea()
      Returns the area of this AABB;.
      AABB getExpanded​(double expansion)
      Returns a new AABB of this AABB expanded by half the given expansion in both the x and y directions.
      double getHeight()
      Returns the height of this AABB.
      AABB getIntersection​(AABB aabb)
      Performs the intersection of this AABB and the given AABB returning the result in a new AABB.
      double getMaxX()
      Returns the maximum x extent.
      double getMaxY()
      Returns the maximum y extent.
      double getMinX()
      Returns the minimum x extent.
      double getMinY()
      Returns the minimum y extent.
      double getPerimeter()
      Returns the perimeter of this AABB.
      AABB getTranslated​(Vector2 translation)
      Returns a new AABB of this AABB translated by the given translation amount.
      AABB getUnion​(AABB aabb)
      Performs a union of this AABB and the given AABB returning a new AABB containing the result.
      double getWidth()
      Returns the width of this AABB.
      AABB intersection​(AABB aabb)
      Performs the intersection of this AABB and the given AABB placing the result into this AABB and then returns this AABB.
      boolean isDegenerate()
      Returns true if this AABB is degenerate.
      boolean isDegenerate​(double error)
      Returns true if this AABB is degenerate given the specified error.
      boolean overlaps​(AABB aabb)
      Returns true if the given AABB and this AABB overlap.
      AABB set​(AABB aabb)
      Sets this aabb to the given aabb's value and returns this AABB.
      java.lang.String toString()  
      void translate​(double x, double y)
      Translates the object the given amounts in the respective directions.
      void translate​(Vector2 translation)
      Translates the object along the given vector.
      AABB union​(AABB aabb)
      Performs a union of this AABB and the given AABB placing the result of the union into this AABB and then returns this AABB
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • minX

        protected double minX
        The minimum extent along the x-axis
      • minY

        protected double minY
        The minimum extent along the y-axis
      • maxX

        protected double maxX
        The maximum extent along the x-axis
      • maxY

        protected double maxY
        The maximum extent along the y-axis
    • Constructor Detail

      • AABB

        public AABB​(double minX,
                    double minY,
                    double maxX,
                    double maxY)
        Full constructor.
        Parameters:
        minX - the minimum x extent
        minY - the minimum y extent
        maxX - the maximum x extent
        maxY - the maximum y extent
      • AABB

        public AABB​(Vector2 min,
                    Vector2 max)
        Full constructor.
        Parameters:
        min - the minimum extent
        max - the maximum extent
        Throws:
        java.lang.IllegalArgumentException - if either coordinate of the given min is greater than the given max
      • AABB

        public AABB​(double radius)
        Full constructor.
        Parameters:
        radius - the radius of a circle fitting inside an AABB
        Since:
        3.1.5
      • AABB

        public AABB​(Vector2 center,
                    double radius)
        Full constructor.

        Creates an AABB for a circle with the given center and radius.

        Parameters:
        center - the center of the circle
        radius - the radius of the circle
        Throws:
        java.lang.IllegalArgumentException - if the given radius is less than zero
        Since:
        3.1.5
      • AABB

        public AABB​(AABB aabb)
        Copy constructor.
        Parameters:
        aabb - the AABB to copy
        Since:
        3.1.1
    • Method Detail

      • createAABBFromPoints

        public static AABB createAABBFromPoints​(Vector2 point1,
                                                Vector2 point2)
        Method to create the valid AABB defined by the two points point1 and point2.
        Parameters:
        point1 - the first point
        point2 - the second point
        Returns:
        The one and only one valid AABB formed by point1 and point2
      • createAABBFromPoints

        public static AABB createAABBFromPoints​(double point1x,
                                                double point1y,
                                                double point2x,
                                                double point2y)
        Method to create the valid AABB defined by the two points A(point1x, point1y) and B(point2x, point2y).
        Parameters:
        point1x - The x coordinate of point A
        point1y - The y coordinate of point A
        point2x - The x coordinate of point B
        point2y - The y coordinate of point B
        Returns:
        The one and only one valid AABB formed by A and B
      • copy

        public AABB copy()
        Returns a copy of this AABB.
        Returns:
        AABB
        Since:
        3.4.0
      • set

        public AABB set​(AABB aabb)
        Sets this aabb to the given aabb's value and returns this AABB.
        Parameters:
        aabb - the aabb to copy
        Returns:
        AABB
        Since:
        3.2.5
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • translate

        public void translate​(double x,
                              double y)
        Description copied from interface: Translatable
        Translates the object the given amounts in the respective directions.
        Specified by:
        translate in interface Translatable
        Parameters:
        x - the translation in the x direction
        y - the translation in the y direction
      • translate

        public void translate​(Vector2 translation)
        Description copied from interface: Translatable
        Translates the object along the given vector.
        Specified by:
        translate in interface Translatable
        Parameters:
        translation - the translation along a vector
      • getTranslated

        public AABB getTranslated​(Vector2 translation)
        Returns a new AABB of this AABB translated by the given translation amount.
        Parameters:
        translation - the translation
        Returns:
        AABB
        Since:
        3.1.1
      • getWidth

        public double getWidth()
        Returns the width of this AABB.
        Returns:
        double
        Since:
        3.0.1
      • getHeight

        public double getHeight()
        Returns the height of this AABB.
        Returns:
        double
        Since:
        3.0.1
      • getPerimeter

        public double getPerimeter()
        Returns the perimeter of this AABB.
        Returns:
        double
      • getArea

        public double getArea()
        Returns the area of this AABB;.
        Returns:
        double
      • union

        public AABB union​(AABB aabb)
        Performs a union of this AABB and the given AABB placing the result of the union into this AABB and then returns this AABB
        Parameters:
        aabb - the AABB to union
        Returns:
        AABB
      • getUnion

        public AABB getUnion​(AABB aabb)
        Performs a union of this AABB and the given AABB returning a new AABB containing the result.
        Parameters:
        aabb - the AABB to union
        Returns:
        AABB the resulting union
      • intersection

        public AABB intersection​(AABB aabb)
        Performs the intersection of this AABB and the given AABB placing the result into this AABB and then returns this AABB.

        If the given AABB does not overlap this AABB, this AABB is set to a zero AABB.

        Parameters:
        aabb - the AABB to intersect
        Returns:
        AABB
        Since:
        3.1.1
      • getIntersection

        public AABB getIntersection​(AABB aabb)
        Performs the intersection of this AABB and the given AABB returning the result in a new AABB.

        If the given AABB does not overlap this AABB, a zero AABB is returned.

        Parameters:
        aabb - the AABB to intersect
        Returns:
        AABB
        Since:
        3.1.1
      • expand

        public AABB expand​(double expansion)
        Expands this AABB by half the given expansion in each direction and then returns this AABB.

        The expansion can be negative to shrink the AABB. However, if the expansion is greater than the current width/height, the AABB can become invalid. In this case, the AABB will become a degenerate AABB at the mid point of the min and max for the respective coordinates.

        Parameters:
        expansion - the expansion amount
        Returns:
        AABB
      • getExpanded

        public AABB getExpanded​(double expansion)
        Returns a new AABB of this AABB expanded by half the given expansion in both the x and y directions.

        The expansion can be negative to shrink the AABB. However, if the expansion is greater than the current width/height, the AABB can become invalid. In this case, the AABB will become a degenerate AABB at the mid point of the min and max for the respective coordinates.

        Parameters:
        expansion - the expansion amount
        Returns:
        AABB
        Since:
        3.1.1
      • overlaps

        public boolean overlaps​(AABB aabb)
        Returns true if the given AABB and this AABB overlap.
        Parameters:
        aabb - the AABB to test
        Returns:
        boolean true if the AABBs overlap
      • contains

        public boolean contains​(AABB aabb)
        Returns true if the given AABB is contained within this AABB.
        Parameters:
        aabb - the AABB to test
        Returns:
        boolean
      • contains

        public boolean contains​(Vector2 point)
        Returns true if the given point is contained within this AABB.
        Parameters:
        point - the point to test
        Returns:
        boolean
        Since:
        3.1.1
      • contains

        public boolean contains​(double x,
                                double y)
        Returns true if the given point's coordinates are contained within this AABB.
        Parameters:
        x - the x coordinate of the point
        y - the y coordinate of the point
        Returns:
        boolean
        Since:
        3.1.1
      • isDegenerate

        public boolean isDegenerate()
        Returns true if this AABB is degenerate.

        A degenerate AABB is one where its min and max x or y coordinates are equal.

        Returns:
        boolean
        Since:
        3.1.1
      • isDegenerate

        public boolean isDegenerate​(double error)
        Returns true if this AABB is degenerate given the specified error.

        An AABB is degenerate given some error if max - min <= error for either the x or y coordinate.

        Parameters:
        error - the allowed error
        Returns:
        boolean
        Since:
        3.1.1
        See Also:
        isDegenerate()
      • getMinX

        public double getMinX()
        Returns the minimum x extent.
        Returns:
        double
      • getMaxX

        public double getMaxX()
        Returns the maximum x extent.
        Returns:
        double
      • getMaxY

        public double getMaxY()
        Returns the maximum y extent.
        Returns:
        double
      • getMinY

        public double getMinY()
        Returns the minimum y extent.
        Returns:
        double