changeset 128:36267deaccd8

Add friction. Remove old dirty sprite attributes.
author Simon Cross <hodgestar@gmail.com>
date Tue, 05 Apr 2011 00:06:13 +0200
parents e1dd3b785269
children 8a8c00a643fa
files skaapsteker/physics.py
diffstat 1 files changed, 11 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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)