Public Group

#### Archived

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

# 2d rotation

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

## 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 on other sites
angle += 10;
x += cos(angle) * x;
y += sin(angle) * x;

//convert to radians first if youre not using a lookout table with degrees

##### 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 on other sites
Thank you very much for responding. Now could you please also explain why it works?

Thanks!

##### 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 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.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632936
• Total Posts
3009312
• ### Who's Online (See full list)

There are no registered users currently online

×