# HG changeset patch # User Simon Cross # Date 1301954773 -7200 # Node ID 36267deaccd80ff5c84c5d3863e3ed99b720cf60 # Parent e1dd3b7852697ee1da3f1e836a31cb42e2591ad8 Add friction. Remove old dirty sprite attributes. diff -r e1dd3b785269 -r 36267deaccd8 skaapsteker/physics.py --- a/skaapsteker/physics.py Tue Apr 05 00:03:33 2011 +0200 +++ b/skaapsteker/physics.py Tue Apr 05 00:06:13 2011 +0200 @@ -12,7 +12,7 @@ from . import options from .constants import EPSILON -class Sprite(pygame.sprite.DirtySprite): +class Sprite(pygame.sprite.Sprite): # physics attributes mobile = True # whether the velocity may be non-zero @@ -20,6 +20,7 @@ terminal_velocity = (300.0, 300.0) # maximum horizontal and vertial speeds (pixels / s) bounce_factor = (0.95, 0.95) # bounce factor mass = 1.0 # used for shared collisions and applying forces + friction_coeff = (0.99, 0.99) # friction factor # collision attributes # Sprite X collides with Y iff (X.collision_layer in Y.collides_with) and X.check_collides(Y) @@ -40,9 +41,6 @@ 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.visible = 1 - self.dirty = 1 - self.blendmode = 0 def init_pos(self): self._float_pos = self.rect.topleft @@ -85,6 +83,10 @@ def collided(self, other): pass + def apply_friction(self): + v_x, v_y = self.velocity + self.velocity = self.friction_coeff[0] * v_x, self.friction_coeff[1] * v_y + def bounce(self, other, normal): """Alter velocity after a collision. @@ -168,6 +170,7 @@ sprite.deltap(max(-1.1 * frac, -dt)) sprite.bounce(others[idx], normal) + for other in others: sprite.collided(other) @@ -185,6 +188,10 @@ for sprite in self._gravitators: sprite.deltav(dv) + # friction + for sprite in self._mobiles: + sprite.apply_friction() + # position update and collision check (do last) for sprite in self._mobiles: sprite.deltap(dt)