Skip to content

Commit

Permalink
perf: faster broadphase iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
ASGAlex committed Oct 20, 2023
1 parent 47df103 commit ab727b2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
3 changes: 2 additions & 1 deletion example/lib/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,8 @@ class SpatialGridDebugCameraWrapper extends SpatialGridCameraWrapper {
class BoundingBoxGridGame extends BoundingHitbox {
@override
FutureOr<void> onLoad() {
fastCollisionForRects = true;
cacheAbsoluteScaledSize = cacheAbsoluteAngle =
groupAbsoluteCacheByType = fastCollisionForRects = true;
return super.onLoad();
}
}
Expand Down
28 changes: 16 additions & 12 deletions lib/src/collisions/broadphase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class SpatialGridBroadphase extends Broadphase<ShapeHitbox> {

@protected
final activeCollisions = <ShapeHitbox>{};
var _activeCollisionsUnmodifiable = <ShapeHitbox>[];

@internal
final allCollisionsByCell = <Cell, HashSet<ShapeHitbox>>{};
Expand Down Expand Up @@ -89,6 +90,7 @@ class SpatialGridBroadphase extends Broadphase<ShapeHitbox> {

@override
void update() {
var activeCollisionsChanged = false;
for (final operation in scheduledOperations) {
if (operation.add) {
final cell = operation.cell;
Expand All @@ -102,6 +104,7 @@ class SpatialGridBroadphase extends Broadphase<ShapeHitbox> {
var list = activeCollisionsByCell[cell];
list ??= activeCollisionsByCell[cell] = HashSet<ShapeHitbox>();
list.add(operation.hitbox);
activeCollisionsChanged = true;
} else {
var list = passiveCollisionsByCell[cell];
list ??= passiveCollisionsByCell[cell] = HashSet<ShapeHitbox>();
Expand All @@ -121,6 +124,7 @@ class SpatialGridBroadphase extends Broadphase<ShapeHitbox> {
} else {
if (operation.active) {
activeCollisions.remove(operation.hitbox);
activeCollisionsChanged = true;
final cellCollisions = activeCollisionsByCell[cell];
if (cellCollisions != null) {
cellCollisions.remove(operation.hitbox);
Expand All @@ -141,6 +145,9 @@ class SpatialGridBroadphase extends Broadphase<ShapeHitbox> {
}
}
scheduledOperations.clear();
if (activeCollisionsChanged) {
_activeCollisionsUnmodifiable = activeCollisions.toList(growable: false);
}
}

Iterable<CollisionProspect<ShapeHitbox>> querySubset(
Expand Down Expand Up @@ -189,8 +196,7 @@ class SpatialGridBroadphase extends Broadphase<ShapeHitbox> {
_potentials.clear();
_prospectPoolIndex = 0;
final activeChecked = HashSet<int>();
final unmodifiableActiveList = activeCollisions.toList(growable: false);
for (final activeItem in unmodifiableActiveList) {
for (final activeItem in _activeCollisionsUnmodifiable) {
final withGridSupport = activeItem.parentWithGridSupport;
if (withGridSupport == null ||
activeItem.isRemoving ||
Expand Down Expand Up @@ -258,12 +264,14 @@ class SpatialGridBroadphase extends Broadphase<ShapeHitbox> {

void _compareItemWithPotentials(
ShapeHitbox activeItem,
Iterable<ShapeHitbox> potentials, [
List<ShapeHitbox> potentials, [
HashSet<int>? activeChecked,
bool excludePureTypeCheck = false,
]) {
final activeParent = activeItem.hitboxParent;
for (final potential in potentials) {
final potentialsLength = potentials.length;
for (var i = 0; i < potentialsLength; i++) {
final potential = potentials[i];
if (potential.parent == null) {
continue;
}
Expand Down Expand Up @@ -479,14 +487,10 @@ class SpatialGridBroadphase extends Broadphase<ShapeHitbox> {
} else {
if (activeItem is BoundingHitbox) {
var skipTimes = activeItem.broadphaseMinimumDistanceSkip[potential];
if (skipTimes != null) {
if (skipTimes <= 0) {
activeItem.broadphaseMinimumDistanceSkip.remove(potential);
} else {
skipTimes--;
activeItem.broadphaseMinimumDistanceSkip[potential] = skipTimes;
return false;
}
if (skipTimes != null && skipTimes != 0) {
skipTimes--;
activeItem.broadphaseMinimumDistanceSkip[potential] = skipTimes;
return false;
}
}

Expand Down

0 comments on commit ab727b2

Please sign in to comment.