calculating X and Y coordinates w/ an angle and distance

Member
Posts: 185
Joined: 2005.02
Post: #1
Ok so i have a program that displays a ship, when you press the up key it "flies" upwards. I decided to add the ability to turn. Instead of screwing with the display matrix and all that I decided to just calculate how much to increase the x and y coordinates based on the angle of the ship. For simplicity's sake I decided to just implement it while the angle was between 0 and 90 degrees. I have if statements that check for special cases (such as when the angle is 0, 90, or -90 degrees).
Frist I calculate how far the ship is going to travel and assign that number to DistIncrem, then I calculate the x and y coordinates as follows:
Code:
coordY += (sin((double) (ZRot * 0.0174532925)) * (DistIncrem));
coordX -= (cos((double) (ZRot * 0.0174532925)) * (DistIncrem));
A few notes about the code: ZRot is multiplied to convert it to radians to accomodate the sine function (why does math.h only have a sine function that uses radians?). CoordX is decremented because glRotate rotates counterclockwise, and the ship's starting position, and effectively the origin of the grid I am working on is at the bottom of the window in the center.

Why the H:mad:LL won't this work?!?
I can't figure it out. But it definately doesn't work. I can tell by watching the program run. I'll have it rotated like 80 degrees to the left and it will be going nearly vertical!
Why can't anything just work when I implement it?!? Cry
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Try the atan2 function. It does what you want.

Degrees make no sense, mathematically. That is why everything uses radians (which do make sense).
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #3
OneSadCookie Wrote:Degrees make no sense, mathematically. That is why everything uses radians (which do make sense).
except glRotate grr.....
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
Yes, OpenGL is silly Smile

Still, glRotate is a convenience function. You'll find as you get more advanced, you use it less and less.
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #5
no, I don't need to find the arctangent, I already have the angle. I'm looking for the two sides. I have the angle, a right angle (assumed), and the hypotenuse.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #6
[Image: trig1.gif]

sine angle = opposite / hypotenuse
cos angle = adjacent / hypotenuse
tan angle = opposite / adjacent

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #7
hmm... switching
Code:
coordY += (sin((double) (ZRot * 0.0174532925)) * (DistIncrem));
coordX -= (cos((double) (ZRot * 0.0174532925)) * (DistIncrem));
for
Code:
coordX -= (sin((double) (ZRot * 0.0174532925)) * (DistIncrem));
coordY += (cos((double) (ZRot * 0.0174532925)) * (DistIncrem));
made it work. But how does that make sense? Sine equals opposite over hypotenuse. So the Sine of ZRot equals Y over DistIncrem, therefore Y equals Zrot multiplied by DistIncrem.
The math is perfectly sound Damnit!
Why does the latter work instead of the first ? Grr.....
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #8
Where do you take 0 from? If it's horizontal, cos will be x and sin will be y, but if it's vertical (done a lot of times, even though it's contrary to most mathematics) it will be the other way around.

BTW, degrees still have some use: for human input, it's a lot easier to deal with degrees than radians. (except for easy numbers like pi/2, pi/4 etc.)
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #9
akb825 Wrote:Where do you take 0 from?
Whadya mean?
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #10
oh, ok I understand what your saying.
Quote this message in a reply
Member
Posts: 142
Joined: 2002.11
Post: #11
ferum Wrote:
Code:
coordY += (sin((double) (ZRot * 0.0174532925)) * (DistIncrem));
coordX -= (cos((double) (ZRot * 0.0174532925)) * (DistIncrem));

Any reason why the second line is -= instead of += ?
Quote this message in a reply
Member
Posts: 142
Joined: 2002.11
Post: #12
ferum Wrote:hmm... switching
Code:
coordY += (sin((double) (ZRot * 0.0174532925)) * (DistIncrem));
coordX -= (cos((double) (ZRot * 0.0174532925)) * (DistIncrem));
for
Code:
coordX -= (sin((double) (ZRot * 0.0174532925)) * (DistIncrem));
coordY += (cos((double) (ZRot * 0.0174532925)) * (DistIncrem));
made it work. But how does that make sense? Sine equals opposite over hypotenuse. So the Sine of ZRot equals Y over DistIncrem, therefore Y equals Zrot multiplied by DistIncrem.
The math is perfectly sound Damnit!
Why does the latter work instead of the first ? Grr.....

Your graphic is probably rotated incorrectly. In the .bmp, .jpg, or whatever you're using, the nose of the ship should face right, my guess is that your graphic is facing up. The reason I think this is that the substitutions you've done to make it work are equivalent to a 90 degree rotation.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #13
Holmes Wrote:Any reason why the second line is -= instead of += ?
I'm thinking it goes clockwise instead of counter-clockwise.
Quote this message in a reply
rosenth
Unregistered
 
Post: #14
althoght your code works, but its wacky!
use this one:

car_x += (cos((double) ((rotation+90) * 0.0174532925)) * (0.01)); //x= r cos(teta), Step is 0.01

car_z +=(-1* (sin((double) ((rotation+90) * 0.0174532925)) * (0.01))); //y= r sin(teta)

what is 90 augment? and what about (-1)!
you multiply (-1) in z position of car, -z is in far and z+ near eye point (both inversed of y vector)
and 90 is added to convert angle Logic in openGL to Math logic , to have true X values.

rest of code:
// while car is moving in front of itself
if(keys[VK_LEFT])

{

rotation+=0.8;

//wheel_rotation+=0.5;

}

else if(keys[VK_RIGHT])

{

rotation-=0.8;

//wheel_rotation-=0.5;

}

if(rotation>360)

rotation=0.8;

//and while car is in rear
if(keys[VK_LEFT])

{

rotation-=0.8;

//wheel_rotation+=0.5;

}

else if(keys[VK_RIGHT])

{

rotation+=0.8;

//wheel_rotation-=0.5;

}

if(rotation>360)

rotation=0.8;
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Formula for converting angle to vector? komirad 2 9,829 Jul 29, 2011 07:29 AM
Last Post: ThemsAllTook
  Question Regarding the Reflect Angle of a Transition iBaby 3 3,502 Apr 27, 2010 03:15 PM
Last Post: JustinFic
  Fast Distance formula? mikey 11 7,319 Nov 23, 2009 10:43 AM
Last Post: mikey
  Local (X, Y) Coordinates from 3D plane coordinates merrill541 5 6,888 Jun 29, 2009 01:32 AM
Last Post: RhinosoRoss
  ending location from angle and speed Kazooless 5 4,472 Apr 3, 2009 02:40 PM
Last Post: Gillissie