Mercurial > skaapsteker
changeset 78:f29b7ada68c1
Fix bouncing and tweak universal constants to show it off.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Mon, 04 Apr 2011 00:42:33 +0200 |
parents | 9af8c6c59cba |
children | fe8652a060df |
files | skaapsteker/physics.py |
diffstat | 1 files changed, 24 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/skaapsteker/physics.py Mon Apr 04 00:14:02 2011 +0200 +++ b/skaapsteker/physics.py Mon Apr 04 00:42:33 2011 +0200 @@ -14,8 +14,8 @@ mobile = True # whether the velocity may be non-zero gravitates = True # whether gravity applies to the sprite - terminal_velocity = (100.0, 100.0) # maximum horizontal and vertial speeds - bounce_factor = (0.9, 0.9) # bounce factor + terminal_velocity = (300.0, 300.0) # maximum horizontal and vertial speeds + bounce_factor = (0.98, 1.05) # bounce factor def __init__(self, *args, **kwargs): super(Sprite, self).__init__(*args, **kwargs) @@ -56,21 +56,35 @@ def collide_immobile(self, immobile): print "Collided with immobile:", self, immobile + if not self.rect.colliderect(immobile.rect): + print " Collision avoived!" + return + v_x, v_y = self.velocity clip = self.rect.clip(immobile.rect) - frac_x = clip.width / abs(v_x) if abs(v_x) > EPSILON else 0.0 - frac_y = clip.height / abs(v_y) if abs(v_y) > EPSILON else 0.0 - frac = max(frac_x, frac_y) - b_x = -v_x * self.bounce_factor[0] * immobile.bounce_factor[0] - b_y = -v_y * self.bounce_factor[1] * immobile.bounce_factor[1] + MAX_DT = 0.1 + frac_x = clip.width / abs(v_x) if abs(v_x) > EPSILON else MAX_DT + frac_y = clip.height / abs(v_y) if abs(v_y) > EPSILON else MAX_DT + + if frac_x > frac_y: + # collision in y + frac = frac_y + b_y = -v_y * self.bounce_factor[1] * immobile.bounce_factor[1] + b_x = v_x + else: + # collision in x + frac = frac_x + b_x = -v_x * self.bounce_factor[0] * immobile.bounce_factor[0] + b_y = v_y + self.velocity = (-v_x, -v_y) - self.deltap(frac) + self.deltap(frac * 1.1) self.velocity = (b_x, b_y) class World(object): - GRAVITY = 9.8 # m/s^2 + GRAVITY = 9.8 * 20.0 # m/s^2 def __init__(self): self._all = pygame.sprite.LayeredUpdates() @@ -99,7 +113,7 @@ self._last_time, dt = now, now - self._last_time # gravity - dv = (0, self.GRAVITY * dt) + dv = (-self.GRAVITY * 0.5 * dt, self.GRAVITY * dt) for sprite in self._gravitators: sprite.deltav(dv)