Python Forum
can't get collision detection to work in platform game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't get collision detection to work in platform game
#11
The floor is not the problem. You can stop collisions by setting speed to zero. You just aren't properly handling all the conditions.

It is actually quite easy. The algorithm can be summed up as:
1: Test if move results in collision
2: If collision detected, don't move
3: If collision not detected, move.
def move(x, y, xvel, yvel):
    #  Check if x+xvel results in a collision
    playerrect.x, playerrect.y = x + xvel, y
    for tile in tile_rects:
        if playerrect.colliderect(tile):
            xvel = 0  # Don't move in x direction
            break

    # Check if y+yvel results in a collision
    playerrect.x, playerrect.y = x, y + yvel
    for tile in tile_rects:
        if playerrect.colliderect(tile):
            yvel = 0  # Don't move in y direction
            break
    return x + xvel, y + yvel, yvel
You have a slight complication in the way that gravity changes your yvel. That is why velocity is returned.
while True:
    # Process all events first
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key in (K_RIGHT, K_d):
                player_speed = 5
            elif event.key in (K_LEFT, K_a):
                player_speed = -5
            elif event.key in (K_UP, K_w):
                player_y_momentum = -5
        elif event.type == KEYUP:
            if event.key in (K_RIGHT, K_d, K_LEFT, K_a):
                # Stop moving in x when key is released
                player_speed = 0
    # Update player momentum
    player_y_momentum = min(3, player_y_momentum + 0.2)

    # Check for collisions.  A collision will stop the player from moving in the direction that
    # resulted in the collision.
    playerx, playery, player_y_momentum = move(
        playerx, playery, playerspeed, player_y_momentum
    )
    # draw the background, the tiles and the player last.
Maybe the trickiest thing is order. One of your problems is you checked for collisions first, then you processed events and did your momentum calc. You want checking for collisions to be the last thing you do before moving the player. move() cannot prevent the player running into things if you ignore or override the actions taken to prevent collisions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] Problem with collision of player and enemy Nekotrooper 1 700 Dec-08-2023, 03:29 PM
Last Post: deanhystad
  [PyGame] Players not falling from the platform, and some other errors. urmom33 1 1,638 Jan-23-2023, 10:28 PM
Last Post: deanhystad
  [PyGame] Collision in not happening onizuka 3 3,426 Sep-07-2020, 11:30 AM
Last Post: metulburr
  [PyGame] No collision detection onizuka 6 3,677 Aug-18-2020, 01:29 PM
Last Post: onizuka
  Problem with collision detection... michael1789 4 3,282 Nov-12-2019, 07:49 PM
Last Post: michael1789
  Arcade Collision Problem randor 0 2,694 Oct-28-2019, 11:17 PM
Last Post: randor
  Multiple wall collision in pacman rustyjoe 4 4,117 Aug-11-2019, 08:08 AM
Last Post: rustyjoe
  drawing, moving, and collision problems (pygame) SheeppOSU 26 14,761 Apr-22-2019, 03:09 AM
Last Post: SheeppOSU
  [PyGame] Game doesn't work after pause Owenix 2 3,184 Sep-20-2018, 08:44 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020