X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=blobdiff_plain;f=bulletml%2Fcollision.py;h=1683e58f6ca70fa583e65e9e967835d2c6831472;hp=f185745ab77f410bcd541846a2b00d28badfd663;hb=ee9429c2c319d5d794e49da7b0fe13fca9945194;hpb=0f067785de711c772c2c44c7f1e1cf8b44b2704f diff --git a/bulletml/collision.py b/bulletml/collision.py index f185745..1683e58 100644 --- a/bulletml/collision.py +++ b/bulletml/collision.py @@ -4,6 +4,10 @@ This module provides simple collision checking appropriate for shmups. It provides a routine to check whether two moving circles collided during the past frame. +An equivalent C-based version will be used automatically if it was +compiled and installed with the module. If available, it will be noted +in the docstrings for the functions. + Basic Usage: from bulletml.collision import collides @@ -20,6 +24,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 +43,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 +87,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