# Multidirectional Movement in 2D

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

## Recommended Posts

Hello, I am currently working on some sprite movement and have encountered a problem. I have a sprite with 36 frames each frame rotates the sprite. So you could make the assumption of 10degree / frame. My problems comes in that I want to control the craft using up/down/left/right. I want up and down to control forward/backward movement and left/right to rotate the ship accordingly. I then want the ship to go forward in the direction its pointing. I have tried several different methods of making it work but all of them seem inefficient. My current method is to check which frame the sprite is in then move in that direction using something like this. x+= .1; y+= .9; for frame 1. Note: Frame 0 is directly upwards, Frame 10 directly right, Frame 20 direct downward, Frame 30 directly left and frame 36 is 10 degrees from upward. Thanks in advance for any replies. I just can't seem to figure out if there is a mathematical way to go about this problem.

##### Share on other sites
it sounds like you have 37 frames (0, 1, 2, . . . 34, 35, 36)

if frame 0 is 0 degrees, and frame 36 is 350 degrees then frame n is ( 350 * n / 36) degrees.

if you rotate the sprite to frame n and then accelerate by dv, the new x and y velocities are gotten with something like: vx += cos( 350*n/36) * dv; vy += sin( 350*n/36) * dv;

hope this helps

##### Share on other sites
36 frames actually but how would I change that if frame 0 was 90 degrees? I understand that formula I just get confused with the angles.

##### Share on other sites
Well, take the rotation ingame, subtract 90 from it (or PI / 2, if you're using radians) and then use the formula from dtudury, or convert his formula to radians

##### Share on other sites
double ang1 = (360*(frame-1)/36)-90;
double ang2 = (360*(frame+1)/36)-90;

m_x += cos(ang1) * 1;
m_y += sin(ang2) * 1;

This is what I'm using at the moment and its giving me some rather odd results. It seems that the cos and sin is what I needed. Thanks for that guys.

##### Share on other sites
Oops. I forgot to convert to radians on that last step. The formulas work great. Thanks for the help.

##### Share on other sites
You're now limiting your rotations to steps of 10 degrees. Why not keep an angle value, and determine which frame to display from that? This also means that, regardless of how the ship is being displayed, it will always move correctly. You could then easily change to other frames without affecting the actual movement.

##### Share on other sites
Good Idea CaptainP I think I will actually implement that real quick.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015654
×