calculating X and Y coordinates w/ an angle and distance
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:
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?!?
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));
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?!?
Try the atan2 function. It does what you want.
Degrees make no sense, mathematically. That is why everything uses radians (which do make sense).
Degrees make no sense, mathematically. That is why everything uses radians (which do make sense).
OneSadCookie Wrote:Degrees make no sense, mathematically. That is why everything uses radians (which do make sense).except glRotate grr.....
Yes, OpenGL is silly
Still, glRotate is a convenience function. You'll find as you get more advanced, you use it less and less.
Still, glRotate is a convenience function. You'll find as you get more advanced, you use it less and less.
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.
sine angle = opposite / hypotenuse
cos angle = adjacent / hypotenuse
tan angle = opposite / adjacent
Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
hmm... switching
for
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.....
Code:
coordY += (sin((double) (ZRot * 0.0174532925)) * (DistIncrem));
coordX = (cos((double) (ZRot * 0.0174532925)) * (DistIncrem));
Code:
coordX = (sin((double) (ZRot * 0.0174532925)) * (DistIncrem));
coordY += (cos((double) (ZRot * 0.0174532925)) * (DistIncrem));
The math is perfectly sound Damnit!
Why does the latter work instead of the first ? Grr.....
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.)
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.)
akb825 Wrote:Where do you take 0 from?Whadya mean?
oh, ok I understand what your saying.
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 += ?
ferum Wrote:hmm... switching
forCode:
coordY += (sin((double) (ZRot * 0.0174532925)) * (DistIncrem));
coordX = (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.Code:
coordX = (sin((double) (ZRot * 0.0174532925)) * (DistIncrem));
coordY += (cos((double) (ZRot * 0.0174532925)) * (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.
Holmes Wrote:Any reason why the second line is = instead of += ?I'm thinking it goes clockwise instead of counterclockwise.
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;
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;
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Formula for converting angle to vector?  komirad  2  13,935 
Jul 29, 2011 07:29 AM Last Post: ThemsAllTook 

Question Regarding the Reflect Angle of a Transition  iBaby  3  5,274 
Apr 27, 2010 03:15 PM Last Post: JustinFic 

Fast Distance formula?  mikey  11  12,158 
Nov 23, 2009 10:43 AM Last Post: mikey 

Local (X, Y) Coordinates from 3D plane coordinates  merrill541  5  10,994 
Jun 29, 2009 01:32 AM Last Post: RhinosoRoss 

ending location from angle and speed  Kazooless  5  6,584 
Apr 3, 2009 02:40 PM Last Post: Gillissie 