Hello,

I have 2 Vector2 that I know will cross each other at one point. How can I find the intersection point between them?

Thank you!

Vectors typically represent points. You can't represent a line with just one vector. At least two vectors are needed. Geometry singleton has the segment_intersects_segment_2d() method that can calculate line intersections for you:

Geometry.segment_intersects_segment_2d(line1_startpoint, line1_endpoint, line2_startpoint, line2_endpoint)

Similarly if you want to check intersection of endless lines instead of line segments you can use:

Geometry.line_intersects_line_2d(line1_startpoint, line1_direction, line2_startpoint, line2_direction)

Both functions take four Vector2 arguments.

I should probably explain better what I'm trying to do. The code is run when a collision between 2 Area2D occurs. Both are part of a scene that also has a Raycast2D, used for something else. Here however I try to find our where those cross for a visual effect at this point (which is not where the collision itself happened).

I tried this following your advice (A1... just for readability during testing):

var A1 = $Target_RayCast2D.global_position var A2 = A1 + $Target_RayCast2D.cast_to var B1 = _area.get_parent().find_node("Target_RayCast2D").global_position var B2 = B1 + _area.get_parent().find_node("Target_RayCast2D").cast_to var intersection_point = Geometry.segment_intersects_segment_2d(A1, A2, B1, B2)

it does however return NULL.

print(str(A1)) print(str(A2)) print(str(B1)) print(str(B2))

returns

(915, 520) (915, 5520) (475, 630.000122) (475, 5630)

which are the correct coordinates.

Well it correctly returns null because segments do not intersect, which is evident from the printed numbers. Depending on what you actually need, you can extend the segments or try using line_intersects_line_2d() instead

But they do intersect at 915,630 ? I can even see it by the rays in the editor.

Edit:

var intersection_point = Geometry.line_intersects_line_2d(A1, A1.direction_to(A2), B1, B1.direction_to(B2))

also return NULL. Not sure what I'm doing wrong.

Your lines, according to printed numbers, are parallel. That's evident because start and end point of each line has the same x coordinate. Parallel lines can't ever intersect. You're probably doing something wrong when extracting A and B points from the raycast nodes. Documentation says that cast_to property is relative to position property. You're taking it relative to global_position. Maybe check that. It's hard to tell without knowing transformation hierarchy of your nodes.

The lines you printed are parallel (2 vertical lines) so they will never intersect.

Yeah. Best to get both points in the local space, xform them to global space and then check for intersection.

It is indeed so that the cast_to position didn't took the rotation of the node the Raycast is attached to into account.

Here is what I came up with:

var A1x = $Target_RayCast2D.position
var A2x = $Target_RayCast2D.cast_to
var B1x = _area.get_parent().find_node("Target_RayCast2D").position
var B2x = _area.get_parent().find_node("Target_RayCast2D").cast_to
	
var A1 = global_transform.xform(A1x)
var A2 = global_transform.xform(A2x)
var B1 = _area.get_parent().global_transform.xform(B1x)
var B2 = _area.get_parent().global_transform.xform(B2x)
	
var intersection_point = Geometry.segment_intersects_segment_2d(A1, A2, B1, B2)

Is this a good way doing this?

a year later