Convert vector to angle?

Member
Posts: 105
Joined: 2007.03
Post: #1
How do I convert a vector with a begin and endpoint into a angular value in a 2d coordinate system?
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
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!

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Member
Posts: 105
Joined: 2007.03
Post: #3
Would you mind writing that out in code for me, I am absolutely terrible when it comes to any kind of math.

Thanks.
Sage
Posts: 1,487
Joined: 2002.09
Post: #4
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.
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
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.

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Moderator
Posts: 776
Joined: 2003.04
Post: #6
Would you mind clicking that link for me?
Member
Posts: 105
Joined: 2007.03
Post: #7
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.
Member
Posts: 105
Joined: 2007.03
Post: #8
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;

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;
}
Member
Posts: 156
Joined: 2002.10
Post: #9
atan2(y, x) is not the same as atan2(x, y)

The first is right, the second is wrong.
Member
Posts: 105
Joined: 2007.03
Post: #10
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.
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
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
Member
Posts: 105
Joined: 2007.03
Post: #12
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)?
Luminary
Posts: 5,143
Joined: 2002.04
Post: #13
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!
Member
Posts: 100
Joined: 2006.05
Post: #14
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.
Member
Posts: 105
Joined: 2007.03
Post: #15
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