Changeset 122:51bcc909873d


Ignore:
Timestamp:
Apr 4, 2011, 8:39:42 PM (9 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Message:

Saner, buggier collision rectangles

Location:
skaapsteker
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/physics.py

    r121 r122  
    3838        self.velocity = (0.0, 0.0)
    3939        self.rect = pygame.Rect(0, 0, 10, 10) # sub-classes should override
     40        self.collide_rect = pygame.Rect(0, 0, 10, 10) # rectangle we use for collisions
    4041        self.image = pygame.Surface((10, 10))
    4142        self.image.fill((0, 0, 200))
     
    5152
    5253    def draw_debug(self, surface):
    53         pygame.draw.rect(surface, self.get_debug_color(), self.rect, 1)
     54        pygame.draw.rect(surface, (240, 0, 0), self.collide_rect, 1)
     55        pygame.draw.rect(surface, self.get_debug_color(), self.collide_rect, 1)
    5456
    5557    def deltaf(self, df):
     
    6870
    6971    def deltap(self, dt):
     72        old_pos = self.rect.topleft
    7073        v_x, v_y = self.velocity
    7174        f_x, f_y = self._float_pos
     
    7477        self._float_pos = f_x, f_y
    7578        self.rect.topleft = int(f_x), int(f_y)
     79        delta_pos = self.rect.left - old_pos[0], self.rect.top - old_pos[1]
     80        self.collide_rect.move_ip(delta_pos)
    7681
    7782    def check_collides(self, other):
     
    146151
    147152    def _backout_collisions(self, sprite, others, dt):
    148         frac, normal, idx = 0.0, None, None
     153        frac, normal, idx = -1.0, None, None
    149154        v_x, v_y = sprite.velocity
    150155        abs_v_x, abs_v_y = abs(v_x), abs(v_y)
    151156
    152157        for i, other in enumerate(others):
    153             clip = sprite.rect.clip(other.rect)
     158            clip = sprite.collide_rect.clip(other.collide_rect)
    154159            # TODO: avoid continual "if abs_v_? > EPSILON"
    155160            frac_x = clip.width / abs_v_x if abs_v_x > EPSILON else dt
     
    184189        for sprite in self._mobiles:
    185190            sprite.deltap(dt)
    186             sprite_collides = sprite.rect.colliderect
     191            sprite_collides = sprite.collide_rect.colliderect
    187192            collisions = []
    188193            for other in self._collision_groups[sprite.collision_layer]:
  • skaapsteker/sprites/base.py

    r117 r122  
    2828        self.starting_tile_pos = pos
    2929        self.rect = self.image.get_rect(topleft=(pos[0]*TILE_SIZE[0], pos[1]*TILE_SIZE[1]))
     30        self.collide_rect = self.image.get_rect(topleft=(pos[0]*TILE_SIZE[0], pos[1]*TILE_SIZE[1]))
    3031        self._layer = Layers.PLAYER
    3132        self.setup(**opts)
     
    5859        self.tile_pos = pos
    5960        self.image = image
    60         self.rect = Rect((pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1]), TILE_SIZE)
     61        self.collide_rect = self.image.get_bounding_rect(1)
     62        self.rect = self.image.get_rect()
     63        self.rect_offset = self.collide_rect.left - self.rect.left, self.rect.top - self.rect.top
     64        self.collide_rect.topleft = pos[0] * TILE_SIZE[0] + self.rect_offset[0], pos[1] * TILE_SIZE[1] + self.rect_offset[1]
     65        self.rect.topleft = pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1]
    6166
    6267    def get_debug_color(self):
  • skaapsteker/sprites/player.py

    r120 r122  
    1919        Sprite.__init__(self)
    2020        self.image = None
     21        self.rect = None
    2122        self._image_dict = {}
    2223        self._animation_frame = 0.0
     
    3839        if self._animation_frame >= len(images):
    3940            self._animation_frame = 0.0
     41        if self.rect:
     42            cur_pos = self.rect.topleft
     43        else:
     44            cur_pos = (0, 0)
    4045        self.image = images[int(self._animation_frame)]
     46        self.rect = self.image.get_rect()
     47        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
    4152
    4253    def update(self):
     
    5263        self.facing = new_facing
    5364
     65
    5466    def set_pos(self, pos):
    5567        self.starting_tile_pos = pos
    56         self.rect = self.image.get_rect(topleft=(pos[0]*TILE_SIZE[0], pos[1]*TILE_SIZE[1]))
    57         self.rect.inflate_ip(-10, -10)
     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]
    5870
    5971    def action_left(self):
Note: See TracChangeset for help on using the changeset viewer.