# How to calcolate moving vector of object by its rotation degress in 2d

This topic is 718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all

i have object in game which is rotating when i move my joystick , now i need to calculate its movement vector by its rotation
the object always moving forward and controlled by the joystick , the joystick is only setting its direction by degree .
here is the image which illustrate the scene .

i need some formula of the vector which the object will move .

[sharedmedia=core:attachments:33561]

Thanks

##### Share on other sites
The vector is (cos(angle), sin(angle)). This vector is so useful that you generally don't need to deal with the angle at all. The representation of a direction as a unit-length vector is much better for many reasons, even if you find angles more natural.

##### Share on other sites

Thanks for the replay , but can you please explain more , or can you can you please direct me to some links to explain ?

##### Share on other sites
The input of the joystick comes in as two numbers, the x direction and the y direction. The velocity of an object can be represented by the same way. There is no need to convert the joystick direction to an angle, just to convert it back to a x,y pair.

For example, this is doing it with trigonometry
float angle = atan2(joystickInput.y, joystickInput.x);
float magnitude = sqrt(joystickInput.x * joystickInput.x + joystickInput.y * joystickInput.y);

float velocityX = cos(angle) * speed * magnitude;
float velocityY = sin(angle) * speed * magnitude;

float velocityX = joystickInput.x * speed;
float velocityY = joystickInput.y * speed;


##### Share on other sites

The vector is (cos(angle), sin(angle)).

note that this is on a unit circle.

if you use axes with pos x = right and pos y = up, and theta measured clockwise from the pos y axis (like compass degrees), your unit direction vector is x = sin(theta), y = cos(theta).

and for a movement distance d, you displacement vector is x = d*sin(theta), y=d*cos(theta).

Thanks all!

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
14
5. 5

• 12
• 9
• 9
• 17
• 20
• ### Forum Statistics

• Total Topics
632600
• Total Posts
3007340

×