如何制作特定长度的新线?

How to make a new created line of specific length?

我需要我画的线有特定的长度。我可以检测到它何时达到所需的长度,但如果用户移动鼠标的速度更快,它可能会使线条比预期的更长。

这是我遇到的问题的视频,当我缓慢移动鼠标时效果很好但速度有问题:https://www.youtube.com/watch?v=4wkYcbG78TE

这是我创建和检测线条长度的代码。

if Input.is_action_pressed("Left_click"): #This checks the distance between last vector and the mouse vector
        #points_array[-1] = get_global_mouse_position() # Gets the last position of the array and sets the mouse cords
        var length = clamp(points_array[-1].distance_to(get_global_mouse_position()),0,20)
        if length == 20: # if its long enough create a new line and set it to mouse position
            var cords = get_global_mouse_position()
            print(cords)
            points_array.append(cords)

当鼠标移动过多时,您可以添加多个点来填充间隙,当然,始终保持正确的距离。

即当最后一个点到鼠标位置的长度大于你想要的距离时,在适当的距离处再添加一个点。

因此,一个 while 循环:

if Input.is_action_pressed("Left_click"):
    var mouse_pos = get_global_mouse_position()
    var distance = 20
    while points_array[-1].distance_to(mouse_pos) > distance:
        var cords = # ???
        points_array.append(cords)

一点点向量代数就会计算出该点的位置。从最后添加的点开始,你想从它到鼠标位置的方向。要走多远?嗯,就是你想要的长度。

if Input.is_action_pressed("Left_click"):
    var mouse_pos = get_global_mouse_position()
    var distance = 20
    while points_array[-1].distance_to(mouse_pos) > distance:
        var last_point = points_array[-1]
        var cords = last_point + last_point.direction_to(mouse_pos) * distance
        points_array.append(cords)

我相信这应该有效。