• Advertisement
Sign in to follow this  

Multidirectional Movement in 2D

This topic is 3627 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, 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 this post


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


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


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


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


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

Share this post


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


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

  • Advertisement