Archived

This topic is now archived and is closed to further replies.

2d rotation

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

Lets say i have for a ship at the coordinates x,y. If this ship for example is facing the top of the monitor, I rotate the picture of this ship 10 degrees to the right using api and want the ship to move 5 pixels forward in the direction the ship is now facing. How do i calculate the new coordinates for the ship?

Share this post


Link to post
Share on other sites
My page on Asteroids math should help....

Rotate Points

What you want is this....at least this is what I used and it worked

x = x + ( sin(angle) * THRUST_SPEED )
y = y + ( -cos(angle) * THRUST_SPEED )

Angle is in radians, but you can create a look up table with (for example) 32 rotation positions and pre-compute these sin/cos values which is what I did for my Vazteroids game.

VazGames.com

Phil P

[edited by - PhilVaz on November 11, 2003 3:24:54 PM]

Share this post


Link to post
Share on other sites
x += cos(angle);

this calculates the movement along a line that has an angle from the x axis of (angle). basic trigonometry.

y -= sin(angle);

does the same thing for the y movement. as philvaz wrote this should be -= since the screen has a coordinate system that is upside down to the one you usually use so you subtract instead of adding. my bad

philvaz'' THRUST_SPEED is a variable increases/decreases the distance moved along the line. increase this to make the ship move faster

basicly what it does is it split up a leaning line to two stright lines. one parallell to the x axis and one to the y axis

to turn the ship add/subtract to angle (between 0 to 360 degrees, or 0 to 2PI radians)

Share this post


Link to post
Share on other sites
Thank you very much. Now i can both use this to move things around at any angle i want, and i understand what i am doing.
Game programming is a excellent way to learn trig.

Share this post


Link to post
Share on other sites