Module org.dyn4j

Class DynamicAABBTree<T>

  • Type Parameters:
    T - the object type
    All Implemented Interfaces:
    BroadphaseDetector<T>, Shiftable

    public final class DynamicAABBTree<T>
    extends AbstractBroadphaseDetector<T>
    Implementation of a self-balancing axis-aligned bounding box tree broad-phase collision detection algorithm.

    This class uses a self-balancing binary tree to store the AABBs. The AABBs are sorted using the perimeter. The perimeter hueristic is better than area for 2D because axis aligned segments would have zero area.

    Since:
    3.0.0
    Version:
    4.1.0
    Author:
    William Bittle
    • Constructor Detail

      • DynamicAABBTree

        public DynamicAABBTree​(BroadphaseFilter<T> broadphaseFilter,
                               AABBProducer<T> aabbProducer,
                               AABBExpansionMethod<T> aabbExpansionMethod)
        Default constructor.
        Parameters:
        broadphaseFilter - the broadphase filter
        aabbProducer - the AABB producer
        aabbExpansionMethod - the AABB expansion method
        Throws:
        NullPointerException - if broadphaseFilter, aabbProducer or aabbExpansionMethod are null
      • DynamicAABBTree

        public DynamicAABBTree​(BroadphaseFilter<T> broadphaseFilter,
                               AABBProducer<T> aabbProducer,
                               AABBExpansionMethod<T> aabbExpansionMethod,
                               int initialCapacity)
        Optional constructor.

        Allows fine tuning of the initial capacity of local storage for faster running times.

        Parameters:
        broadphaseFilter - the broadphase filter
        aabbProducer - the AABB producer; cannot be null
        aabbExpansionMethod - the AABB expansion method; cannot be null
        initialCapacity - the initial capacity of local storage
        Throws:
        NullPointerException - if broadphaseFilter, aabbProducer or aabbExpansionMethod are null
        IllegalArgumentException - if initialCapacity is less than zero
        Since:
        3.1.1
    • Method Detail

      • add

        public void add​(T obj)
        Description copied from interface: BroadphaseDetector
        Adds a new object to this broad-phase.

        If the given object has already been added, the object is updated (BroadphaseDetector.update(Object)).

        Parameters:
        obj - the object to add
      • remove

        public boolean remove​(T obj)
        Description copied from interface: BroadphaseDetector
        Removes the given object from this broad-phase.
        Parameters:
        obj - the object to remove
        Returns:
        boolean true if the object was removed
      • update

        public void update​(T obj)
        Description copied from interface: BroadphaseDetector
        Updates the broad-phase representation of the given object.

        This should be called when the object's location, rotation, or shape has changed.

        Parameters:
        obj - the object
      • isUpdated

        public boolean isUpdated​(T obj)
        Description copied from interface: BroadphaseDetector
        Returns true if the given object is marked as updated in this broad-phase.

        If BroadphaseDetector.isUpdateTrackingEnabled() is false, this method will always return true.

        Returns false if the given object is not part of this broad-phase.

        Parameters:
        obj - the object
        Returns:
        boolean
      • setUpdated

        public void setUpdated​(T obj)
        Description copied from interface: BroadphaseDetector
        This method forces this broad-phase to include the given object in the updated list to ensure they are checked in the updated-only detection routine.

        The BroadphaseDetector.update(Object) method will only mark an object as updated if it's fixtures have moved enough to change the internally stored AABB.

        Parameters:
        obj - the object
      • setUpdateTrackingEnabled

        public void setUpdateTrackingEnabled​(boolean flag)
        Description copied from interface: BroadphaseDetector
        Sets the update tracking to the given flag.

        Tracking updates to the broad-phase can have huge performance gains if the majority of objects are stationary or moving slowly enough.

        Disabling this feature will clear the set of tracked updates (the updates themselves are not cleared). In addition, when enabling this feature (after disabling it), the user is expected to re-update all items in the broad-phase manually to ensure the updates set is non-empty. Typically this will self heal in the next iteration though.

        Specified by:
        setUpdateTrackingEnabled in interface BroadphaseDetector<T>
        Overrides:
        setUpdateTrackingEnabled in class AbstractBroadphaseDetector<T>
        Parameters:
        flag - true to turn on update tracking
        See Also:
        BroadphaseDetector.isUpdateTrackingSupported()
      • clearUpdates

        public void clearUpdates()
        Description copied from interface: BroadphaseDetector
        Clears internal state that tracks what objects have been updated.

        Typically this method would be called from a pipeline after a broad-phase collision detection method has been called to clear the state before starting to track new updates.

      • contains

        public boolean contains​(T obj)
        Description copied from interface: BroadphaseDetector
        Returns true if the given object exists in this broad-phase.
        Parameters:
        obj - the object
        Returns:
        boolean
      • clear

        public void clear()
        Description copied from interface: BroadphaseDetector
        Clear all the internal state of this broad-phase.
      • size

        public int size()
        Description copied from interface: BroadphaseDetector
        Returns the number of objects managed in this broad-phase.
        Returns:
        int
      • detectIterator

        public Iterator<CollisionPair<T>> detectIterator​(boolean forceFullDetection)
        Description copied from interface: BroadphaseDetector
        Performs collision detection on all objects that have been added to this BroadphaseDetector and returns an iterator of potential collision pairs (i.e. those pairs whose AABBs overlap).

        The pairs returned from this method will depend on the value of the BroadphaseDetector.isUpdateTrackingEnabled() flag. When false, the returned iterator will report all pairs, every invocation. When true, the returned iterator will only contain pairs whose objects moved significantly enough to generate new AABBs. As a result, this mode would not report those pairs who ARE NOT overlapping, nor would it report those pairs who ARE overlapping, but the objects didn't move enough.

        Use the forceFullDetection parameter to override this behavior for a single call.

        NOTE: This method returns CollisionPairs that are mutable internally. If you need to store the pairs outside of the iteration, be sure to call the Copyable.copy() method to create a copy of the pair data.

        Parameters:
        forceFullDetection - true if a full detection should be performed
        Returns:
        Iterator<CollisionPair>
      • detectIterator

        public Iterator<T> detectIterator​(AABB aabb)
        Description copied from interface: BroadphaseDetector
        Performs a broad-phase collision test using the given AABB and returns the items that overlap.
        Parameters:
        aabb - the AABB to test
        Returns:
        Iterator<T>
      • raycastIterator

        public Iterator<T> raycastIterator​(Ray ray,
                                           double length)
        Description copied from interface: BroadphaseDetector
        Performs a raycast over all the objects in the broad-phase and returns the items that intersect.
        Parameters:
        ray - the Ray
        length - the length of the ray; 0.0 for infinite length
        Returns:
        Iterator<T>
      • shift

        public void shift​(Vector2 shift)
        Description copied from interface: Shiftable
        Translates the object to match the given coordinate shift.
        Parameters:
        shift - the amount to shift along the x and y axes
      • isUpdateTrackingSupported

        public boolean isUpdateTrackingSupported()
        Description copied from interface: BroadphaseDetector
        Returns whether this particular BroadphaseDetector supports update tracking.
        Returns:
        boolean
      • getHeight

        public int getHeight()
        Returns the height of the tree.
        Returns:
        int
      • getPerimeterRatio

        public double getPerimeterRatio()
        Returns a quality metric for the tree.
        Returns:
        double
        Since:
        4.0.0
      • optimize

        public void optimize()
        Description copied from interface: BroadphaseDetector
        Attempts to optimize the broad-phase based on the current state.

        This method could be very intensive so should only be called if there's a clear benefit.