Skip to content

Commit d71fba2

Browse files
pvcravenPaul V Cravenclaude
authored
Use cached hit box bounds in the spatial hash (#2900)
SpatialHash._get_cell_bounds() scanned the hit box's adjusted points itself. It now reads HitBox.get_adjusted_bounds(), which caches the bounds until the points change and shares them with collision checks. For sprites that haven't moved since their bounds were last needed, querying is ~40% faster and add + remove ~33% faster. Moving sprites are unchanged, since their points are recalculated either way. Co-authored-by: Paul V Craven <paul.craven@optimizley.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a3a9eb2 commit d71fba2

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Arcade [PyPi Release History](https://pypi.org/project/arcade/#history) page.
1919
### Misc Changes
2020
- Sped up sprite collision checks. Sprites that pass the quick distance check are now compared by cached hit box bounds before the polygon test, and the polygon test skips horizontal and vertical edges, which the bounds check already covers. Checks that reach the polygon test are about 2-4x faster, e.g. 8.0 to 2.3 µs for two box hit boxes and 21.6 to 10.2 µs for two default octagon hit boxes. `are_polygons_intersecting` is also faster (7.3 to 1.6 µs for two rectangles).
2121
- Sped up collision checks further by caching each hit box's distinct edge directions. Parallel edges (such as opposite sides of the default octagon hit boxes, or matching edges on two sprites with the same angle) are only tested once, and the cache is kept when a sprite moves. Two unrotated octagon hit boxes go from 12.0 to 4.6 µs, and two rotated 30° from 20.8 to 6.9 µs.
22+
- `SpatialHash` now uses the hit box's cached bounds to find a sprite's grid cells, so they're shared with collision checks. Adding, removing, and querying for sprites that haven't moved is about 33-40% faster.
2223
- Added collision benchmark scripts in `benchmarks/collisions/` (`micro.py`, `hit_box.py`, and `compare_reference.py`, which checks collision results against a simple reference on random sprite pairs) and `benchmarks/spatial_hash/queries.py`.
2324
- Updated the optional `pymunk` extra to 7.3.0 (from 7.2.0). Packaging-only release (free-threaded CPython and pyodide wheels, improved type hints) with no breaking changes or deprecations.
2425

‎arcade/sprite_list/spatial_hash.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,10 @@ def reset(self):
110110

111111
def _get_cell_bounds(self, sprite: BasicSprite) -> tuple[IPoint, IPoint]:
112112
"""Get the min and max cells covered by a sprite's hit box."""
113-
# One pass over the points instead of using left/right/bottom/top,
114-
# which each fetch and scan the hit box points separately.
115-
x_points, y_points = zip(*sprite.hit_box.get_adjusted_points())
116-
min_point = self.hash((trunc(min(x_points)), trunc(min(y_points))))
117-
max_point = self.hash((trunc(max(x_points)), trunc(max(y_points))))
113+
# The hit box caches its bounds, so collision checks can reuse them
114+
left, right, bottom, top = sprite.hit_box.get_adjusted_bounds()
115+
min_point = self.hash((trunc(left), trunc(bottom)))
116+
max_point = self.hash((trunc(right), trunc(top)))
118117
return min_point, max_point
119118

120119
def add(self, sprite: SpriteType) -> None:

0 commit comments

Comments
 (0)