2d rotation
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?
angle += 10;
x += cos(angle) * x;
y += sin(angle) * x;
//convert to radians first if youre not using a lookout table with degrees
x += cos(angle) * x;
y += sin(angle) * x;
//convert to radians first if youre not using a lookout table with degrees
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]
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]
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)
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)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement