Changeset 66:12ec95a2e8ea


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

Bouncing.

Location:
skaapsteker
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/constants.py

    r59 r66  
    1010DEBUG = True
    1111
     12EPSILON = 1e-10
     13
    1214# Layer defination
    1315class Layers(object):
  • skaapsteker/physics.py

    r59 r66  
    88import pygame
    99import time
    10 from constants import DEBUG, Layers
     10from constants import DEBUG, EPSILON
    1111
    1212class Sprite(pygame.sprite.DirtySprite):
     
    2626        self.dirty = 1
    2727        self.blendmode = 0
    28         self._layer = Layers.BACKGROUND
     28
     29    def init_pos(self):
     30        self._float_pos = self.rect.topleft
    2931
    3032    def draw_debug(self, surface):
     
    4345    def deltap(self, dt):
    4446        v_x, v_y = self.velocity
     47        f_x, f_y = self._float_pos
    4548        d_x, d_y = v_x * dt, v_y * dt
    46         self.rect.move_ip(d_x, d_y)
     49        f_x, f_y = f_x + d_x, f_y + d_y
     50        self._float_pos = f_x, f_y
     51        self.rect.topleft = int(f_x), int(f_y)
     52
     53    def collide(self, other):
     54        print "Collided:", self, other
     55
     56    def collide_immobile(self, immobile):
     57        print "Collided with immobile:", self, immobile
     58        v_x, v_y = self.velocity
     59        clip = self.rect.clip(immobile.rect)
     60        frac_x = clip.width / abs(v_x) if abs(v_x) > EPSILON else 0.0
     61        frac_y = clip.height / abs(v_y) if abs(v_y) > EPSILON else 0.0
     62        frac = max(frac_x, frac_y)
     63        self.velocity = (-v_x, -v_y)
     64        self.deltap(frac)
    4765
    4866
     
    5472        self._all = pygame.sprite.LayeredUpdates()
    5573        self._mobiles = pygame.sprite.Group()
     74        self._immobiles = pygame.sprite.Group()
    5675        self._gravitators = pygame.sprite.Group()
    5776        self._last_time = None
    5877
    5978    def add(self, sprite):
     79        sprite.init_pos()
    6080        self._all.add(sprite)
    6181        if sprite.mobile:
    6282            self._mobiles.add(sprite)
     83        else:
     84            self._immobiles.add(sprite)
    6385        if sprite.gravitates:
    6486            self._gravitators.add(sprite)
     
    83105
    84106        # check for collisions
    85         for sprite1, sprites in pygame.sprite.groupcollide(self._mobiles, self._all, False, False).iteritems():
    86             for sprite2 in sprites:
    87                 if sprite1 is sprite2:
    88                     continue
     107        collisions = []
     108        collide = collisions.append
     109        for sprite1 in self._mobiles.sprites():
     110            spritecollide = sprite1.rect.colliderect
     111            for sprite2 in self._mobiles.sprites():
     112                if id(sprite1) < id(sprite2) and spritecollide(sprite2):
     113                    collide((sprite1, sprite2))
     114            for sprite2 in self._immobiles.sprites():
     115                if spritecollide(sprite2):
     116                    collide((sprite1, sprite2))
     117        self.dispatch_collisions(collisions)
     118
     119    def dispatch_collisions(self, collisions):
     120        for s1, s2 in collisions:
     121            if not s2.mobile:
     122                s1.collide_immobile(s2)
     123            else:
     124                s1.collide(s2)
    89125
    90126    def draw(self, surface):
Note: See TracChangeset for help on using the changeset viewer.