Okay, looking at the code, I think I may know what is causing the issue. From what I remember, the Line2D node expects points passed to be in local space (position relative to its origin) rather than global space (relative to the scene origin). Since the mouse positions are in global space, there is a disconnect in the position spaces being used and that is leading to the offset.
Thankfully, converting between spaces is relatively easy. I believe for this issue, all that needs to be done is convert the mouse position to local space relative to the Line2D:
func on_Timer_timeout():
add_point(global_transform.affine_inverse().xform(get_global_mouse_position()))
$Timer.start()
@jujuneos said:
@TwistedTwigleg I don't know how to draw freely using draw_line, when I use it, godot itself already shows a line drawn automatically, could you help me with that?
Using draw_line
would be similar to how the Line2D node works internally: You would need to cache a series of points in an array, and then iterate through that array and draw the lines. Something like this (untested):
var draw_points = []
var point_timer = 0
const POINT_WAIT_TIME = 0.1
func _process(delta):
if Input.is_action_pressed("draw"):
if (point_timer <= 0):
draw_points.append(get_global_mouse_position())
point_timer += POINT_WAIT_TIME
update()
else:
point_timer -= delta
func _draw():
for i in range(0, draw_points.size()-1):
var next_position = global_transform.affine_inverse().xform(draw_points[i+1])
var current_position = global_transform.affine_inverse().xform(draw_points[i])
draw_line(current_position, next_position, Color.black, 1.0)