comparison skaapsteker/physics.py @ 172:bf144d817113

if year in range(1980, 1990): continue # how can we sleep while the kitsune is stuck to the floor?
author Simon Cross <hodgestar@gmail.com>
date Wed, 06 Apr 2011 01:35:20 +0200
parents 134ed5e026ae
children 88a78fb9fa82
comparison
equal deleted inserted replaced
171:13e10b877f6c 172:bf144d817113
28 # Collisions result in the colliding movement being partially backed out, a call to X.bounce(frac) and a call to X.collided(Y) 28 # Collisions result in the colliding movement being partially backed out, a call to X.bounce(frac) and a call to X.collided(Y)
29 # X.bounce(frac) is only called for the first (as determined by backing out distance) collision in a multi-collision event 29 # X.bounce(frac) is only called for the first (as determined by backing out distance) collision in a multi-collision event
30 collision_layer = None # never collides with anything 30 collision_layer = None # never collides with anything
31 collides_with = set() # nothing collides with this 31 collides_with = set() # nothing collides with this
32 32
33 # set to True to have .update() called once per tick 33 # set to True to have .update() called once per tick (and have .collision_group set)
34 wants_updates = False 34 wants_updates = False
35 35
36 debug_color = (240, 0, 0) 36 debug_color = (240, 0, 0)
37 37
38 floor = False # We special case collisions with ground objects 38 floor = False # We special case collisions with ground objects
44 self.velocity = (0.0, 0.0) 44 self.velocity = (0.0, 0.0)
45 self.rect = pygame.Rect(0, 0, 10, 10) # sub-classes should override 45 self.rect = pygame.Rect(0, 0, 10, 10) # sub-classes should override
46 self.collide_rect = pygame.Rect(0, 0, 10, 10) # rectangle we use for collisions 46 self.collide_rect = pygame.Rect(0, 0, 10, 10) # rectangle we use for collisions
47 self.image = pygame.Surface((10, 10)) 47 self.image = pygame.Surface((10, 10))
48 self.image.fill((0, 0, 200)) 48 self.image.fill((0, 0, 200))
49 self.collision_group = None
49 self._mask_cache = {} # image id -> collision bit mask 50 self._mask_cache = {} # image id -> collision bit mask
50 51
51 def init_pos(self): 52 def init_pos(self):
52 self._float_pos = self.rect.topleft 53 self._float_pos = self.rect.topleft
53 54
130 else: 131 else:
131 self.deltav((dv_x, dv_y)) # oof 132 self.deltav((dv_x, dv_y)) # oof
132 133
133 def update(self): 134 def update(self):
134 pass # only called in wants_update = True 135 pass # only called in wants_update = True
136
137 def check_collide_rect(self, new_collide_rect, new_rect, new_image):
138 if self.collision_group is None:
139 return True
140
141 # TODO: decide whether to throw out checking of existing
142 # collisions. Doesn't seem needed at the moment and takes
143 # time.
144 old_image = self.image
145 old_rect = self.rect
146 #rect_collides = self.collide_rect.colliderect
147 old_collisions = set()
148 #for other in self.collision_group:
149 # if rect_collides(other.collide_rect) \
150 # and self.check_collides(other):
151 # old_collisions.add(other)
152
153 self.image = new_image
154 self.rect = new_rect
155 new_rect_collides = new_collide_rect.colliderect
156 new_collisions = set()
157 for other in self.collision_group:
158 if new_rect_collides(other.collide_rect) \
159 and self.check_collides(other):
160 new_collisions.add(other)
161
162 self.image = old_image
163 self.rect = old_rect
164 return not bool(new_collisions - old_collisions)
135 165
136 166
137 class World(object): 167 class World(object):
138 168
139 GRAVITY = 0.0, 9.8 * 20.0 # pixels / s^2 169 GRAVITY = 0.0, 9.8 * 20.0 # pixels / s^2
163 self._updaters.add(sprite) 193 self._updaters.add(sprite)
164 self._add_collision_group(sprite.collision_layer) 194 self._add_collision_group(sprite.collision_layer)
165 for layer in sprite.collides_with: 195 for layer in sprite.collides_with:
166 self._add_collision_group(layer) 196 self._add_collision_group(layer)
167 self._collision_groups[layer].add(sprite) 197 self._collision_groups[layer].add(sprite)
198 if sprite.wants_updates:
199 self._updaters.add(sprite)
200 sprite.collision_group = self._collision_groups[sprite.collision_layer]
168 201
169 def _add_collision_group(self, layer): 202 def _add_collision_group(self, layer):
170 if layer in self._collision_groups: 203 if layer in self._collision_groups:
171 return 204 return
172 self._collision_groups[layer] = pygame.sprite.Group() 205 self._collision_groups[layer] = pygame.sprite.Group()