Optional Pyrex extension for the collision module. More than doubles its speed.
[python-bulletml.git] / bulletml / collision.py
index f185745..0394ed3 100644 (file)
@@ -4,6 +4,9 @@ This module provides simple collision checking appropriate for
 shmups. It provides a routine to check whether two moving circles
 collided during the past frame.
 
 shmups. It provides a routine to check whether two moving circles
 collided during the past frame.
 
+If Pyrex was available when installing, this will used optimized
+versions of the functions.
+
 Basic Usage:
 
     from bulletml.collision import collides
 Basic Usage:
 
     from bulletml.collision import collides
@@ -20,6 +23,8 @@ def overlaps(a, b):
     Usually, you'll want to use the 'collides' method instead, but
     this one can be useful for just checking to see if the player has
     entered an area or hit a stationary oject.
     Usually, you'll want to use the 'collides' method instead, but
     this one can be useful for just checking to see if the player has
     entered an area or hit a stationary oject.
+
+    (This function is unoptimized.)
     """
 
     dx = a.x - b.x
     """
 
     dx = a.x - b.x
@@ -37,6 +42,8 @@ def collides(a, b):
     px, py - not required, defaults to x, y, previous frame position
     radius - not required, defaults to 0.5
 
     px, py - not required, defaults to x, y, previous frame position
     radius - not required, defaults to 0.5
 
+    (This function is unoptimized.)
+
     """
     # Current locations.
     xa = a.x
     """
     # Current locations.
     xa = a.x
@@ -79,3 +86,18 @@ def collides(a, b):
 
     # dist_sq < radius_sq
     return dist_x * dist_x + dist_y * dist_y <= radius * radius
 
     # dist_sq < radius_sq
     return dist_x * dist_x + dist_y * dist_y <= radius * radius
+
+def collides_all(a, others):
+    """Filter the second argument to those that collide with the first.
+
+    This is equivalent to filter(lambda o: collides(a, o), others),
+    but is much faster when the compiled extension is available (which
+    it is not currently).
+
+    """
+    return filter(lambda o: collides(a, o), others)
+
+try:
+    from bulletml._collision import collides, overlaps, collides_all
+except ImportError:
+    pass