[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Finding the exact point of collision between a circle and a line



Hello,

If you know the distance it's a good deal simpler.  It's quite easy to find and store, if you have the position.  It looks like you can use line_segment_point_distance().

Let d1 be the distance to the line at t0.
Let d2 be the distance to the line at t1.
Let x be the spell's position in x.
Let y be the spell's position in y.
Let xD be the speed in the x direction.  It is linear.
Let yD be the speed in the y direction.  It is linear.

if d2 < radius: #the spell hit the line
    #fraction of frame at collision
    #I figured this out through linear interpolation.  Given constant time and known distances,
    #here's how you find an imaginary frame where distance = radius in the middle.
    portion = float(d1-radius)/float(d1-d2) #multiply by 100 to get % of time until next frame
    position_of_collision = [x + xD*portion,
                                      y + yD*portion]

I hope this is what you're looking for.
Cheers,
Ian