Find point at which two rays intersect

I’m trying to find the point at which a ray originating from Vector2 coord_1 and pointing at angle theta_1 intersects with another ray originating from Vector2 coord_2 and pointing at angle theta_2.

I found a few things about this online, but I don’t quite remember how to interpret the geometric notation of this post, and I’m not sure how to use the method outlined in this post because I don’t have a ray object, I have the point and angle.

Use Geometry2D::segment_intersects_segment()

Hi, this should do it.

extends Node2D

@onready var point1 = $Marker2D_1.position
@onready var point2 = $Marker2D_2.position
@onready var angle1 = 0
@onready var angle2 = 90

func ray_intersection(p1: Vector2, a1: float, p2: Vector2, a2: float) -> Variant:
	var d1 = Vector2(cos(a1), sin(a1))
	var d2 = Vector2(cos(a2), sin(a2))

	var denom = d1.x * d2.y - d1.y * d2.x
	if abs(denom) < 0.000001:
		return null # parallel or nearly parallel

	var diff = p2 - p1
	var t = (diff.x * d2.y - diff.y * d2.x) / denom
	var intersection = p1 + d1 * t

	return intersection

func _ready():	
	var hit = ray_intersection(point1, deg_to_rad(angle1), point2, deg_to_rad(angle2))
	if hit != null:
		print("Intersection: ", hit)
	else:
		print("No intersection")

This intersects lines, not rays. Mathematical line goes to infinity in two direction, while a mathematical ray has an origin point and goes into infinity only in one direction. In the context of game engines, a ray will almost always have a finite length. This basically makes it a line segment, hence the simplest way is to use Geometry2D::segment_intersects_segment() as I already mentioned.

Btw you don’t need to call trig functions to make a direction vector from an angle. There’s already a built in function that does it: Vector2::from_angle()

You are right, i just added this cause for me is not clear if OP has only points and angle or vectors.

Btw you don’t need to call trig functions to make a direction vector from an angle. There’s already a built in function that does it: Vector2::from_angle()

Sure, happens when not having godot at work. :smiley:
var d1 = Vector2.from_angle(a1)
var d2 = Vector2.from_angle(a2)

It doesn’t really matter. Ultimately, you need two points per ray. The origin point is given explicitly, and the other one can be trivially calculated from the origin point and the direction vector (which can again be trivially deduced from the angle)