circle-circle collision problem

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

func collision(id,other.id)
{

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


 if (id!=other.id)

    {
        dx=other.x-x;
        dy=other.y-y;
        dis=sqrt(sqr(dx)+sqr(dy));

        if dis<=radius+other.radius
        {
            //normalize
            dx/=dis;
            dy/=dis;

            //calculate the component of velocity in the direction
            vp1=hspeed*dx+vspeed*dy;
            vp2=other.hspeed*dx+other.vspeed*dy;

            if (vp1-vp2)!=0
            {
                dt=(radius+other.radius-dis)/(vp1-vp2);

                //move the balls back so they just touch
                x-=hspeed*dt;
                y-=vspeed*dt;
                other.x-=other.hspeed*dt;
                other.y-=other.vspeed*dt;

                //projection of the velocities in these axes
                va1=(hspeed*dx+vspeed*dy); 
                vb1=(vspeed*dx-hspeed*dy);
                va2=(other.hspeed*dx+other.vspeed*dy); 
                vb2=(other.vspeed*dx-other.hspeed*dy);

                //new velocities in these axes. take into account the mass of each ball.
                vaP1=(va1+bounce*(va2-va1))/(1+mass/other.mass);
                vaP2=(va2+other.bounce*(va1-va2))/(1+other.mass/mass);

                hspeed=vaP1*dx-vb1*dy; 
                vspeed=vaP1*dy+vb1*dx;
                other.hspeed=vaP2*dx-vb2*dy;  
                other.vspeed=vaP2*dy+vb2*dx;

                //we moved the balls back in time, so we need to move them forward
                x+=hspeed*dt;
                y+=vspeed*dt;
                other.x+=other.hspeed*dt;
                other.y+=other.vspeed*dt;
            }
        }
    }

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

x+=hspeed*dt;

y+=vspeed*dt;

other.x+=other.hspeed*dt;

other.y+=other.vspeed*dt;

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.

Answers


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(c1.center,c2.center);
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?