# Line segment / circle intersection - X value seems wrong?

Major Edit

I'm making a Breakout clone, and having difficulty with collision detection between a circle and line segment.

Apologies, my earlier question was a result of frustration and no sleep :)

Having read this post on collision detection I've re-written my collision code to the following:

    // Takes 2 points on a line, a circle centre, and a circle radius
// Returns true is collision, false otherwise
public Boolean DoesCollide(Vector2 a, Vector2 b, Vector2 c, float rad)
{
// First up, let's normalise our vectors so the circle is on the origin
Vector2 normA = a - c;
Vector2 normB = b - c;

Vector2 d = normB - normA;

// Want to solve as a quadratic equation, need 'a','b','c' components
float aa = Vector2.Dot(d, d);
float bb = 2 * (Vector2.Dot(normA, d));

// Get determinant to see if LINE intersects
double deter = Math.Pow(bb, 2.0) - 4 * aa * cc;
if (deter > 0)
{
// Get t values (solve equation) to see if LINE SEGMENT intersects
double t = (-bb - Math.Sqrt(deter)) / (2 * aa);
double t2 = (-bb + Math.Sqrt(deter)) / (2 * aa);
Boolean match = false;

if (0.0 <= t && t <= 1.0)
{
// Interpolate to get collision point
Vector2 collisionPoint = c + Vector2.Lerp(normA, normB, (float)t);
match = true;
}
if (0.0 <= t2 && t2 <= 1.0)
{
Vector2 collisionPoint2 = c + Vector2.Lerp(normA, normB, (float)t2);
match = true;
}
return match;
}
else
return false;
}


However, I'm getting strange results for the X components of the collision points found 'on hit' - see the following debug output:

Coll at {X:1033.931 Y:620}, t1 = 0.203759334390812, Normalised = {X:0.8576241    Y:0.5142772}
Coll2 at {X:1052.069 Y:620}, t2 = 0.409877029245551, Normalised = {X:0.8615275 Y:0.5077109}
Circ cent = {X:1043 Y:610}, rad = 13.5


As you can see, the X components are 10px either side of the circle centre, but the Y component (620) is the very topmost pixel of the top of the Breakout paddle. Surely here the X points can't be 20 pixels apart, given the location of the Y point?

• can you please add some comments to your code? what are a,b,c vectors? and what does aa,bb,cc represent? – Ali1S232 Oct 12 '11 at 6:25
• Did you check my answer here? gamedev.stackexchange.com/questions/18333/… I explain how the algorithm works in a simple way, this is generally better than try to adapt a code you don't fully understand – FxIII Oct 12 '11 at 6:52
• Apologies all, I posed this at 3am and was very frustrated. Will look at FxIII's algorithm :) – codinghands Oct 12 '11 at 10:25
• @FxIII I've read through your post and written a method to solve for t as per your post. However, I'm getting some odd results - I've updated my question above. Could you assist? – codinghands Oct 12 '11 at 11:32
• @FxIII, sorry - updated now :) – codinghands Oct 12 '11 at 11:42

You are missing a stage before calculating t0 and t1. It should look more like this:

float q; // Holds the solution to the quadratic equation
if (bb >= 0)
{
q = (-bb - Math.Sqrt(deter)) / 2;
}
else
{
q = (-bb + Math.Sqrt(deter)) / 2;
}

float t0 = q / aa;
float t1 = cc / q;


After that, make sure t0 and t1 are sorted.  I made a small error with the determinant signs, note they are now reversed.

• thanks for answering. How do you mean 'sorted' (in reference to t1 / t0)? – codinghands Oct 12 '11 at 12:48
• This kind of solution sounds new to me. I'm try to figure out its correctness... – FxIII Oct 12 '11 at 12:49
• Checkt t0 is smaller than t1, and if they are not, swap them. – Darcy Rayner Oct 12 '11 at 13:32
• What is the case here if t1 and t0 are exactly equal, yet less than 0? Determ is positive. – codinghands Oct 15 '11 at 13:58
• Also, otherwise this seems to be working a treat. Thanks to FxIII also, will upvote when I've enough rep. – codinghands Oct 16 '11 at 14:47