Circle / Circle

Note: this example does not use a radius from the center of the circle, but the width of the flash moviecliip object.

I think the best use of it in a game setting would be collision for smaller entities that can fit within a circle like people, trees, upright objects like light poles, and so on.

We have two options for what happens when you run into the green sphere with your blue sphere. Either the two will push each other out of the way, or the green sphere will remain stationary while your sphere moves around it.

Choose this with a simple bool...

Circle / Square

Resolving the collision between circles and squares is somewhat similar to Circle / Circle collisions.

Unlike Circle / Circle collision this code is more helpful for when smaller objects like people run into walls or anything that can be defined in a square area such as cars, tables, or park benches.

If the circle is your character and the squares and rectangles your world tiles then the character will not be able to move in any direction in which collision is detected. This is also helpful in side scrolling platformers where the ground doesn't need to be rotated. The process for collision there is simple; if there is collision below the character then we'll get the intersection "depth", or how much overlap there is, and revert the character in the opposite direction the length of that overlap. If there is no intersection then just apply gravity and have him be "pushed" away from the edge of the platform once he begins to slide off. Thus preventing that visual quirk of a character being 95 % off a platform when using rectangles for character collision.

For non-tile based worlds, or worlds where you want to test circles against slanted surfaces that can't be defined by Axis-Aligned Bounding Boxes, I would just use circle / line collision where each surface is a line segment. (See Circle / Line collision)

One thing to keep in mind about the code that follows is that if the velocity of one of the two objects is too fast then it may cause inaccuracies. This is because it is checking for intersection between the edge of the circle and the center of the circle. This produces a rule of thumb to follow when using this approach to collision reaction.

If the velocity of the circle is greater than the radius, this is not a suitable form of collision detection.

A second problem with the current code you see here is that the circle doesn't react well to hitting multiple rectangles. Try placing the two rectangles near each other and squeezing the circle through a gap smaller than the circle. One of the rectangle checks will fail.

This second problem of course comes from the fact that we check the objects in order and the position corrections applied to the circle are not aware of each other at all. One rectangle is checked, and then another, they do not take each other into consideration at all.

You can somewhat alleviate this by re-running the collision check function several times. So you'd check rectangle 1, rectangle 2, and then rectangle 1, and 2, again. The more times you recheck this the more accurate the resolution will be. The original problem of velocity exceeding the radius will, however, remain.