PDA

Click to See Complete Forum and Search --> : Line Collision Detection



xDrunkCowx
10-24-2002, 04:23 PM
Hey,
I can do hittest between circles and other circles and walls thats not hard (not useing .hittest) but how do u do hittest between a ball and a line?

dnalogic
10-24-2002, 10:20 PM
If you want to find if a circle is touching a straight (infinite) line, then do the follwoing:

1. Get the gradient of the straight line.
2. Calculate the inverse of that gradient (ie the gradient of a line that would cut the first line at a right angle).
3. Work out the cross section of the original line, and our new imaginary line (the one that cuts the original line at a right angle - it MUST also pass through the centre of the circle)
4. Calculate the distance between the centre of the circle, and the point of cross-section. If this distance is less than (or equal to) the radius of the circle, then the circle is touching the line, otherwise it is not.
There is probably a more efficient way, but this wouldnt take up too many lines of code

Additional:
If you want to use a line that is not infinite, then I think (am not sure though) that you could do the following:
1. Get the distance between the centre of the circle and one of the end points of the line.
2. Get the distance between the centre of the circle and the other end point of the line.
3. Add these distances together. Call this value T.
4. Get the length of the line (the distance between the end points). Call this value L.
5. Get the radius of the circle. Call it R.
6. If T>(L + 2 x R) then the circle is not touching, otherwise it is.
--- I am not too sure about that though. It is true in some cases, and seems like it would be true (or at least nearly true) in the other cases. I havent worked it out. Another idea would be to make a triangle between the centre of the circle, and the two end points of the line, and calucate its area or some other value of a triangle. If that value is above a certain limit then the circle is not touching the line, etc.

Also:
If you want to work out which side of the line the circle is on, get the cross product (cant remember if thats what its called, but there's a common formula for working out if a series of points are clockwise or anticlockwise, that you can derive from those inequalities graphs . ewww).

xDrunkCowx
10-25-2002, 12:41 AM
Really nice post.. and i sorta understand it... but im just not getting everything.. Can anyone else help with this?