Convert vector to angle?
How do I convert a vector with a begin and endpoint into a angular value in a 2d coordinate system?
tan(angle) = opposite/adjacent, in a right angle triangle.
x is one side, y is another, your vector is the hypotenuse.
use atan (inverse tangent) to find the angle given +x/+y, then check if x and y are positive or negative to get your rotation in the correct quadrant.
edit: ah, found it!
this thread should be interesting to you http://www.idevgames.com/forum/showthread.php?t=10652
x is one side, y is another, your vector is the hypotenuse.
use atan (inverse tangent) to find the angle given +x/+y, then check if x and y are positive or negative to get your rotation in the correct quadrant.
edit: ah, found it!
this thread should be interesting to you http://www.idevgames.com/forum/showthread.php?t=10652
Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Would you mind writing that out in code for me, I am absolutely terrible when it comes to any kind of math.
Thanks.
Thanks.
It's pretty tricky.
Code:
atan2(y, x)
Scott Lembcke  Howling Moon Software
Author of Chipmunk Physics  A fast and simple rigid body physics library in C.
Leroy Wrote:Would you mind writing that out in code for me, I am absolutely terrible when it comes to any kind of math.
Thanks.
The link I gave you...
Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Would you mind clicking that link for me?
PowerMacX Wrote:Would you mind clicking that link for me?
I'm not good with with math or links Well, thanks for the link, I guess I was in a rush and didn't notice it the first time.
Ok, I'm still very confused about this. Here is the code I'm using(or at least its the basic idea) to move the player around, maybe someone could tell me what I'm doing wrong still. Oh, and everything is done in the upper right quadrant of opengl.
float angle;
float radians
void move_player()
{
NSPoint vector;
vector.x = new_world_dst.x  player.x;
vector.y = new_world_dst.y  player.y;
angle = atan2(vector.x, vector.y);
radians = PI / 180 * angle;
position.x = sin(radians) * SPRITE_SPEED;
position.y += cos(radians) * SPRITE_SPEED;
}
float angle;
float radians
void move_player()
{
NSPoint vector;
vector.x = new_world_dst.x  player.x;
vector.y = new_world_dst.y  player.y;
angle = atan2(vector.x, vector.y);
radians = PI / 180 * angle;
position.x = sin(radians) * SPRITE_SPEED;
position.y += cos(radians) * SPRITE_SPEED;
}
atan2(y, x) is not the same as atan2(x, y)
The first is right, the second is wrong.
The first is right, the second is wrong.
IBethune Wrote:atan2(y, x) is not the same as atan2(x, y)
The first is right, the second is wrong.
Actually I'm using the second on and it works just fine now.
That's 'cos you're *also* using sin for x and cos for y, where you should be using cos for x and sin for y
OneSadCookie Wrote:That's 'cos you're *also* using sin for x and cos for y, where you should be using cos for x and sin for y
I'm well aware, but it works out to the same thing. Is there some hidden danger though in doing it this way or is this just bad coding practice(which I can live with)?
The hidden danger is that the first person to sit beside you and read your code will clout you so hard around the head that you'll be in a coma for a month.
Just stick to convention!
Just stick to convention!
OneSadCookie Wrote:The hidden danger is that the first person to sit beside you and read your code will clout you so hard around the head that you'll be in a coma for a month.
Just stick to convention!
LOL. Readability is good. You will probably smack yourself if you ever return to this code later on. I've had this happen to me on more than one occasion and decided to stick to clean code from then on.
Nevada Wrote:LOL. Readability is good. You will probably smack yourself if you ever return to this code later on. I've had this happen to me on more than one occasion and decided to stick to clean code from then on.
Well considering my near 1st grade math level, and since I can barely interpret that code when written conventionally, it's every bit as confusing to me when I see it inverted
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Formula for converting angle to vector?  komirad  2  12,158 
Jul 29, 2011 07:29 AM Last Post: ThemsAllTook 

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

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

Angle between two points?  Graphic Ace  6  6,312 
Nov 8, 2008 12:11 PM Last Post: macnib 

calculating X and Y coordinates w/ an angle and distance  ferum  13  17,723 
Jun 25, 2008 10:53 PM Last Post: rosenth 