How to get the move direction of a PathFollow2D without set the Rotate on?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By alexzheng
:warning: Old Version Published before Godot 3 was released.

I do not want the node rotate itself when move on the path, but I need the move direction.
I do this to get the direction

var pre_pos = get_pos()
set_offset(get_offset() + offset)
var pos = get_pos()
var angle = atan2(pos.y - pre_pos.y, pos.x - pre_pos.x)

it’s ok to get the direction, but the value returned become jitter on the corner of the path, for example, move from a Horizontal line to a vertical line, it won’t simple produce the angle from 0 to PI/2, but produce some data around PI/2

:bust_in_silhouette: Reply From: mollusca

If your path doesn’t have any rounded corners it might be easier to do the movement manually instead of using a PathFollow2D. That way you can easily get the direction of the path segment that your node is currently on.

extends Sprite

var dir = 0.0
var ind = 0
var offset = 0.0
var points = []
var l_scale = 1.0
var pos_curr = Vector2()
var pos_next = Vector2()

func _ready():
    set_process(true)

    #assumes the Sprite is a child of a Path2D
    var curve = get_parent().get_curve()
    for i in range(curve.get_point_count()):
        points.push_back(curve.get_point_pos(i))

    pos_curr = points[ind]
    pos_next = points[ind + 1]
    dir = points[ind + 1].angle_to_point(points[ind])
    l_scale = 100.0 / points[ind].distance_to(points[ind + 1])

func _process(delta):
    offset += delta * l_scale
    if offset >= 1.0:
        ind = (ind + 1) % (points.size() - 1)
        offset = fmod(offset, 1.0)
        pos_curr = points[ind]
        pos_next = points[ind + 1]
        l_scale = 100.0 / pos_curr.distance_to(pos_next)
        dir = pos_next.angle_to_point(pos_curr)
        update()
    set_pos(pos_curr.linear_interpolate(pos_next, offset))

func _draw():
    draw_line(Vector2(0, 0), Vector2(0, 16).rotated(dir), Color(0.9, 0.0, 0.0), 2)

Thanks…

alexzheng | 2018-01-05 01:37