sub distance(px,py, x1,y1, x2,y2)
local dx,dy,clip_line
dx = x2 - x1
dy = y2 - y1
clip_line = ((px-x1)*dx + (py-y1)*dy) / (dx*dx + dy*dy)
closest_point_x = (x1 + clip_line*dx)
closest_point_y = (y1 + clip_line*dy)
dx = px - closest_point_x
dy = py - closest_point_y
return sqrt(dx*dx + dy*dy)
end sub
that subroutine will calculate the distance between a point and a straight line where
px,py = point x,y
x1,y1,x2,y2 = two points that lie along that line
the subroutine assumes that the line is infinate in length and will return the shortest distance to that infinate line. if the value returned by the subroutine is less than ball radius then closest_point_x and closest_point_y must be used to check if the clostest point is beyond the actual extents of the true line.
Thats actually the easy part the hard part is changing the movement vector of the circle so that the ball bounces off both edges and corners in a realistic manor.
Im thinking that the line of action is the line between the circles center and the edge it collides with, the vector has to be mirrored accross this line somehow, This holds true for colliding with surfaces too but the difference is that the line of action is drawn between the circles center point and the point along the surface that first touches the circle