简体   繁体   中英

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.

Right now you only collide if the distance is exactly 27. If you assume spheres, you can still consider them "colliding" if they are less than 27 pixels appart.

So replace all your distances with if distance <= 27: Note the less than or equals.

Another thing to note is that calculating square roots is very slow. It's much faster to check if distance_squared <= 27 * 27 then to check math.pow(distance_squared, 0.5) <= 27 .

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!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM