On this page
How to use it#
- Choose a collision type: rect vs rect, circle vs circle, rect vs circle or point vs rect.
- Drag shape A or B. Drag the small square handle to resize a rectangle or change a circle's radius.
- Watch the result badge and the overlap area. The code panel shows the Pygame code for the current positions.
Keyboard: click the canvas (or Tab to it), choose which shape to move from the list, then use the arrow keys. Hold Shift for larger steps.
The four tests#
Rect vs rect. Two axis-aligned rectangles overlap when each one's left side is left of the other's right side and each one's top is above the other's bottom. Pygame's Rect.colliderect() does exactly this, and Rect.clip() returns the overlapping area, shown in yellow.
Circle vs circle. Two circles touch when the distance between their centres is no more than the sum of their radii. The dashed line shows that distance.
Rect vs circle. Find the point of the rectangle closest to the circle's centre by clamping the centre's coordinates to the rectangle's edges; if that point is within the radius, they collide. The yellow dot marks the closest point.
Point vs rect. Rect.collidepoint() checks whether a point, such as the mouse position, is inside. Points on the right and bottom edges count as outside, because a Rect's right and bottom are one past its last pixel.
Which test should I use?#
| Objects | Good choice |
|---|---|
| Boxes, tiles, platforms, UI buttons | Rect vs rect, point vs rect |
| Balls, coins, round enemies | Circle vs circle |
| A ball hitting a paddle or wall | Rect vs circle |
| Irregular sprites where precision matters | Masks (pygame.sprite.collide_mask) after a rect pre-check |
The collision detection tutorial covers masks, sprite groups and how to stop objects passing through walls.