Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I want a contact detection algorithm to detect collision between 2 ball (2D). I used this algorithm but it doesn't work well, Please Help me.


C#
//in Ball Class:

public bool Collides(Balls otherSprite)
{
    // Check if two Ball collide
    if (this.rectangle.X + this.texture2D.Width > otherSprite.rectangle.X &&
    this.rectangle.X < otherSprite.rectangle.X + otherSprite.texture2D.Width &&
    this.rectangle.Y + this.texture2D.Height > otherSprite.rectangle.Y &&
    this.rectangle.Y < otherSprite.rectangle.Y + otherSprite.texture2D.Height)
        return true;
    else
        return false;
}


if (Balls[0].Collides(Balls[1]))
{
    Vector2 tempVelocity = Balls[0].velocity;
    Balls[0].velocity = Balls[1].velocity;
    Balls[1].velocity = tempVelocity;
}
Posted
Updated 3-Mar-13 23:45pm
v6

Try using the Rectangle.Intersect Method[^].
 
Share this answer
 
Comments
Yossi Yaari 4-Mar-13 13:21pm    
you meant IntersectsWith(...) of course
Richard MacCutchan 4-Mar-13 13:41pm    
No, I meant Intersects, but your suggestion is also worth looking at. What I was really aiming for, was to get the questioner to make use of the documentation.
Yossi Yaari 5-Mar-13 0:50am    
OK
Sergey Alexandrovich Kryukov 15-Mar-13 14:52pm    
In case of rectangular scene, even of the ball is round, not rectangular, it works, if you consider the trivial geometry of the game. My 5. Simple and correct.
—SA
Well, if you want to detect collision between two balls then the condition should be (subject to Sergey's approval :-D).
C#
(XC0-XC1)*(XC0-XC1)+(YC0-YC1)*(YC0-YC1) <= R1*R1+R2*R2+2*R1*R2


That is distance between the two centers is less than (or equal) to sum of their radii
 
Share this answer
 
Comments
Yossi Yaari 4-Mar-13 13:13pm    
I think he means circles or 2D balls.
in any case your solution for calculating distance between centers is correct.

I wonder if it is worth testting Rectangle.Intersect in advance?
CPallini 4-Mar-13 15:56pm    
I don't know: Intersect is (hopefully) a fast method. However multiplication too is relatively fast.
Yossi Yaari 5-Mar-13 0:53am    
the best reason I think to do so is to minimize chance for error.
if the rectangles don't intersect - their not close enough. so very small chance for significant false positive answer on collision, if there is an error in the calculation.
CPallini 5-Mar-13 3:49am    
I would use it for speeding-up the process: if rectangles don't intersect then you may skip other checks.
Yossi Yaari 5-Mar-13 14:21pm    
that is of course correct in general.
and here the mathematical solution is very light weight. so I'm not sure how much it will improve performance. as you your self commented.
I think the best solution combines both comments above, and should look something like this:

C#
if( ! Ball1.IntersectsWith(Ball2))
{
   return false;
}

double Ball1CenterX = (Ball1.Right - Ball1.Width)/2;
double Ball2CenterX = (Ball2.Right - Ball2.Width)/2;
double Ball1CenterY = (Ball1.Bottom - Ball1.Height)/2;
//double Ball2CenterY = (Ball1.Bottom - Ball1.Height)/2;    // oops! (Ball1 ?) [MTH: fixed below]
double Ball2CenterY = (Ball2.Bottom - Ball2.Height)/2;

double Ball1Radious = Ball1.Width/2;
double Ball2Radious = Ball2.Width/2;

return (Ball2CenterX - Ball1CenterX)^2 + (Ball2CenterY - Ball1CenterY)^2 
         <= 
        (Ball1Radious + Ball2Radious)^2;   // [MTH: fixed. don't "times 2"]
 
Share this answer
 
v3
Comments
CPallini 5-Mar-13 4:01am    
I think you should fix your last statement: collision should be true when distance is less than or equal to sum of radii.
Morevoer sum of radii squared should include the (2 * Ball1Radious * Ball2Radious) term.
Yossi Yaari 5-Mar-13 14:19pm    
I think I fixed. thanks for the comment
Matt T Heffron 5-Mar-13 17:48pm    
I just fixed typos in the value of Ball2CenterY assignment.
Also, you do NOT need to multiply the sum of the radii by 2 (either before or after squaring).
(And, of course, ^2 is not the c# way to square a value.)
Yossi Yaari 10-Mar-13 11:40am    
:-)
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900