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));
float cc = Vector2.Dot(normA,normA)-(rad*rad);
// 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?