This is all using trigonometry to accomplish the movement. I'm not sure how well equipped I am to explain trig :)
Your player's position
is a vector, which Godot calls a Vector2
. It looks just like a point with an x and a y coordinate, but vectors are more than that. They are basically a way to describe in what direction something is pointing.
Angles also describe how something is pointing, they are kind of the opposite of vectors. So if the mouse cursor is in the upper right of the screen, and the player is down in the lower left, then the mouse cursor is about 45 degrees from the player.
var angle = position.angle_to_point(mouse_pos)
is taking two vectors (the player's position and the mouse cursor's position), and figuring out what the angle is between them. Like in the above example, it'd figure out 45 degrees.
Once you have an angle, you can convert it into a vector. cos
and sin
are trig functions that help with that.
var direction = Vector2(cos(angle), sin(angle))
took the angle (45 degrees) and converted that into a vector.
Once you have the proper vector, you can give it to Godot and it will use it to move your character. Godot uses vectors for a lot of things, in this case it is using it for movement.
If you want to learn more, doing a Google search for "trigonometry for game developers" turns up a lot of stuff. Here is a tutorial I just found that looks like it might be good:
https://www.raywenderlich.com/2736-trigonometry-for-game-programming-part-1-2
If nothing else, that tutorial explains sin
and cos