+1 vote

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.

enter image description here

in Engine by (1,469 points)
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.
To move the sprite I used a virtual joystick (on touch screen) kindly provided by kubecz3k:
But what function do you use to actually move your sprite? Is it a `KinematicBody2D`you 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.
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.
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?
Great, I had not thought of being able to use the linear_velocity to determine the direction . Thank you!
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.

1 Answer

0 votes

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

by (372 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.