Changeset 78:f29b7ada68c1


Ignore:
Timestamp:
Apr 3, 2011, 10:42:33 PM (9 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Message:

Fix bouncing and tweak universal constants to show it off.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/physics.py

    r68 r78  
    1515    gravitates = True # whether gravity applies to the sprite
    1616
    17     terminal_velocity = (100.0, 100.0) # maximum horizontal and vertial speeds
    18     bounce_factor = (0.9, 0.9) # bounce factor
     17    terminal_velocity = (300.0, 300.0) # maximum horizontal and vertial speeds
     18    bounce_factor = (0.98, 1.05) # bounce factor
    1919
    2020    def __init__(self, *args, **kwargs):
     
    5757    def collide_immobile(self, immobile):
    5858        print "Collided with immobile:", self, immobile
     59        if not self.rect.colliderect(immobile.rect):
     60            print "  Collision avoived!"
     61            return
     62
    5963        v_x, v_y = self.velocity
    6064        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
    6680        self.velocity = (-v_x, -v_y)
    67         self.deltap(frac)
     81        self.deltap(frac * 1.1)
    6882        self.velocity = (b_x, b_y)
    6983
     
    7185class World(object):
    7286
    73     GRAVITY = 9.8 # m/s^2
     87    GRAVITY = 9.8 * 20.0 # m/s^2
    7488
    7589    def __init__(self):
     
    100114
    101115        # gravity
    102         dv = (0, self.GRAVITY * dt)
     116        dv = (-self.GRAVITY * 0.5 * dt, self.GRAVITY * dt)
    103117        for sprite in self._gravitators:
    104118            sprite.deltav(dv)
Note: See TracChangeset for help on using the changeset viewer.