1
votes

While working on a recent platformer, I've come across a collision problem that occurs on a slanted platform sprite used in my game. I've tried implementing a collision mask onto the slanted sprite, to hopefully counteract this cumbersome error. But when I run the program, this jittering error occurs, where the player sprite bounces up and down on the slanted platform, and if I try implementing this slanted platform sprite like any other platform, the player will walk over the sprite like its a rectangle, when really its a triangle shaped sprite. I hope the above text made since, if it didn't I'll be more than happy to elaborate more, and any suggestions are appreciated :)

I believe the error is in one of the following code segments. If you feel you need to see more code, I'd be more than happy to provide more.

class Platform(pygame.sprite.Sprite):
    def __init__(self, id, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.tile_images = []
        self.get_images()

        self.id = int(id)
        self.image = self.tile_images[self.id]
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

        if self.id == 5 or self.id == 4: #4 and 5 are the slanted platform sprites
            self.mask = pygame.mask.from_surface(self.image)

Update section of my game class.

def update(self):
    self.player_sprite.update()

    if self.player.rect.x <= 0:
        self.player.direction = "R"
        self.player.rect.x = 0

    if self.player.vel.y > 0:
        hits = pygame.sprite.spritecollide(self.player, self.platform_list, False)
        if hits:
            lowest = hits[0]
            for hit in hits:
                if hit.rect.bottom > lowest.rect.bottom:
                    lowest = hit
            if self.player.rect.y < lowest.rect.centery:
                self.player.pos.y = lowest.rect.top + 1
                self.player.vel.y = 0
                self.player.jumped = False

Player update section, maybe a gravity error?

def update(self):
    self.animate()

    self.acc.x += self.vel.x * PLAYER_FRICTION
    self.vel += self.acc
    if abs(self.vel.x) < 0.1:
        self.vel.x = 0
    self.pos += self.vel + 0.5 * self.acc
    self.rect.midbottom = self.pos
    self.acc = vec(0, PLAYER_GRAV)
1

1 Answers

0
votes

I'm not very familliar with pygame masks but i'll give it a try.

 hits = pygame.sprite.spritecollide(self.player, self.platform_list, False)

tests collisions using the mask of your slanted platform but your collisions handling code is using its rect :

 self.player.pos.y = lowest.rect.top + 1

The jittering comes from here. Your player is going all the way to the mask but is sent back at the rect after collision handling, which is higher than the mask.