# How do I make collision detection more effective?

In the game I am developing, there are 220 circles that are not moving. One of the circles is moving and in every update, it iterates through an ArrayList that contains all the circles and check if it collides with them and if it does, then its velocity is set to 0.

Sometimes, it works perfectly. However,most of the time, when the moving circle collides with another circle, it stops late sometimes when the ball is already inside the other circle. How can I deal with this?

I am using LibGDX with Java.

Your problem is that you are checking for a collision and then stopping, this means that if your frame places you to far into an object you are now stuck. To solve this you have to solve the equation of exactly where both object were when they hit one another.

So you have to find where the following is true:

|t*v1+p1-t*v2+p2)|=|r|


where t is time v is the velocity r is the radius of the circa;e and p is the position.

So some math:

|t*v1+p1-t*v2+p2|=|r|

|t*(v1+p1-v2-p2)|=|r|

t=|r*(v1+p1-v2-p2)|


(in case you don't know || means length as in Pythagorus a^2+b^2=c^2)

you now have the exact moment of collision and can multiple calculate the position of both object at the moment of collision.

That should at least solve your stickiness problem, you can go here: Efficiently detecting objects inside multiple radius to solve your efficiency problem in order to also solve your fps problem.

A simpler way, that will also speed up your program, is to check if your circle is moving towardas the other circle before check for a possible collision. You can do in it in this way:

vector2 p;
p.x = other.position.x - your.position.x;
p.y = other.position.y - your.position.y;
if(p.x*yourSpeed.x + p.y*yourSpeed.y > 0
{
//check for collision;
}


look at this: http://compsci.ca/v3/viewtopic.php?t=14897