- Object
-
- AbstractBroadphaseDetector<T>
-
- Sap<T>
-
- Type Parameters:
T
- the object type
- All Implemented Interfaces:
BroadphaseDetector<T>
,Shiftable
public final class Sap<T> extends AbstractBroadphaseDetector<T> implements BroadphaseDetector<T>
Implementation of the Sweep and Prune broad-phase collision detection algorithm.This implementation maintains a red-black tree of
CollisionBody
Fixture
s where each update will reposition the respectiveCollisionBody
Fixture
in the tree.Projects all
CollisionBody
Fixture
s on both the x and y axes and performs overlap checks on all the projections to test for possible collisions (AABB tests).This algorithm is O(n) for all
AbstractBroadphaseDetector.detect(AABB)
andAbstractBroadphaseDetector.raycast(Ray, double)
methods.- Since:
- 1.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 Sap(BroadphaseFilter<T> broadphaseFilter, AABBProducer<T> aabbProducer, AABBExpansionMethod<T> aabbExpansionMethod)
Default constructor.Sap(BroadphaseFilter<T> broadphaseFilter, AABBProducer<T> aabbProducer, AABBExpansionMethod<T> aabbExpansionMethod, int initialCapacity)
Full constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(T object)
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 object)
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.boolean
isUpdated(T object)
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 object)
Removes the given object from this broad-phase.void
setUpdated(T object)
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 object)
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
-
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface BroadphaseDetector
detect, detect, detect, detect, detect, detectIterator, getAABBExpansionMethod, getAABBProducer, getBroadphaseFilter, isUpdateTrackingEnabled, raycast
-
-
-
-
Constructor Detail
-
Sap
public Sap(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
-
Sap
public Sap(BroadphaseFilter<T> broadphaseFilter, AABBProducer<T> aabbProducer, AABBExpansionMethod<T> aabbExpansionMethod, int initialCapacity)
Full constructor.Allows fine tuning of the initial capacity of local storage for faster running times.
- Parameters:
broadphaseFilter
- the broadphase filteraabbProducer
- the AABB produceraabbExpansionMethod
- the AABB expansion methodinitialCapacity
- 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
-
add
public void add(T object)
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)
).- Specified by:
add
in interfaceBroadphaseDetector<T>
- Parameters:
object
- the object to add
-
remove
public boolean remove(T object)
Description copied from interface:BroadphaseDetector
Removes the given object from this broad-phase.- Specified by:
remove
in interfaceBroadphaseDetector<T>
- Parameters:
object
- 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
.- Specified by:
update
in interfaceBroadphaseDetector<T>
-
update
public void update(T object)
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.
- Specified by:
update
in interfaceBroadphaseDetector<T>
- Parameters:
object
- the object
-
isUpdated
public boolean isUpdated(T object)
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.
- Specified by:
isUpdated
in interfaceBroadphaseDetector<T>
- Parameters:
object
- the object- Returns:
- boolean
-
setUpdated
public void setUpdated(T object)
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.- Specified by:
setUpdated
in interfaceBroadphaseDetector<T>
- Parameters:
object
- the object
-
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.
- Specified by:
clearUpdates
in interfaceBroadphaseDetector<T>
-
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()
.- Specified by:
getAABB
in interfaceBroadphaseDetector<T>
- Parameters:
object
- the object- Returns:
AABB
-
contains
public boolean contains(T object)
Description copied from interface:BroadphaseDetector
Returns true if the given object exists in this broad-phase.- Specified by:
contains
in interfaceBroadphaseDetector<T>
- Parameters:
object
- the object- Returns:
- boolean
-
clear
public void clear()
Description copied from interface:BroadphaseDetector
Clear all the internal state of this broad-phase.- Specified by:
clear
in interfaceBroadphaseDetector<T>
-
size
public int size()
Description copied from interface:BroadphaseDetector
Returns the number of objects managed in this broad-phase.- Specified by:
size
in interfaceBroadphaseDetector<T>
- Returns:
- int
-
isUpdateTrackingSupported
public boolean isUpdateTrackingSupported()
Description copied from interface:BroadphaseDetector
Returns whether this particularBroadphaseDetector
supports update tracking.- Specified by:
isUpdateTrackingSupported
in interfaceBroadphaseDetector<T>
- Returns:
- boolean
-
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()
-
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.- Specified by:
detectIterator
in interfaceBroadphaseDetector<T>
- 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.- Specified by:
detectIterator
in interfaceBroadphaseDetector<T>
- 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.- Specified by:
raycastIterator
in interfaceBroadphaseDetector<T>
- 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.
-
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.
- Specified by:
optimize
in interfaceBroadphaseDetector<T>
-
-