Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411433 Posts in 69363 Topics- by 58418 Members - Latest Member: Pix_RolleR

April 20, 2024, 07:03:32 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)How to calcolate moving vector of object by its rotation degress in 2d
Pages: [1]
Print
Author Topic: How to calcolate moving vector of object by its rotation degress in 2d  (Read 841 times)
umen
Level 0
**


View Profile
« on: October 01, 2016, 10:25:16 AM »

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 .
Logged
Thaumaturge
Level 10
*****



View Profile WWW
« Reply #1 on: October 01, 2016, 11:57:53 AM »

In short, this is trigonometry, I believe.

If I'm not much mistaken:

Your vector can be thought of in terms of its components--that is, its x- and y- value--which, being aligned with the axes, are at ninety degrees to each other. As a result, they form a right-angled triangle, and one of the other angles is your direction-angle.

This means that as long as you know the length of your movement vector (which might simply be the speed at which the object is moving), you can use the sin- and cos- functions to calculate the x- and y- values of your movement vector.

Two caveats, however:

1) You say that you have your angle in degrees; you may find that your math library's sin- and cos- functions expect radians. If so, it's possible that your math library also provides a convenient conversion function--although the conversion is fairly straightforward, I think, and can likely be found online.

2) If you find that your movement vector is pointing in the wrong direction, it may be that your use of sin and cos assume one "zero" direction--that is, the direction that corresponds to an angle of zero--while your movement- or positioning- code assumes another. For example, you may have a draw-call that positions objects on the screen with the origin at the top-right, with x-values increasing to the left, but build your vector code such that an angle of ninety degrees to indicate a vector pointing to the right. In this case, your screen-placement code considers "right" to correspond to "negative x", while your vector-code considers "right" to correspond to "positive x", potentially producing unexpected results.
Logged

projecteterna
Level 0
*


View Profile
« Reply #2 on: October 02, 2016, 09:32:28 PM »


velocityX = cos(angle) * speed * time
velocityY = sin(angle) * speed * time
x = x + velocityX
y = y + velocityY
Logged
umen
Level 0
**


View Profile
« Reply #3 on: October 03, 2016, 12:57:12 AM »

Thanks! all
Logged
oahda
Level 10
*****



View Profile
« Reply #4 on: October 03, 2016, 01:37:20 AM »

You may or may not want to make the y one negative depending on the orientation of your coördinate system.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic