Two vectors with their position (start position+direction) if they intersect or not

Hello, what is the easiest way to get the info if the two rays in 2d space intersect or not? (I have some complicated idea to subtract their positions, compute the dot angles between their position difference and the direction of each point, then compare if the summary of these two angles is less than 180) but is there some more simple setup?THANKS!

ps: some progress and file, it is naive, first step, does not work for 360 turns yet, I am still quite poor in trigon node math.


rays.blend (1.5 MB)

Maybe like this?
Disclaimer: I’m not good at trig either, so take it with a grain of salt.

The idea here is that there’s a range of directions in which Ray2 can possibly intersect Ray1: it has to be between direction of Ray1 and direction to its origin/start.
So first we get all the needed vectors: Ray1, Ray2 and Vec = (Start2 - Start1).
To find if Ray2 is in the range we can perform two checks:

  • is angle between (Vec, Ray2) less than (Vec, Ray1)? We use Dot Product for that. But that alone won’t be enough - it can still return “true” if the Ray2 is out of range.
  • are Ray1 and Ray2 in the same rotational direction from Vec (clockwise or counterclockwise)? Here Cross Product is our friend. It returns a vector perpendecular to two input vector. If both Cross Products point in the same direction (again we can check it with DotProduct), than our vectors are rotated in the same direction relative to Vec.
    Combining this two checks we should find if Ray2 intersects Ray1 or not.

crossRays.blend (497.6 KB)

3 Likes

Hello,

I don’t have a method that I can share right now, but it’s very likely that it’s a common problem and you can find some well known algorithm for that , instead of coming with your own ( which is fun to do anyway) …

For instance, at some point I needed to find the intersection point of two lines and I translated that code into geometry nodes : https://gist.github.com/hanigamal/6556506

That was for that project : Arrays of books! (Geometry nodes)

This sound a bit daunting at first as code can be intimidating, but in the end most part of the code can be ignored, I never learned C++ but since it’s mostly about math function it’s basically the same stuff that you can find in GN…

You might also look for math courses that shares some algorithm without having to deal with code…

Just food for thought !
Have fun !

2 Likes

Hi,
you can use the new Matrix Determinant node for that.

Here’s a setup using the formulas from wikipedia which also gives you the intersection point:


GN_2D_line_line_intersection.blend (677.5 KB)

Good luck!

3 Likes

thank you both! Looking at the math sources was my first intention before I asked here algorithm - Determining if two rays intersect - Stack Overflow, but I was just lazy to try to translate it so first I asked here if someone will come up with already existing node proved minimal solution. Meanwhile was testing my own…:wink:

1 Like

Thanks Zebra!

1 Like

Hello, so finally had more time to compare and try to understand:
@stray - any salt there so far, it works good, elegant idea!
@Zebrahead - this is probably more sophisticated, unfortunately whatever I do with matrices, is more guesswork than pure understanding. Intersection position output works well, but the boolean for some reason does not work. Maybe something wrong on my side (blender 4.3)

I just stupidly copied the formula from here seems to me it works well. All three approaches in the file below:

rays-intersection.blend (1.9 MB)

1 Like

On that Wikipedia link under Formulas → Given two points on each line, there is a formula for the X and Y position of the intersection. As stated after, if the denominator of these formulas is 0 then the two lines are parallel → i.e. not intersecting. If you’re only interested in knowing if there’s an intersection or not, you can simply calculate the denominator:
grafik
which would look like this in Geometry Nodes without using matrices:

The problem in your setup lies somewhere in the input vectors. I didn’t understood the connections there, sorry. Just make sure that you plug in the start and end position of each line there!

1 Like

Thanks Zebra, in my setup I have curve with one ray at each end (any vector, but for testing I used tangents of the original two curves), so line1pos1 is curve endpoint 10 and line2pos1 is endpoint 01 then these two endpoints offseting by some vector that should be the ray - it is just my probably strange way how to adjust to your setup made for two curves/edges. Anyway when I open your file without touching anything the boolean check does not work.
blender_JWn52Biksk
Other two setups working well, just wondering why this is happening.

1 Like

I can’t confirm… it works on my end :man_shrugging:
If the two lines are completly parallel (Scale X → 0), the result is False:


As soon as i rotate one line just a little bit it turns to True (because the two lines will intersect really really far away):

Maybe the threshold is set too small for your usecase? You can change the Epsilon value of the Comparison node inside the nodegroup and see if that works for you:

got it, sorry, was just misunderstanding;) I am checking if they ever intersect or not and only the rays in one direction. The reason I was asking I have multiple lines with different rays at each end ( lines connecting the points on the curve with a band sampling from that band curve its normal and then use this as rays on the connecting curves) I need to eliminate lines where these rays never intersect. Anyway your setup is good, will be a great help in another case.

Thanks!

1 Like

Ahhh, i get it now! I misunderstood the initial question, my bad!

thanks to that I have another useful node…:wink:

1 Like