views:

262

answers:

2
+3  A: 

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.

Andrew Keith
A: 

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
}
FernandoZ
...uhm, I think you mean (firstCircleRadius + secondCircleRadius)
Pedery
Here-here. OP should create separate `collided` (or perhaps `didCollide`) and `bounce` functions.
outis
Ahh... yes.. (firstCircleRadius + secondCircleRadius)
FernandoZ
Yes I forgot to name the function should be named something like that Collided and return a boolean value.
FernandoZ