circle-circle collision problem

I have a problem with circle-circle collision detection.I used the following algorithm

func collision(id,

 var vaP1,vaP2,dis,va1,vb1,va2,vb2,vp1,vp2,dx,dy,dt;

 if (id!


        if dis<=radius+other.radius

            //calculate the component of velocity in the direction

            if (vp1-vp2)!=0

                //move the balls back so they just touch

                //projection of the velocities in these axes

                //new velocities in these axes. take into account the mass of each ball.


                //we moved the balls back in time, so we need to move them forward

x=ball 1 x-position

y=ball 1 y-position

other.x= ball 2 x position

other.y=ball 2 y position

this algorithm works well when i have a ball image of 40 x 40 pixel and ball center is (20,20) means image consists only ball.But the problem arises when image size is 80 x 80.and ball center position is (60,60),means ball is lower right corner with radius 20. in this case there are multiple collision occur,means the portion





unable to seperate the ball /velocity does not change according to collision. I have changed the value of x which is the center of image 40,40 to 60,60 center of ball adding 20.but the result is same .Can any one tell me what is the problem.I think algorithm is correct because it works nicely in all other case and lots of people used this algorithm.problem is changing position from image center to ball center.what correction should i do for this??? or any idea.if someone want to help plz give me e-mail address so that i can send my full project.


I didnt have the mental power to digest your entire question, but here is my 2 cents on how to solve your problem

1) The simplest way to detect a circle collision with another is to check if their distance is less than the radius of the combined circles. (i might be wrong with the math, so correct me if i am wrong)

Circle c1,c2;
float distance = DISTANCE(,;
if(distance < c1.radius + c2.radius)
  // collision .. BOOOOOOM

2) Try to use accurate data types. Try not to convert floats to integers without checking overflow, underflow and decimal points. Better still, just use floats .

3) Write a log and trace through your values. See if there are any obvious maths errors .

4) Break down your code to its simplest portion. Try to remove all that velocity computation to get the simplest movements to help you debug.

I will not give you the answer that you are looking for and I am not sure someone else will. The amount of code that must be decyphered to get you the answer may not warrant the reward. What I would recommend is to losen the coupling in your algorithm. The function above is doing way too much work.

Ideally you would have a collision detection that concentrated only on the collision and not on advancing the balls. Something like function shown below and that would allow other developers to help you more easily if you still had a problem.

function(firstCircleCenterX, firstCircleCenterY, secondCircleCenterX, secondCircleCenterY, firstCircleRadius, secondCircleRadius)
    ...this code should concentrate on logic to determine collision
    ...use pythagoran theory to find distance between the two centers
    ...if the distance between the two centers is less than ((2*firstCircleRadius)+(2*secondCircleRadius) then you have a collision

    ...return true or false depending on collision

Need Your Help

PDB: exception when in console - full stack trace

python pdb

When at the pdb console, entering a statement which causes an exception results in just a single line stack trace, e.g.

How do you override the opacity of a parent control in WPF?

wpf transparency opacity

When you set the opacity on a Grid in WPF, all the child elements appear to inherit its Opacity. How can you have a child element not inherit the parent's opacity?