2d rotation

Started by
4 comments, last by endasil 20 years, 5 months ago
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?
EndasilVisit my site, try my gameswww.dragonrealmsoftware.comI need more beta tester for my game! Check out http://www.dragonrealmsoftware.com
Advertisement
angle += 10;
x += cos(angle) * x;
y += sin(angle) * x;

//convert to radians first if youre not using a lookout table with degrees
//---------//Ueberbobo//*********
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]
Thank you very much for responding. Now could you please also explain why it works?


Thanks!
EndasilVisit my site, try my gameswww.dragonrealmsoftware.comI need more beta tester for my game! Check out http://www.dragonrealmsoftware.com
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)
//---------//Ueberbobo//*********
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.
EndasilVisit my site, try my gameswww.dragonrealmsoftware.comI need more beta tester for my game! Check out http://www.dragonrealmsoftware.com

This topic is closed to new replies.

Advertisement