Jump to content
  • Advertisement
Sign in to follow this  
umen242

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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;
Just do this instead
float velocityX = joystickInput.x * speed;
float velocityY = joystickInput.y * speed;

Share this post


Link to post
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!