Class DynamicAABBTree<T extends CollisionBody<E>,E extends Fixture>
- Object
-
- AbstractBroadphaseDetector<T,E>
-
- DynamicAABBTree<T,E>
-
- Type Parameters:
T
- theCollisionBody
typeE
- theFixture
type
- All Implemented Interfaces:
BroadphaseDetector<T,E>
,Shiftable
public final class DynamicAABBTree<T extends CollisionBody<E>,E extends Fixture> extends AbstractBroadphaseDetector<T,E> implements BroadphaseDetector<T,E>
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.0.0
- Author:
- William Bittle
-
-
Field Summary
-
Fields inherited from interface BroadphaseDetector
DEFAULT_AABB_EXPANSION, DEFAULT_INITIAL_CAPACITY
-
-
Constructor Summary
Constructors Constructor Description DynamicAABBTree()
Default constructor.DynamicAABBTree(int initialCapacity)
Optional constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(T body, E fixture)
Adds a newFixture
for the givenCollisionBody
to the broad-phase.void
clear()
Clears all theCollisionBody
Fixture
s from this broad-phase and any state held by this broadphase.void
clearUpdates()
Clears any internal state that tracks whatCollisionBody
Fixture
s have been updated.boolean
contains(CollisionItem<T,E> item)
Returns true if the givenFixture
on the givenCollisionBody
has been added to this broadphase.boolean
contains(T body, E fixture)
Returns true if the givenFixture
on the givenCollisionBody
has been added to this broadphase.Iterator<CollisionPair<T,E>>
detectIterator(boolean forceFullDetection)
Performs collision detection on allCollisionBody
Fixture
s that have been added to thisBroadphaseDetector
and returns the list of potential pairs.Iterator<CollisionItem<T,E>>
detectIterator(AABB aabb)
Performs a broad-phase collision test using the givenAABB
and returns the items that overlap.AABB
getAABB(CollisionItem<T,E> item)
Returns the AABB for the givenCollisionBody
Fixture
.int
getHeight()
Returns the height of the tree.double
getPerimeterRatio()
Returns a quality metric for the tree.boolean
isUpdated(CollisionItem<T,E> item)
Returns true if the givenFixture
is included in the updated list.boolean
isUpdated(T body, E fixture)
Returns true if the givenFixture
is included in the updated list.void
optimize()
Attempts to optimize the broadphase based on the current state.Iterator<CollisionItem<T,E>>
raycastIterator(Ray ray, double length)
Performs a raycast over all the bodies in the broad-phase and returns the items that intersect.boolean
remove(CollisionItem<T,E> item)
Removes the givenFixture
for the givenCollisionBody
from the broad-phase and returns true if it was found.boolean
remove(T body, E fixture)
Removes the givenFixture
for the givenCollisionBody
from the broad-phase and returns true if it was found.void
setUpdated(T body, E fixture)
TheBroadphaseDetector.update(CollisionBody, Fixture)
method will only mark theFixture
as updated if theFixture
has moved enough to change the internally stored AABB.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 ofCollisionBody
Fixture
s that are being managed in this broad-phase.void
update(T body, E fixture)
Updates the givenCollisionBody
'sFixture
.-
Methods inherited from class AbstractBroadphaseDetector
add, contains, detect, detect, detect, detect, detect, detect, detect, getAABB, getAABB, getAABBExpansion, isAABBExpansionSupported, isUpdated, isUpdateTrackingEnabled, isUpdateTrackingSupported, raycast, raycast, remove, setAABBExpansion, setUpdated, supportsAABBExpansion, update
-
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface BroadphaseDetector
add, contains, detect, detect, detect, detect, detect, detect, detect, getAABB, getAABB, getAABBExpansion, isAABBExpansionSupported, isUpdated, isUpdateTrackingEnabled, isUpdateTrackingSupported, raycast, raycast, remove, setAABBExpansion, setUpdated, supportsAABBExpansion, update
-
-
-
-
Constructor Detail
-
DynamicAABBTree
public DynamicAABBTree()
Default constructor.
-
DynamicAABBTree
public DynamicAABBTree(int initialCapacity)
Optional constructor.Allows fine tuning of the initial capacity of local storage for faster running times.
- Parameters:
initialCapacity
- the initial capacity of local storage- Throws:
IllegalArgumentException
- if initialCapacity is less than zero- Since:
- 3.1.1
-
-
Method Detail
-
add
public void add(T body, E fixture)
Description copied from interface:BroadphaseDetector
Adds a newFixture
for the givenCollisionBody
to the broad-phase.- Specified by:
add
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
body
- the bodyfixture
- the fixture to add
-
remove
public boolean remove(T body, E fixture)
Description copied from interface:BroadphaseDetector
Removes the givenFixture
for the givenCollisionBody
from the broad-phase and returns true if it was found.- Specified by:
remove
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
body
- the bodyfixture
- the fixture to remove- Returns:
- boolean true if the fixture was found and removed
-
remove
public boolean remove(CollisionItem<T,E> item)
Description copied from interface:BroadphaseDetector
Removes the givenFixture
for the givenCollisionBody
from the broad-phase and returns true if it was found.- Specified by:
remove
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
item
- the collision item- Returns:
- boolean true if the fixture was found and removed
-
update
public void update(T body, E fixture)
Description copied from interface:BroadphaseDetector
Updates the givenCollisionBody
'sFixture
.Used when a fixture on a
CollisionBody
has moved or rotated.This method will add the
Fixture
if it doesn't currently exist in this broad-phase.- Specified by:
update
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
body
- theCollisionBody
fixture
- theFixture
that has moved
-
isUpdated
public boolean isUpdated(T body, E fixture)
Description copied from interface:BroadphaseDetector
Returns true if the givenFixture
is included in the updated list.If
BroadphaseDetector.isUpdateTrackingEnabled()
is false, this method will always return true.Returns false if the given body-fixture pair is not part of this broadphase.
- Specified by:
isUpdated
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
body
- theCollisionBody
fixture
- theFixture
- Returns:
- boolean
-
isUpdated
public boolean isUpdated(CollisionItem<T,E> item)
Description copied from interface:BroadphaseDetector
Returns true if the givenFixture
is included in the updated list.If
BroadphaseDetector.isUpdateTrackingEnabled()
is false, this method will always return true.Returns false if the given body-fixture pair is not part of this broadphase.
- Specified by:
isUpdated
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
item
- the collision item- Returns:
- boolean
-
setUpdated
public void setUpdated(T body, E fixture)
Description copied from interface:BroadphaseDetector
TheBroadphaseDetector.update(CollisionBody, Fixture)
method will only mark theFixture
as updated if theFixture
has moved enough to change the internally stored AABB.This method is intended to force the broadphase to include the
Fixture
in the updated list to ensure they are checked in the updated-only detection routine.- Specified by:
setUpdated
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
body
- theCollisionBody
fixture
- theFixture
-
setUpdateTrackingEnabled
public void setUpdateTrackingEnabled(boolean flag)
Description copied from interface:BroadphaseDetector
Sets the update tracking to the given flag.Tracking updates to the broadphase 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 broadphase manually to ensure the updates set is non-empty. Typically this will self heal in the next iteration though.
NOTE: Some broadphase detectors may not support this feature.
- Specified by:
setUpdateTrackingEnabled
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Overrides:
setUpdateTrackingEnabled
in classAbstractBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
flag
- true to turn on update tracking- See Also:
BroadphaseDetector.isUpdateTrackingSupported()
-
clearUpdates
public void clearUpdates()
Description copied from interface:BroadphaseDetector
Clears any internal state that tracks whatCollisionBody
Fixture
s have been updated.Typically this method would be called from a pipeline after a broadphase collision detection method has been called to clear the state before starting to track new updates.
- Specified by:
clearUpdates
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
-
getAABB
public AABB getAABB(CollisionItem<T,E> item)
Description copied from interface:BroadphaseDetector
Returns the AABB for the givenCollisionBody
Fixture
.If the body and its fixture have not been added to this broad-phase, a new AABB is created and returned (but not added to broad-phase).
In all cases this method returns the expanded AABB in the case this detector supports it.
NOTE: The
AABB
returned from this method should not be modified. Instead use theAABB.copy()
method to create a new instance to modify.- Specified by:
getAABB
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
item
- the collision item- Returns:
AABB
-
contains
public boolean contains(T body, E fixture)
Description copied from interface:BroadphaseDetector
Returns true if the givenFixture
on the givenCollisionBody
has been added to this broadphase.- Specified by:
contains
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
body
- theCollisionBody
fixture
- theFixture
- Returns:
- boolean
-
contains
public boolean contains(CollisionItem<T,E> item)
Description copied from interface:BroadphaseDetector
Returns true if the givenFixture
on the givenCollisionBody
has been added to this broadphase.- Specified by:
contains
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
item
- the collision item- Returns:
- boolean
-
clear
public void clear()
Description copied from interface:BroadphaseDetector
Clears all theCollisionBody
Fixture
s from this broad-phase and any state held by this broadphase.- Specified by:
clear
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
-
size
public int size()
Description copied from interface:BroadphaseDetector
Returns the number ofCollisionBody
Fixture
s that are being managed in this broad-phase.- Specified by:
size
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Returns:
- int
-
detectIterator
public Iterator<CollisionPair<T,E>> detectIterator(boolean forceFullDetection)
Description copied from interface:BroadphaseDetector
Performs collision detection on allCollisionBody
Fixture
s that have been added to thisBroadphaseDetector
and returns the list of potential pairs.The returned pairs from this method will depend on the
BroadphaseDetector.isUpdateTrackingEnabled()
flag. If the flag is true, then only updated pairs will be emitted, otherwise all pairs are emitted.Use the forceFullDetection parameter to override this behavior for this 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.- Specified by:
detectIterator
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
forceFullDetection
- true if a full detection should be performed- Returns:
- Iterator<
CollisionPair
>
-
detectIterator
public Iterator<CollisionItem<T,E>> detectIterator(AABB aabb)
Description copied from interface:BroadphaseDetector
Performs a broad-phase collision test using the givenAABB
and returns the items that overlap.- Specified by:
detectIterator
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
aabb
- theAABB
to test- Returns:
- Iterator<
CollisionItem
>
-
raycastIterator
public Iterator<CollisionItem<T,E>> raycastIterator(Ray ray, double length)
Description copied from interface:BroadphaseDetector
Performs a raycast over all the bodies in the broad-phase and returns the items that intersect.- Specified by:
raycastIterator
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Parameters:
ray
- theRay
length
- the length of the ray; 0.0 for infinite length- Returns:
- Iterator<
CollisionItem
>
-
shift
public void shift(Vector2 shift)
Description copied from interface:Shiftable
Translates the object to match the given coordinate shift.
-
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
-
optimize
public void optimize()
Description copied from interface:BroadphaseDetector
Attempts to optimize the broadphase based on the current state.This method could be very intensive so should only be called if there's a clear benefit.
- Specified by:
optimize
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
-
-