Search code examples
pythonpygamecollision

My collision detection is not working properly


I was programming a game in python using the pygame and math modules. I wrote these codes for doing a collision detection (I made 5 obstacles with which I want my player to collide) but the problem was during playing the game, sometimes it works and sometimes it doesn't.

These are the collision functions I defined

def collide1(binX, binY, playerX, playerY):
    distance = math.sqrt(math.pow(binX - playerX, 2) + math.pow(binY - playerY, 2))
    if distance == 27:
        return True
    else:
        return False


def collide2(snowX, snowY, playerX, playerY):
    distance = math.sqrt(math.pow(snowX - playerX, 2) + math.pow(snowY - playerY, 2))
    if distance == 27:
        return True
    else:
        return False


def collide3(glacierX, glacierY, playerX, playerY):
    distance = math.sqrt(math.pow(glacierX - playerX, 2) + math.pow(glacierY - playerY, 2))
    if distance == 27:
        return True
    else:
        return False


def collide4(boardX, boardY, playerX, playerY):
    distance = math.sqrt(math.pow(boardX - playerX, 2) + math.pow(boardY - playerY, 2))
    if distance == 27:
        return True
    else:
        return False


def collide5(iceX, iceY, playerX, playerY):
    distance = math.sqrt(math.pow(iceX - playerX, 2) + math.pow(iceY - playerY, 2))
    if distance == 27:
        return True
    else:
        return False

In the while loop

# Collision Detection
collision1 = collide1(binX, binY, playerX, playerY)
collision2 = collide2(snowX, snowY, playerX, playerY)
collision3 = collide3(glacierX, glacierY, playerX, playerY)
collision4 = collide4(boardX, boardY, playerX, playerY)
collision5 = collide5(iceX, iceY, playerX, playerY)

if collision1:
    print("You have collided!")
elif collision2:
    print("You have collided!")
elif collision3:
    print("You have collided!")
elif collision4:
    print("You have collided!")
elif collision5:
    print("You have collided!")

Please tell me where am I doing it wrong.


Solution

  • Actually, you are just checking if the player is touching an obstacle, but you will miss the collision if the player intersects the obstacle. You have to evaluate if distance <= 27 rather than distance == 27. Furthermore it is completely sufficient to implement 1 function for the collision test.

    def collide(x1, y1, x2, y2):
        distance = math.sqrt(math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2))
        if distance <= 27:
            return True
        else:
            return False
    

    The function can be further simplified:

    def collide(x1, y1, x2, y2):
        distance = math.hypot(x1 - x2, y1 - y2)
        return distance <= 27
    

    Use a loop to do the collision test:

    obstacles = [(binX, binY), (snowX, snowY), (glacierX, glacierY), (boardX, boardY), (iceX, iceY)]
    
    for x, y in obstacles:
        collision = collide(x, y, playerX, playerY)
        if collision:
            print("You have collided!")