# HG changeset patch # User Simon Cross # Date 1302036674 -7200 # Node ID b2e0f1246c38881a713c27beba42efa042ca3982 # Parent 1008a7bae425e3cf740831f6b8bfc804cc2459c9 Hook up bitmasked based collisions (mask caching may be unnecessary, please comment on whether this effects performance for you). diff -r 1008a7bae425 -r b2e0f1246c38 skaapsteker/physics.py --- a/skaapsteker/physics.py Tue Apr 05 22:43:09 2011 +0200 +++ b/skaapsteker/physics.py Tue Apr 05 22:51:14 2011 +0200 @@ -8,6 +8,7 @@ import pygame import pygame.draw import pygame.sprite +from pygame.mask import from_surface from . import options from .constants import EPSILON @@ -35,7 +36,7 @@ debug_color = (240, 0, 0) floor = False # We special case collisions with ground objects - block = False + block = False def __init__(self, *args, **kwargs): super(Sprite, self).__init__(*args, **kwargs) @@ -45,6 +46,7 @@ self.collide_rect = pygame.Rect(0, 0, 10, 10) # rectangle we use for collisions self.image = pygame.Surface((10, 10)) self.image.fill((0, 0, 200)) + self._mask_cache = {} # image id -> collision bit mask def init_pos(self): self._float_pos = self.rect.topleft @@ -81,8 +83,18 @@ delta_pos = self.rect.left - old_pos[0], self.rect.top - old_pos[1] self.collide_rect.move_ip(delta_pos) + def _check_mask(self): + image_id = id(self.image) + mask = self._mask_cache.get(image_id, None) + if mask is None: + mask = self._mask_cache[image_id] = from_surface(self.image) + self.mask = mask + def check_collides(self, other): - return True # default to relying purefly on collision_layer and collides_with + # check bitmasks for collision + self._check_mask() + other._check_mask() + return pygame.sprite.collide_mask(self, other) def collided(self, other): pass