Changeset 78:f29b7ada68c1
 Timestamp:
 04/03/11 22:42:33 (12 years ago)
 Branch:
 default
 Phase:
 public
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

skaapsteker/physics.py
r68 r78 15 15 gravitates = True # whether gravity applies to the sprite 16 16 17 terminal_velocity = ( 100.0, 100.0) # maximum horizontal and vertial speeds18 bounce_factor = (0.9 , 0.9) # bounce factor17 terminal_velocity = (300.0, 300.0) # maximum horizontal and vertial speeds 18 bounce_factor = (0.98, 1.05) # bounce factor 19 19 20 20 def __init__(self, *args, **kwargs): … … 57 57 def collide_immobile(self, immobile): 58 58 print "Collided with immobile:", self, immobile 59 if not self.rect.colliderect(immobile.rect): 60 print " Collision avoived!" 61 return 62 59 63 v_x, v_y = self.velocity 60 64 clip = self.rect.clip(immobile.rect) 61 frac_x = clip.width / abs(v_x) if abs(v_x) > EPSILON else 0.0 62 frac_y = clip.height / abs(v_y) if abs(v_y) > EPSILON else 0.0 63 frac = max(frac_x, frac_y) 64 b_x = v_x * self.bounce_factor[0] * immobile.bounce_factor[0] 65 b_y = v_y * self.bounce_factor[1] * immobile.bounce_factor[1] 65 MAX_DT = 0.1 66 frac_x = clip.width / abs(v_x) if abs(v_x) > EPSILON else MAX_DT 67 frac_y = clip.height / abs(v_y) if abs(v_y) > EPSILON else MAX_DT 68 69 if frac_x > frac_y: 70 # collision in y 71 frac = frac_y 72 b_y = v_y * self.bounce_factor[1] * immobile.bounce_factor[1] 73 b_x = v_x 74 else: 75 # collision in x 76 frac = frac_x 77 b_x = v_x * self.bounce_factor[0] * immobile.bounce_factor[0] 78 b_y = v_y 79 66 80 self.velocity = (v_x, v_y) 67 self.deltap(frac )81 self.deltap(frac * 1.1) 68 82 self.velocity = (b_x, b_y) 69 83 … … 71 85 class World(object): 72 86 73 GRAVITY = 9.8 # m/s^287 GRAVITY = 9.8 * 20.0 # m/s^2 74 88 75 89 def __init__(self): … … 100 114 101 115 # gravity 102 dv = ( 0, self.GRAVITY * dt)116 dv = (self.GRAVITY * 0.5 * dt, self.GRAVITY * dt) 103 117 for sprite in self._gravitators: 104 118 sprite.deltav(dv)
Note:
See TracChangeset
for help on using the changeset viewer.