Changeset 144:6b488e1351a5


Ignore:
Timestamp:
Apr 5, 2011, 1:46:13 PM (9 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Message:

Buggy ground implementation. Make the world less bouncy

Location:
skaapsteker
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/physics.py

    r128 r144  
    1818    mobile = True # whether the velocity may be non-zero
    1919    gravitates = True # whether gravity applies to the sprite
    20     terminal_velocity = (300.0, 300.0) # maximum horizontal and vertial speeds (pixels / s)
     20    terminal_velocity = (450.0, 450.0) # maximum horizontal and vertial speeds (pixels / s)
    2121    bounce_factor = (0.95, 0.95) # bounce factor
    2222    mass = 1.0 # used for shared collisions and applying forces
     
    3535    debug_color = (240, 0, 0)
    3636
     37    is_ground = False  # We special case collisions with ground objects
     38
    3739    def __init__(self, *args, **kwargs):
    3840        super(Sprite, self).__init__(*args, **kwargs)
     41        self.on_ground = False
    3942        self.velocity = (0.0, 0.0)
    4043        self.rect = pygame.Rect(0, 0, 10, 10) # sub-classes should override
     
    100103        dv_x = - normal[0] * b_x * v_x
    101104        dv_y = - normal[1] * b_y * v_y
     105
     106        if normal == (0, 1) and other.is_ground and v_y > 0 and self.collide_rect.top < other.collide_rect.top:
     107            # Colliding with the ground from above is special
     108            self.on_ground = True
     109            dv_y = -v_y
    102110
    103111        if other.mobile:
     
    187195        dv = self.GRAVITY[0] * dt, self.GRAVITY[1] * dt
    188196        for sprite in self._gravitators:
    189             sprite.deltav(dv)
     197            if sprite.on_ground:
     198                sprite.deltav((dv[0], 0.0))
     199            else:
     200                sprite.deltav(dv)
    190201
    191202        # friction
     
    199210            collisions = []
    200211            for other in self._collision_groups[sprite.collision_layer]:
    201                 if sprite_collides(other) and sprite.check_collides(other):
     212                if sprite_collides(other.collide_rect) \
     213                        and sprite.check_collides(other):
    202214                    collisions.append(other)
    203215            if collisions:
    204216                self._backout_collisions(sprite, collisions, dt)
     217            if sprite.on_ground:
     218                # Check if we are still in contact with the ground
     219                contact_rect = sprite.collide_rect.move((0, 1))
     220                collides = contact_rect.colliderect
     221                still_on_ground = False
     222                for other in self._collision_groups[sprite.collision_layer]:
     223                    if other.is_ground and collides(other.collide_rect):
     224                        still_on_ground = True
     225                        break
     226                sprite.on_ground = still_on_ground
     227            else:
     228                # Are we currently in contact with the groun
     229                contact_rect = pygame.Rect(
     230                        (sprite.collide_rect.left, sprite.collide_rect.bottom -1),
     231                        (sprite.collide_rect.width, 1))
     232                collides = contact_rect.colliderect
     233                for other in self._collision_groups[sprite.collision_layer]:
     234                    if other.is_ground and collides(other.collide_rect):
     235                        sprite.on_ground = True
     236                        break
     237
    205238
    206239        # call update methods
  • skaapsteker/sprites/base.py

    r127 r144  
    7272    gravitates = False
    7373    collides_with = set([PC_LAYER, MONSTER_LAYER])
     74    is_ground = True
     75    bounce_factor = (0.0, 0.0)
    7476
    7577    def __init__(self, pos, image):
  • skaapsteker/sprites/player.py

    r122 r144  
    4040            self._animation_frame = 0.0
    4141        if self.rect:
    42             cur_pos = self.rect.topleft
     42            cur_pos = self.collide_rect.topleft
    4343        else:
    4444            cur_pos = (0, 0)
     
    4646        self.rect = self.image.get_rect()
    4747        self.collide_rect = self.image.get_bounding_rect(1)
    48         self.rect_offset = self.collide_rect.left - self.rect.left, self.collide_rect.top - self.rect.top
    49         self.rect.topleft = cur_pos
    50         self.collide_rect.topleft = cur_pos[0] + self.rect_offset[0], cur_pos[1] + self.rect_offset[1]
    51 
     48        self.rect_offset = self.rect.left - self.collide_rect.left, self.rect.top - self.collide_rect.top
     49        self.collide_rect.topleft = cur_pos
     50        self.rect.topleft = cur_pos[0] + self.rect_offset[0], cur_pos[1] + self.rect_offset[1]
     51        self.init_pos()
    5252
    5353    def update(self):
     
    6666    def set_pos(self, pos):
    6767        self.starting_tile_pos = pos
    68         self.collide_rect.topleft = pos[0] * TILE_SIZE[0] + self.rect_offset[0], pos[1] * TILE_SIZE[1] + self.rect_offset[1]
    69         self.rect.topleft = pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1]
     68        self.rect.topleft = pos[0] * TILE_SIZE[0] + self.rect_offset[0], pos[1] * TILE_SIZE[1] + self.rect_offset[1]
     69        self.collide_rect.topleft = pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1]
    7070
    7171    def action_left(self):
     
    8282
    8383    def action_up(self):
    84         self.deltav((0.0, -100.0))
     84        if self.on_ground:
     85            self.deltav((0.0, -350.0))
     86            self.on_ground = False
    8587
    8688    def action_down(self):
Note: See TracChangeset for help on using the changeset viewer.