Advertisement

How would I...

Started by November 16, 2000 10:52 AM
3 comments, last by Xain 24 years, 2 months ago
How do you tell if a point collides with a line?
quote: Original post by Xain

How do you tell if a point collides with a line?


Check to see if that point satisfies the equation of the line:
(y1 - y2) == (slope)(x1-x2)


(x1,y1) is a point on the line.
slope is the slope of the line.
(x2,y2) is the point you are checking.

If the equation is satisfied then the point collides with the line.


----------
Andrew
Advertisement
Probably not the simplest of answers but...

line_dist = dist( l1, l2 )
dist1 = dist( point, l1 )
dist2 = dist( point, l2 )
fudge_factor = 0 // this should probably be 1 to account for rounding errors

if ( line_dist + fudge_factor >= dist1 + dist2 ) {
// collision
}
Andrew, so if x1, y1 is just a point on a line, do I have to perform this check many times to cover the whole line?
quote: Original post by Xain

Andrew, so if x1, y1 is just a point on a line, do I have to perform this check many times to cover the whole line?


No. All you need is the general equation of the line.

For example.
You know that point (x1,y1) = (3,4) is on the line.
You know that the line has a slope of 2.
General Equation: (4-y2) = 2*(3-x2).

You can use this equation to see if any point collides with that line.

(x2,y2) = (2,2)
(4-2) = 2(3-2)
2 = 2 -> OK. Collision


(x2,y2) = (3,5)
(4-5) = 2(3-3)
-1 = 0 NOT OK. no collision

Just form the general equation and you can check any point that would be anywhere on the line.

--------
Andrew

This topic is closed to new replies.

Advertisement