Jump to content
  • Advertisement
Sign in to follow this  

[C/SDL] movement and the angle

This topic is 4414 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

hi! I have tried to make the movement on my little tank game to be like in asteroids, but without the acceleration. What i want is that i could change my angle of the tank when i press the right/left arrow and that it would move forward/backward with the new angle set when i would press the up/down arrow. i've tried some with sin() and cos(), but it was not successful since whenever i pressed left/right arrow the tank began to circle. in the right direction, but it was doing full circles. any how-to-make-asteroids(or games with similar movement) tutorials would come in handy, ty.

Share this post

Link to post
Share on other sites
You could perhaps keep a 2d vector (x, y) to track the direction that the tank is facing. So when you rotate your tank, you can also rotate the direction vector with the rotation matrix:

[ cos() -sin() ]
[ sin() cos() ]

which will rotate your vector counter-clockwise. You should then be able to use that vector to help move your tank forwards/backwards along its new direction. After translating the tank coordinates about the origin, you can move it's coordinates along a line/vector with the parametric equation:

p(t) = p0 + t(p1 - p0)

where p0 would be the origin (0, 0), p1 would be your vector (x, y), and t describes how much you want your point to move along this line. If p0 is the origin (0, 0) this could simplify to:

p(t) = t(p1)

A negative value for t would move the point backwards. After you move it along the line, apply the inverse of the translation you originally used to move the tank to the origin.

I'm not sure exactly how you're implementing your tank movement in your code, but the concepts above should be applicable. I hope that helped you and didn't just complicate things instead :p

Share this post

Link to post
Share on other sites
Original post by homeboye
this 2d vector thing sounds interesting, can you give me some literature?
There are some tutorials in the math articles section here on gdnet. A google for 'vector math' might also turn up some good references.

Share this post

Link to post
Share on other sites
YellowMaple is defenitly onto something there.
I just though Id show you a simple way to accomplish 2d movement

Give your tank a angle variable.

float angle;

Update this angle when you press left/right arrow.

angle -= 1.0;
if(angle < 0.0)
angle = 360.0;

angle += 1.0;
if(angle > 359.0)
angle = 0.0;

When you move the tank, you use this angle along with the current velocity.

float x_movement = cos(DEGREE_TO_RADIAN(angle)) * velocity;
float y_movement = sin(DEGREE_TO_RADIAN(angle)) * velocity;
tank.x += x_movement;
tank.y += y_movement;

All you need to do now is to let the up/down arrow keys control the velocity

velocity = 10.0;
else if(VK_DOWN)
velocity = -10.0;
else velocity = 0.0;

Note that there is some special cases where cos(angle) and sin(angle) will equal zero (If I remember correct, cos(angle) = 0 when angle = 90/270 degrees)

This method should work as long as you have a graphic API that let you rortate a tank image based on the angle.
It is very simple though, if you want to make anything more than a simple 2d demo using bitmaps, YellowMaple's idea is a better path.

Hopefully I wont get aggro for handing out mathematics :D
This is pretty simple stuff though, and you where on same track already...

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!