gummworld2.spatialhash
index
c:\cygwin64\home\bw\dev\python\dist\gummworld2\gamelib\gummworld2\spatialhash.py

spatialhash.py - High performance spatial hash for spatial
partitioning and fast collision detection.
 
Objects (other than geometry.LineGeometry) must have a pygame Rect attribute.
Optionally, objects may have a collided static method attribute for lower-level
collision detection (see the gummworld2.geometry module).
 
Objects that are outside the world bounding rect are ignored by add().
 
As of v0.5.0 SpatialHash will sort objects returned by most methods that return
a list of objects. See the methods' docstrings. In order to have the objects
sorted set the SpatialHash's sort_key method. This is the same key that is
passed to sort(key=func) and sorted(key=func), e.g.:
key=lambda obj: obj.rect.bottom.
 
This module is derived from the article and source code written by Conkerjo at
http://conkerjo.wordpress.com/2009/06/13/spatial-hashing-implementation-for-fast-2d-collisions/.

 
Modules
       
gummworld2.geometry

 
Classes
       
__builtin__.object
SpatialHash

 
class SpatialHash(__builtin__.object)
     Methods defined here:
__contains__(self, obj)
__init__(self, cell_size=50)
__iter__(self)
This method honors self.sort_key.
__len__(self)
__repr__(self)
__str__(self)
add(self, entity)
Add or re-add obj. Return True if in bounds, else return False.
 
If this method returns False then the object is completely out of
bounds and cannot be stored in this space.
 
Note that when obj changes its position, you must add it again so that
its cell membership is updated. This method first removes the object if
it is already in the spatial hash.
addlist(self, entities)
clear(self)
Clear all objects.
collide(self, entity)
Return list of entities that collide with entity.
collidealldict(self, rect=None)
Return dict of all collisions.
 
If rect is specified, only the cells that intersect rect will be
checked.
 
The contents of the returned dict are: {entity: [entity, ...}
collideany(self, entity)
Return True if entity collides with any other object, else False.
collidedict(self, entities)
Return dict of entities that collide with entities.
get_cell(self, cell_id)
Return the cell stored at bucket index cell_id.
 
The returned cell is a list of entities. None is returned if a cell does
not exist for cell_id.
get_cell_pos(self, cell_id)
Return the world coordinates for topleft corner of cell.
get_nearby_entities(self, entity)
Return a list of entities that share the same cells as entity.
 
This method honors self.sort_key.
index(self, cell)
Return the bucket index of cell.
 
Returns None if cell does not exist in buckets.
 
Note that SpatialHash.buckets.index(cell) does *NOT* work because
list.index() tests equality, not identity.
index_at(self, x, y)
Return the cell_id of the cell that contains point (x,y).
 
None is returned if point (x,y) is not in bounds.
intersect_cell_ids(self, rect)
Return list of cell ids that intersect rect.
 
The return value is a list of int. Each int is a key for self.buckets. Note: the values are NOT checked for
presence in buckets; so if self.buckets[i] is accessed directly, empty buckets may be accidentally created.
 
:param rect: pygame.Rect; bounding rect
:return: list of int
intersect_entities(self, rect)
Return list of entities whose rects intersect rect.
 
This method honors self.sort_key.
remove(self, entity)
removelist(self, entities)

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
entities
Return the entire list of entities.
 
This method honors self.sort_key.

 
Data
        __all__ = ['SpatialHash']