Circle-circle collision is detected by comparing center distance to sum of radii
Two circles collide (overlap) when the distance between their centers is less than the sum of their radii. In Processing, use the built-in dist(x1,y1,x2,y2) function to calculate center-to-center distance. Subtract both radii from this distance: if the result is negative, the circles are overlapping. The magnitude of the negative value is the overlap depth. This computation runs for every pair of objects every frame, making it O(n²) per frame — manageable for tens of objects, expensive for thousands. The overlap value can drive visual or behavioral responses: fading, bouncing, spawning new geometry at the collision midpoint.
Examples
Overlap test: float dis = dist(x, y, otherCirc.x, otherCirc.y); float overlap = dis - radius - otherCirc.radius; if (overlap < 0) { // circles are touching }. Drawing a circle at the intersection midpoint: ellipse((x+otherCirc.x)/2, (y+otherCirc.y)/2, -overlap, -overlap);
Assessment
Given a Processing class Ball with x, y, radius properties, write the collision detection code to determine if two Ball instances are overlapping, and describe what visual response you would add.