- Object
-
- AbstractBroadphaseDetector<T>
-
- 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
-
-
Field Summary
-
Fields inherited from class AbstractBroadphaseDetector
AABB_REDUCTION_RATIO, aabbExpansionMethod, aabbProducer, broadphaseFilter, updateTrackingEnabled
-
Fields inherited from interface BroadphaseDetector
DEFAULT_INITIAL_CAPACITY
-
-
Constructor Summary
Constructors Constructor Description DynamicAABBTree(BroadphaseFilter<T> broadphaseFilter, AABBProducer<T> aabbProducer, AABBExpansionMethod<T> aabbExpansionMethod)
Default constructor.DynamicAABBTree(BroadphaseFilter<T> broadphaseFilter, AABBProducer<T> aabbProducer, AABBExpansionMethod<T> aabbExpansionMethod, int initialCapacity)
Optional constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(T obj)
Adds a new object to this broad-phase.void
clear()
Clear all the internal state of this broad-phase.void
clearUpdates()
Clears internal state that tracks what objects have been updated.boolean
contains(T obj)
Returns true if the given object exists in this broad-phase.Iterator<CollisionPair<T>>
detectIterator(boolean forceFullDetection)
Performs collision detection on all objects that have been added to thisBroadphaseDetector
and returns an iterator of potential collision pairs (i.e.Iterator<T>
detectIterator(AABB aabb)
Performs a broad-phase collision test using the givenAABB
and returns the items that overlap.AABB
getAABB(T object)
Returns the AABB for the given object.int
getHeight()
Returns the height of the tree.double
getPerimeterRatio()
Returns a quality metric for the tree.boolean
isUpdated(T obj)
Returns true if the given object is marked as updated in this broad-phase.boolean
isUpdateTrackingSupported()
Returns whether this particularBroadphaseDetector
supports update tracking.void
optimize()
Attempts to optimize the broad-phase based on the current state.Iterator<T>
raycastIterator(Ray ray, double length)
Performs a raycast over all the objects in the broad-phase and returns the items that intersect.boolean
remove(T obj)
Removes the given object from this broad-phase.void
setUpdated(T obj)
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.void
setUpdateTrackingEnabled(boolean flag)
Sets the update tracking to the given flag.void
shift(Vector2 shift)
Translates the object to match the given coordinate shift.int
size()
Returns the number of objects managed in this broad-phase.void
update()
Updates all the currently stored objects AABBs based on their current state, theAABBProducer
, and theAABBExpansionMethod
.void
update(T obj)
Updates the broad-phase representation of the given object.-
Methods inherited from class AbstractBroadphaseDetector
detect, detect, detect, detect, detect, detectIterator, getAABBExpansionMethod, getAABBProducer, getBroadphaseFilter, isUpdateTrackingEnabled, raycast
-
-
-
-
Constructor Detail
-
DynamicAABBTree
public DynamicAABBTree(BroadphaseFilter<T> broadphaseFilter, AABBProducer<T> aabbProducer, AABBExpansionMethod<T> aabbExpansionMethod)
Default constructor.- Parameters:
broadphaseFilter
- the broadphase filteraabbProducer
- the AABB produceraabbExpansionMethod
- 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 filteraabbProducer
- the AABB producer; cannot be nullaabbExpansionMethod
- the AABB expansion method; cannot be nullinitialCapacity
- the initial capacity of local storage- Throws:
NullPointerException
- if broadphaseFilter, aabbProducer or aabbExpansionMethod are nullIllegalArgumentException
- if initialCapacity is less than zero- Since:
- 3.1.1
-
-
Method Detail
-
getAABB
public AABB getAABB(T object)
Description copied from interface:BroadphaseDetector
Returns the AABB for the given object.NOTE: Some
BroadphaseDetector
s use modified (expanded for example) AABBs rather than tight fitting AABBs as a performance enhancement. This method returns the AABB used by this detector, and therefore, the modified AABB.NOTE: The
AABB
returned from this method should not be modified. Instead use theAABB.copy()
method to create a new instance to modify.If the given object does not exist in this
BroadphaseDetector
a new AABB is created based on the object's shape, theBroadphaseDetector.getAABBProducer()
andBroadphaseDetector.getAABBExpansionMethod()
.- Parameters:
object
- the object- Returns:
AABB
-
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()
Description copied from interface:BroadphaseDetector
Updates all the currently stored objects AABBs based on their current state, theAABBProducer
, and theAABBExpansionMethod
.
-
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 interfaceBroadphaseDetector<T>
- Overrides:
setUpdateTrackingEnabled
in classAbstractBroadphaseDetector<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 thisBroadphaseDetector
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
CollisionPair
s that are mutable internally. If you need to store the pairs outside of the iteration, be sure to call theCopyable.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 givenAABB
and returns the items that overlap.- Parameters:
aabb
- theAABB
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
- theRay
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 particularBroadphaseDetector
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.
-
-