Get consecutive frame sprite positions

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

I’m a beginner in Godot Engine. I’m making a game like a snake race.
I would orient the head of the snake relative to the direction of movement.
Should I get two consecutive position, at temporal distance of “delta” , to determine the slope of the straight line joining the two positions (as shown by the figure). Any suggestions? Thanks in advance.
-jospic

Can you give us more information on how the steering works in your game? Maybe it is easier to get the rotation from there and then rotate the sprite accordingly.

umfk | 2016-03-03 15:57

To move the sprite I used a virtual joystick (on touch screen) kindly provided by kubecz3k:
Omni-directional touch pad - Archive - Godot Forum

jospic | 2016-03-03 16:14

But what function do you use to actually move your sprite? Is it a KinematicBody2Dyou move with move()or do you apply a force? I would guess that somewhere in your movement code you already have the angle you need or a vector pointing in the right direction. You could then use set_rot() to rotate your character.

umfk | 2016-03-03 17:01

I’m using a RigidBody2D and apply a force to move it. Yes get the force components in x and y directions and apply the rotation command. But the character rotates on itself at the slightest movement.

jospic | 2016-03-03 17:06

So you want to rotate the snake’s head to point in the direction of movement? Then you can use RigidBody2D.get_linear_velocity to get the direction vector. Or is it something else you want to do?

umfk | 2016-03-03 17:11

Great, I had not thought of being able to use the linear_velocity to determine the direction . Thank you!

jospic | 2016-03-03 17:14

I put it as an answer so it’s easier for people to find. If you want you can accept it as a correct answer.

umfk | 2016-03-03 17:59

:bust_in_silhouette: Reply From: umfk

You can use RigidBody2D.get_linear_velocity() to get the vector of the current movement direction.