Godot Version
v4.2.stable.official [46dc27791]
Question
I am trying to programmatically find the intersection of 2 line segments. I know there would be a way to do this mathematically, but I thought I would use the existing SegmentShape2D class to detect the intersections via collide_and_get_contacts.
This produces very strange results, which are definitely not at the intersection of the 2 segments, and there are actually 4 collisions rather than the expected 1. I even created a simplified project to illustrate what I’m doing, but without all the extra stuff in my other project. I get the feeling this could be a bug, but thought I’d post here first to see if I’m doing something wrong before opening an issue.
Here is a screenshot of the results of my simplified project:
And here is the code for the simplified project:
extends Node2D
var shape1:SegmentShape2D
var shape2:SegmentShape2D
var points:PackedVector2Array
func _ready():
shape1 = SegmentShape2D.new()
shape1.a = Vector2(324, 658)
shape1.b = Vector2(524, 259)
shape2 = SegmentShape2D.new()
shape2.a = Vector2(524, 458)
shape2.b = Vector2(124, 658)
draw_line(shape1.a, shape1.b, Color.CORNFLOWER_BLUE, 5)
draw_line(shape2.a, shape2.b, Color.CRIMSON, 5)
points = shape1.collide_and_get_contacts(Transform2D.IDENTITY, shape2, Transform2D.IDENTITY)
func _process(delta):
queue_redraw()
func _draw():
draw_line(shape1.a, shape1.b, Color.CORNFLOWER_BLUE, 5)
draw_line(shape2.a, shape2.b, Color.CRIMSON, 5)
for point in points:
draw_circle(point, 3, Color.CHARTREUSE)
Any help would be appreciated, thanks!