Bear in mind that the collision between point P and line AB will only occur in two spots along that line. The x coord of one collision will be the same as the x coord of P (Px). The y coord of the second collision will be the same as the y coord of P (Py). As both the point and the line moves, these coords will chage, but the relationship remains the same: The collisions will occur at (Px, Uy) and (Ux, Py), where U is an unknown. U can be thought of, now, as a coord, U(x,y), which must be solved.

You first need the gradient of that line:

m = difference in y / difference in x

m = (By - Ay) / (Bx - Ax)

Then, to get Ux, you would run through all the points between Ay and Py, adding the gradient, m, to an initial value of Ax (example in BASIC):

Ux = Ax

For i = Ay to Py

Ux = Ux + m

Next i

As you should see, this is the same as multiplying the difference between Ay and Py by m and adding the result to Ax. This gives the following formula:

Ux = Ax + (Py - Ay)m

or, as a coded equation:

Ux = Ax + ((Py - Ay) * m)

You can now adapt this function to find Uy:

Px = Ax + (Uy - Ay)m

Px - Ax = (Uy - Ay)m

(Px - Ax) / m = Uy - Ay

((Px - Ax) / m) + Ay = Uy

Therefore, in longhand form:

Collision #1 = [Px, (Ax + ((Py - Ay) * ((By - Ay)/(Bx - Ax)))]

Collision #2 = [((Px - Ax) / ((By - Ay) / (Bx - Ax)) + Ay), Py]

Now all you need to do is clean up the code a little:

If Px is not within the boundaries of AB, Collision #1 will never occur

If Py is not within the boundaries of AB, Collision #2 will never occur

If By = Ay then the gradient calculation will cause a divide by zero error; get around this

by making Ay = Ay + 0.001 (it will cause slight errors in your results but it should not

be noticable on screen).

Hope this helps you get started.

J.

(NB: Depending on the screen coord layout, you may find you have to swap/invert the method for finding the gradient, e.g., instead of m = (By - Ay) / (Bx - Ax):

m = (Bx - Ax) / (By - Ay) or

m = -(By - Ay) / (Bx - Ax) or

m = -(Bx - Ax) / (By - Ay) etc

Use some trial and error to get this to work to your liking)