How to convert a radial into a Vector2

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

You can convert a Vector2 into radials using the .angle() function, but can you do something in reverse?

In a game I am making, the player character produces a Sword object when the player attacks. This Sword object’s position and rotation depends on which way the Player object is facing, represented in a normalized Vector2 called face and multiplied by a constant called SWORD_OFFSET so the sword isn’t right in the center of the body but along a perimeter.

set_pos(get_node("../Player").get_global_pos()+(face*SWORD_OFFSET))

What I am trying to do is modify this Sword position code so that it can take the face of the player object, turn it into a radial with .angle() add an additional radial value to modify a bit, and then turn that back into a Vector2 so it can be used with SWORD_OFFSET to determine the position of the sword.

My use of radials is because in another part of code I use a normalized face Vector2 to determine the rotation of the Sword object.

set_rot(face.angle()+(SWORD_ANGLE_OFFSET*get_node("../Player").attack_state))

In this bit of code the Sword takes the attack_state value from the player (basically how far along it is in an attacking animation) to determine its angle in the sword slash, represented in a value of 0.3 radials.

Overall, I could just back up a bit and redo this bit of code so it works with both rotation and positioning but I’m curious if there’s a way to make Vectors out of radials.

Did you mean “Radian”? I am pretty sure you do, but … I could of course be wrong.

Tybobobo | 2016-11-08 21:41

:bust_in_silhouette: Reply From: Zylann

You can convert an angle in radians (not radial) into a Vector2 using simple math:

var direction = Vector2(cos(angle), sin(angle))

This will give you a normalized vector pointing at an angle.

:bust_in_silhouette: Reply From: Cerno_b

You can rotate a vector, so just create a vector with the length of your offset and rotate it to get the vector at the angle you want:

Vector2(offset, 0).rotated(angle)