Isn't it
wonderful to move through the virtual world in a 3D game? The way you
can walk into walls and slide along them as you freely turn  withoug
getting your rail gun stuck between you and the wall? The way you and
your opponent can run at each other, swords drawn, actually stop when
you reach each other, and can then back away without worrying about your
shield getting caught in his chain mail? The way you can circle strafe
your opponent over uneven ground without ever having to worry about tripping?
In the real world you would have to worry about this kind of stuff…
As developers we can't afford to do collision detection between every
polygon of a character and their environment on even the best current
hardware; if we did, not only would our games be painfully slow, but we'd
also run into problems like those described above. Usually a decent spatial
partitioning system and the use of spheres to bound complex, mobile objects
let us perform collision detection routines that are very fast and only
involve the minimum number of polygons. Not only is it faster, but it
helps avoid entangling both player characters and nonplayer characters
in the environment and each other.
Then add the radii of the two circles together. If the sum of the radii
is greater than or equal to Dist, then the circles are touching.
Since multiplications are less computationally expensive than square roots,
you should speed up this code by not performing the square root when calculating
the distance, and instead square the sum of the radii. The code below
shows a sample implementation using this shortcut.
double deltaXSquared = A.x  B.x; // calc. delta X
deltaXSquared *= deltaXSquared; // square delta Xdouble deltaYSquared = A.y  B.y; // calc. delta Y
deltaYSquared *= deltaYSquared; // square delta Y// Calculate the sum of the radii, then square it
double sumRadiiSquared = A.radius + B.radius;
sumRadiiSquared *= sumRadiiSquared;if(deltaXSquared + deltaYSquared <= sumRadiiSquared){
// A and B are touching
}
Your Break: Collision between a moving circle and a stationary circle
For this problem you are given a circle A that is moving
through a virtual world. Over a finite period of time, which we'll call
t, A moves a certain distance in a certain direction.
We'll represent this movement by a vector V. Also in this
virtual world is a circle B that is not moving. The problem
is to figure out if A comes into contact with B
while it is moving along V, and if it does, by what amount
do we have to shorten V so that A comes to rest just at
the point of contact with B? An illustration of the problem
is shown in figure 2.


Figure 2: Illustration of the DynamicStatic Collision Problem 
Leveraging
Stationary Collision
The first, and most obvious solution to this would be to use the stationary
collision method described above on the changing destination location
of circle A. In other words, move circle A
to the end of the movement vector and then test it's new position for
collisions. If A is in contact with another circle in its
new location, you have two choices. You could do a recursive back off,
where you shorten the movement vector and try again until A
and B are not touching. Or you could simply not move A.
These methods have several problems.
If you went with the recursive back off solution, you could potentially
eat up a lot of CPU time trying to make the collision appear accurate.
Or, you could set a limit on the number or retries, reducing the computations
to an acceptable load but then leaving you potentially with the same problems
with the other option…
You could not move the circle at all. It's really cheap to do, but nothing
would ever seem to really touch. In the game, circle A would
move towards a static circle B over several frames until
it's new position intersected with B. Then it would appear
to just stop dead some amount before it would have hit B,
as if the static circle B had some kind of invisible force
field built around it. Assuming you are doing collision detection based
on frame rate, the effects would be more noticeable as the frame rate
drops.
Finally, if A is moving fast, it is possible that A's
final destination is on the other side of B, but not touching
it. You would have to perform additional checks to make sure A did not
pass through any other object.
Clipping
the Movement Vector
A better approach would be to use the movement vector V
to represent the circle as it moves through the world. Now all the collision
detection is simplified down to a vector tested against a static circle.
This approach does one test to see if V touches B.
If it does not collide then you are done testing.
This solution has problems as well. Consider the situation shown in figure
3. The movement vector V from the center of A
does not come into contact with anything. However, it only checks the
path traveled by the center of A for collision, and the
bottom or top could still collide with B even if the middle
does not.


Figure 3 
A possible solution to this problem is to test against two more vectors coming from the edges of the moving circle parallel to the movement vector V. While this may fix the problem described above, we can see in figure 4 that if we adjust the movement vector V to be the length of the clipped second vector, the circles will still overlap. Also, if the moving circle is larger than the static one, the static one might fit between the top and center vectors, or between the center and bottom vectors, so the collision would not be detected at all. The moving circle would appear to go right over the smaller static one. Obviously this is not the correct answer for collision detection.


Figure 4 
satish chandra 
14 Oct 2012 at 12:22 pm PST

this article really helped me. thank you.



tact swiftly 
Thank you sir for this article!
I made like 10 balls and gave them all the same sizes and masses and set them to different velocities and had them bounce acround the screen, but then if I trace out their aggregate speeds sqrt(vx*vx+vy+vy) for each ball, the total sum changes quite significantly first frame will be 70 for example then after that seems to be between 6080. Since no energy is lost how come the significant changes in energy? Is seems like a lot of difference for little float decimals to add up. Is my way of calculate total speeds wrong? =\ article from 2002 thats a decade ago ahhhhhhh lol 


nicholas ralabate 
This article is totally awesome, I used it for our game Centrifeud... though I'm still unsure if this is technically a sweep test or a separating axis test.



Kevin Connors 
This article helped me so much. I don't understand the math behind these kinds of collisions, so I was struggling to make it work in my game. Every other article I found was either too hard to follow or actually incorrect physics. Thank you!


