Changeset 121:5f5e43391395


Ignore:
Timestamp:
Apr 4, 2011, 8:37:59 PM (9 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Message:

Ramming speed, Mr Sulu\!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/physics.py

    r117 r121  
    2020    terminal_velocity = (300.0, 300.0) # maximum horizontal and vertial speeds (pixels / s)
    2121    bounce_factor = (0.95, 0.95) # bounce factor
     22    mass = 1.0 # used for shared collisions and applying forces
    2223
    2324    # collision attributes
     
    5253        pygame.draw.rect(surface, self.get_debug_color(), self.rect, 1)
    5354
     55    def deltaf(self, df):
     56        dv = df[0] / self.mass, df[1] / self.mass
     57        self.deltav(dv)
     58
    5459    def deltav(self, dv):
    5560        v_x, v_y = self.velocity
     
    8691        b_x = 1.0 + self.bounce_factor[0] * other.bounce_factor[0]
    8792        b_y = 1.0 + self.bounce_factor[1] * other.bounce_factor[1]
    88         v_x = (1.0 - normal[0] * b_x) * v_x
    89         v_y = (1.0 - normal[1] * b_y) * v_y
    90         self.velocity = v_x, v_y
     93        dv_x = - normal[0] * b_x * v_x
     94        dv_y = - normal[1] * b_y * v_y
     95
     96        if other.mobile:
     97            total_mass = self.mass + other.mass
     98            f_self = self.mass / total_mass
     99            f_other = other.mass / total_mass
     100
     101            self.deltav((dv_x * f_self, dv_y * f_self))
     102            other.deltav((- dv_x * f_other, - dv_y * f_other))
     103        else:
     104            self.deltav((dv_x, dv_y)) # oof
    91105
    92106    def update(self):
Note: See TracChangeset for help on using the changeset viewer.