   2d collision and reflection - circle vs. line
  I've written a very simple game which involves creating "lines" which balls bounce upon.I've got two basic problems, the first is collision detection.  The second is working out the bounce itself.I have solutions to both of these problems, but they are not great.Here's the rough overview of how it all works:I have a ball with position "x,y".   The radius is "r".  Each tick of the game the direction is updated with "x = x + dx" and "y = y + dy".  dx+dy are speedy/vectory things.I have a line with start-point "x1,y1", and end point "x2,y2".To test intersections I use the pythogoral distance between the center of the round ball and each point on the line - which I calculate via a Bresenham-style line function.  If the distance is smaller than the radius I detect a collision.At collision time I basically invert the "dy" + "dx" step - so the ball moves away in the opposite direction.  But I'd prefer the ball to bounce, ie. the angle of the line should influence this - problem is I don't know how.Any and all suggestions welcomeSteve   
  Here is how you reflect something off of a surface:Vector2 velocity; // the velocity vector that is to be reflected off of the surface/plane/lineVector2 normal;  // the plane or line's normalvelocity -= 2 * Vector2.DotProduct(velocity, normal) * normal; // now the velocity is reflectedHere is an example.I'm actually writing a small paper on this exact same question of yours; unfortuantely, it isn't finished.  I'd write the equations and stuff for the collision detection, but I figure it will be easier to finish the paper and give you a link (I'll try to finish it tonight).   ;Quote:Original post by skx2To test intersections I use the pythogoral distance between the center of the round ball and each point on the line - which I calculate via a Bresenham-style line function.  If the distance is smaller than the radius I detect a collision.This is very ineffective! instead use a function that finds the closest point on the line segment relative to the ball. Search for "closest point on line".Here is a code example from this forum. It finds the closest point on line segment AB relative to point P:Vector GetClosetPoint(Vector A, Vector B, Vector P){    Vector AP = P - A;    Vector AB = B - A;    float ab2 = AB.x*AB.x + AB.y*AB.y;    float ap_ab = AP.x*AB.x + AP.y*AB.y;    float t = ap_ab / ab2;    if (t < 0.0f) t = 0.0f;    else if (t > 1.0f) t = 1.0f;    Vector Closest = A + AB * t;    return Closest;}This is not only a lot faster, but you also need to know the closest point P in order to calculate the bounce correctly. The collision normal vector already mentioned earlier is simply Ball position vector - closest point vector.cheers,Michael   