My apologies, I didn't see the segment_intersects_segment_2d()
function in the Geometry
class. That is the applicable function here since you're creating line segments not lines.
I didn't catch that you were feeding the wrong arguments to line_intersects_line_2d()
either. If you wanted to use that functions (when using lines) in the future you would give it the following arguments:
var direction_for_line_1 = (line1.get_points()[1] - line1.get_points()[0]).normalized()
var direction_for_line_2 = (line2.get_points()[1] - line2.get_points()[0]).normalized()
Geometry.line_intersects_line_2d(line1.get_points()[0], direction_for_line_1, line2.get_points()[0], direction_for_line_2)
I checked this code a handful of times and saw no errors in the sprite placement, please let me know if you have any questions about it!
extends Node2D
export (int) var line_amount
var window_width = ProjectSettings.get("display/window/size/width")
var window_height = ProjectSettings.get("display/window/size/height")
var rng = RandomNumberGenerator.new()
var nodes = []
func get_intersections(line1,line2):
return Geometry.segment_intersects_segment_2d(line1.get_points()[0],line1.get_points()[1],
line2.get_points()[0],line2.get_points()[1])
func _ready():
for line in line_amount:
var line2d = Line2D.new()
nodes.append(line2d)
for node in nodes:
rng.randomize()
var rng_x = Vector2(rng.randf_range(0,window_width),rng.randf_range(0,window_height))
var rng_y = Vector2(rng.randf_range(0,window_width),rng.randf_range(0,window_height))
node.width = 5
add_child(node)
node.add_point(rng_x)
node.add_point(rng_y)
# range(n, m) creates an array of [n, n+1, n+2, ..., m-2, m-1]
# hence using nodes.size() - 1 and not nodes.size() - 2
for outer_index in range(0, nodes.size() - 1):
for inner_index in range(outer_index + 1, nodes.size()):
var current_intersection = get_intersections(nodes[outer_index],nodes[inner_index])
if current_intersection:
var int_sprite = Sprite.new()
int_sprite.texture = load("res://icon.png")
int_sprite.position.x = current_intersection.x
int_sprite.position.y = current_intersection.y
int_sprite.scale.x = .350
int_sprite.scale.y = .350
add_child(int_sprite)