Class BruteForceBroadphase<T extends CollisionBody<E>,E extends Fixture>
- Object
-
- AbstractBroadphaseDetector<T,E>
-
- BruteForceBroadphase<T,E>
-
- Type Parameters:
T
- theCollisionBody
typeE
- theFixture
type
- All Implemented Interfaces:
BroadphaseDetector<T,E>
,Shiftable
public final class BruteForceBroadphase<T extends CollisionBody<E>,E extends Fixture> extends AbstractBroadphaseDetector<T,E> implements BroadphaseDetector<T,E>
This class implements the simplest possible broad-phase detector, a brute-force algorithm for finding all pairs of collisions (and similar queries).This implementation is not tuned for performance in any way and should not be used except for testing purposes. One main reason this was developed is for automated testing of the other broad-phase detectors.
The logic of this class is simple: It holds a hash table of all the nodes and each time a query is made it scans linearly all the nodes to find the answer.
Important note: This class must not use AABB expansion in order to always return the minimum set of pairs/items. This property is used to test the other broad-phase detectors correctly.
- Since:
- 3.4.0
- Version:
- 4.0.0
- Author:
- Manolis Tsamis
-
-
Field Summary
-
Fields inherited from interface BroadphaseDetector
DEFAULT_AABB_EXPANSION, DEFAULT_INITIAL_CAPACITY
-
-
Constructor Summary
Constructors Constructor Description BruteForceBroadphase()
Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated 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
.double
getAABBExpansion()
Returns theAABB
expansion value used to improve performance of broad-phase updates.boolean
isAABBExpansionSupported()
Returns whether this particularBroadphaseDetector
supports expanding AABBs.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.boolean
isUpdateTrackingEnabled()
Returns true if this broadphase is tracking updated items.boolean
isUpdateTrackingSupported()
Returns whether this particularBroadphaseDetector
supports update tracking.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.boolean
supportsAABBExpansion()
Deprecated.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, isUpdated, raycast, raycast, remove, setAABBExpansion, setUpdated, update
-
-
-
-
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
-
isUpdateTrackingEnabled
public boolean isUpdateTrackingEnabled()
Description copied from interface:BroadphaseDetector
Returns true if this broadphase is tracking updated items.Tracking updates to the broadphase can have huge performance gains if the majority of objects are stationary or moving slowly enough.
- Specified by:
isUpdateTrackingEnabled
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Overrides:
isUpdateTrackingEnabled
in classAbstractBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- 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 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.
-
getAABBExpansion
public double getAABBExpansion()
Description copied from interface:BroadphaseDetector
Returns theAABB
expansion value used to improve performance of broad-phase updates.If supportsAABBExpansion() returns false the value returned is unspecified and should not be taken into account.
- Specified by:
getAABBExpansion
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Overrides:
getAABBExpansion
in classAbstractBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Returns:
- double
- See Also:
BroadphaseDetector.setAABBExpansion(double)
-
supportsAABBExpansion
@Deprecated public boolean supportsAABBExpansion()
Deprecated.Description copied from interface:BroadphaseDetector
Returns whether this particularBroadphaseDetector
supports expanding AABBs.- Specified by:
supportsAABBExpansion
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Overrides:
supportsAABBExpansion
in classAbstractBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Returns:
- boolean
-
isAABBExpansionSupported
public boolean isAABBExpansionSupported()
Description copied from interface:BroadphaseDetector
Returns whether this particularBroadphaseDetector
supports expanding AABBs.- Specified by:
isAABBExpansionSupported
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Overrides:
isAABBExpansionSupported
in classAbstractBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Returns:
- boolean
-
isUpdateTrackingSupported
public boolean isUpdateTrackingSupported()
Description copied from interface:BroadphaseDetector
Returns whether this particularBroadphaseDetector
supports update tracking.- Specified by:
isUpdateTrackingSupported
in interfaceBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Overrides:
isUpdateTrackingSupported
in classAbstractBroadphaseDetector<T extends CollisionBody<E>,E extends Fixture>
- Returns:
- boolean
-
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>
-
-