changeset 121:5f5e43391395

Ramming speed, Mr Sulu\!
author Simon Cross <hodgestar@gmail.com>
date Mon, 04 Apr 2011 22:37:59 +0200
parents 9b08afeadf06
children 51bcc909873d
files skaapsteker/physics.py
diffstat 1 files changed, 17 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/skaapsteker/physics.py	Mon Apr 04 22:11:33 2011 +0200
+++ b/skaapsteker/physics.py	Mon Apr 04 22:37:59 2011 +0200
@@ -19,6 +19,7 @@
     gravitates = True # whether gravity applies to the sprite
     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
 
     # collision attributes
     # Sprite X collides with Y iff (X.collision_layer in Y.collides_with) and X.check_collides(Y)
@@ -51,6 +52,10 @@
     def draw_debug(self, surface):
         pygame.draw.rect(surface, self.get_debug_color(), self.rect, 1)
 
+    def deltaf(self, df):
+        dv = df[0] / self.mass, df[1] / self.mass
+        self.deltav(dv)
+
     def deltav(self, dv):
         v_x, v_y = self.velocity
         v_x, v_y = v_x + dv[0], v_y + dv[1]
@@ -85,9 +90,18 @@
         v_x, v_y = self.velocity
         b_x = 1.0 + self.bounce_factor[0] * other.bounce_factor[0]
         b_y = 1.0 + self.bounce_factor[1] * other.bounce_factor[1]
-        v_x = (1.0 - normal[0] * b_x) * v_x
-        v_y = (1.0 - normal[1] * b_y) * v_y
-        self.velocity = v_x, v_y
+        dv_x = - normal[0] * b_x * v_x
+        dv_y = - normal[1] * b_y * v_y
+
+        if other.mobile:
+            total_mass = self.mass + other.mass
+            f_self = self.mass / total_mass
+            f_other = other.mass / total_mass
+
+            self.deltav((dv_x * f_self, dv_y * f_self))
+            other.deltav((- dv_x * f_other, - dv_y * f_other))
+        else:
+            self.deltav((dv_x, dv_y)) # oof
 
     def update(self):
         pass # only called in wants_update = True