Ray vs Ray (2D)

Started by
4 comments, last by Jnz86 18 years, 8 months ago
Hi, i feel so stupid, but here it goes: lets say Ray has these components: Vector2 Origin; Vector2 Direction;(normalized) then, any point on ray P = Origin + Direction*t; when i want to find where two ray's intersect, i do this: P1 = P2; Origin1 + Direction1*t1 = Origin2 + Direction2*t2; and now i am lost, i got two unknown variables(t1,t2) and only one equation... how to find t1,t2 ?
Advertisement
Ah... but you have two equations:

origin1.x + t1 * direction1.x = origin2.x + t2 * direction2.x
origin1.y + t1 * direction1.y = origin2.y + t2 * direction2.y
Assuming that t is time then you need a speed.

Origin+(Speeed*Direction)*Time;

Other wise you are always going at 1 unit per time unit.

For the two rays time will be the same.

So it is

Origin1+(Speed1*Direction1)*TIME=Origin2+(Speed2*Direction2)*TIME

And now you are solving for one unknown... Time...

Hope that helps.
------------------------------------------------------------------
Quote:
Assuming that t is time then you need a speed.

Origin+(Speeed*Direction)*Time;

Other wise you are always going at 1 unit per time unit.

For the two rays time will be the same.

So it is

Origin1+(Speed1*Direction1)*TIME=Origin2+(Speed2*Direction2)*TIME

And now you are solving for one unknown... Time...

Hope that helps.
------------------------------------------------------------------

If i understand correctly, this is more for particle collision, i don't want this, i want the WHOLE lines to collide, not just point
(it's hard to say, but it's not what i need)


Quote:
Ah... but you have two equations:

origin1.x + t1 * direction1.x = origin2.x + t2 * direction2.x
origin1.y + t1 * direction1.y = origin2.y + t2 * direction2.y

Yes, but from those 2 equations i derived this one:
t2 = ((O2.y - O1.y)/D1.y - (O2.x + O1.x)/D1.x)/(D2.x/D1.x - D2.y/D1.y)
and what if D1.y is 0? or D1.x is 0? It won't work, there must be a simpler way to find collision between rays;

I am thinking about converting one ray to plane, and then test collision between ray and plane...
Well, anyway, now i am going out and will be back only tomorrow, so, if you have any ideas, share...
Using a planar equation you get (origin1+t*direction1).normal = origin2.normal
Which in this case normal = <direction2.y,-direction2.x>
So, dotting with the normal can be replaced with the scalar 2d cross product with direction2
So, (origin1+t*direction1) x direction2 = origin2 x direction2
Solving for t, t = (o2 x d2 - o1 x d2)/(d1 x d2)
If d1 x d2 = 0 then they are parallel and there is no intersection
Unless (o2 x d2 - o1 x d2) also = 0, then they are the same line
Thnx, i think i got it... :)
r++

This topic is closed to new replies.

Advertisement