X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=blobdiff_plain;f=bulletml%2Fcollision.py;h=0394ed3b929b0f2edf2b6b768be3b389c4e3c8a5;hp=f185745ab77f410bcd541846a2b00d28badfd663;hb=7b73a60799150ec3df407a8a1620a613aad5f59c;hpb=e360de79a855c7c2a1dc80ae940aad00962175ad diff --git a/bulletml/collision.py b/bulletml/collision.py index f185745..0394ed3 100644 --- a/bulletml/collision.py +++ b/bulletml/collision.py @@ -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. +If Pyrex was available when installing, this will used optimized +versions of the functions. + 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. + + (This function is unoptimized.) """ 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 + (This function is unoptimized.) + """ # 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 + +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