The side of the collision depends on th relatuve moving direction. The relative movment is the difference of the movement of rect1
and rect2
.
Anyway, you can try to compute the difference of the on each side and to find the distance with the minimum amount:
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"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…