Circle Collision

Yet another simple type of collision, this deals with mapping out the distance between the centers of two circles to detect a collision. Its algorithm goes as follows:

circle1 = {radius: 20, x: 5, y: 5};circle2 = {radius: 12, x: 10, y: 5};dx = circle1.x - circle2.x;dy = circle1.y - circle2.y;distance = Math.sqrt(dx * dx + dy * dy);if (distance < circle1.radius + circle2.radius) { // Circle Collision!}

As you can observe here, we have two circles. We then take their individual distances of x and y coordinates. After that, we take the square root of the sum of their squares. This is just the simple formula to calculate the distance between two circles. Then in our testing condition, we check if the distance is less than the ...

Get Learning Android Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.