Implement optimized collision detection system using NumPy

- Introduced a hybrid collision detection approach that utilizes NumPy for vectorized operations, improving performance for games with many entities (200+).
- Added a spatial grid for efficient lookups and AABB (Axis-Aligned Bounding Box) collision detection.
- Implemented a new `CollisionSystem` class with methods for registering units, checking collisions, and managing spatial data.
- Created performance tests to benchmark the new collision system against the old O(n²) method, demonstrating significant speed improvements.
- Updated existing code to integrate the new collision detection system and ensure compatibility with game logic.
This commit is contained in:
2025-10-24 19:13:30 +02:00
parent 47028c95ae
commit 12836dd2d2
17 changed files with 1591 additions and 64 deletions
+4 -1
View File
@@ -26,6 +26,8 @@ class Unit(ABC):
Bounding box for collision detection (x1, y1, x2, y2).
stop : int
Number of ticks to remain stationary.
collision_layer : int
Collision layer for the optimized collision system.
Methods
-------
@@ -38,7 +40,7 @@ class Unit(ABC):
die()
Remove unit from game and handle cleanup.
"""
def __init__(self, game, position=(0, 0), id=None):
def __init__(self, game, position=(0, 0), id=None, collision_layer=0):
"""Initialize a unit with game reference and position."""
self.id = id if id else uuid.uuid4()
self.game = game
@@ -49,6 +51,7 @@ class Unit(ABC):
self.partial_move = 0
self.bbox = (0, 0, 0, 0)
self.stop = 0
self.collision_layer = collision_layer
@abstractmethod
def move(self):