Jump to content
• ### What is your GameDev Story?

• Advertisement

# Ray vs Ray (2D)

This topic is 4913 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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 ?

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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.
------------------------------------------------------------------

#### Share this post

##### Share on other sites
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 isOrigin1+(Speed1*Direction1)*TIME=Origin2+(Speed2*Direction2)*TIMEAnd 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.xorigin1.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...

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
Thnx, i think i got it... :)
r++

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 10
• 11
• 13
• 9
• 11
• Advertisement
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015434
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!