views:

209

answers:

2

I'm developing a game that uses a fair bit of geometry and I need to know how to find the intersection of two lines, and the distance of a point from this intersection point.

A: 

I am assuming X and Y are the beginning and endpoints of your wall, though your problem is a little vague. Using a linear equation:

y=mx+b

where m = (change in y/change in x) and b is constant

Determine 'm' and 'b' for the X-Y line (the wall) and the L-M line. Combine these two equations to determine the point where these lines intersect - you will end up with a point on the X-Y line. Determine whether this point falls within the line segment of the wall by comparing it to the endpoints.

If you figure out that the player hits the wall, then I assume you want the time when he hits the wall... so you need to find the distance traveled along the line from the player's start to the point of intersection, multiplied by the player's speed.

x = v*t (constant speed)

RMorrisey
+2  A: 

X,Y is one line, L, M is another. So first find the intersection of the two lines with standard methods, and call the intersection point P. Since P in on the line defined by L,M, we can solve for the value of t which yeilds the point P. If 0 <= t <= 1 then the player does intersect the wall at the time t that was solved for, otherwise, the wall is missed.

Paul Hsieh