how to know pygame.Rect’s side that collide to other Rect?

The side of the collision depends on the relative moving direction. The side of the collision depends on the relative movement of rect1 and rect2.

Anyway, you can estimate the side by calculating the difference in object position and finding the side with the minimum distance:

dr = abs(rect1.right - rect2.left)
dl = abs(rect1.left - rect2.right)
db = abs(rect1.bottom - rect2.top)
dt = abs(rect1.top - rect2.bottom)

if min(dl, dr) < min(dt, db):
    direction = "left" if dl < dr else "right"
else:
    direction = "bottom" if db < dt else "top"

Leave a Comment