• Create Account

# Angle between two 2D points

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

37 replies to this topic

### #1_Sauce_  Members   -  Reputation: 116

Like
0Likes
Like

Posted 23 August 2009 - 08:43 PM

I'm trying to find the angle between two points in 2D, A and B. A is the location of a sprite, and B is the location of the mouse. I want to set the rotation of the sprite to the angle between the two points, so that the sprite appears to follow the mouse. I've been told that the dot product of two unit vectors returns the cosine of the angle between them, in the range -1 to 1, so the angle between the vectors must be... theta = acos(A.x * B.x + A.x * B.y) theta of course, is represented in radians. Now, the library I'm using is SFML, which requires that the angle for the sprite be passed to it in degrees, so it's as simple as Sprite::SetRotation(theta * 180.0f / PI), but the problem is, acos() seems to only be returning a value in the range 0 to PI/2 radians aka 0 to 90 degrees. How do I get an angle in the range 0 - 360 instead?

### #2shadowcomplex  Members   -  Reputation: 445

Like
0Likes
Like

Posted 23 August 2009 - 08:49 PM

float CMath::Angle( const CPoint* a, const CPoint* b){	return ToDegrees( atan2( b->y - a->y, b->x - a->x ) );}float CMath::Angle( const CPoint* b){	CPoint a(0,0,0);	return Angle( &a, b );}float CMath::ToDegrees( float radians ){	return radians * DEGREES_PER_RADIAN;}float CMath::ToRadians( float degrees ){	return degrees * RADIANS_PER_DEGREE;}

### #3_Sauce_  Members   -  Reputation: 116

Like
0Likes
Like

Posted 23 August 2009 - 08:56 PM

haha, I suppose that will do it :)

I was just curious as to how to do it using the dot product. I'm still interested to hear if someone knows the answer.

### #4phresnel  Members   -  Reputation: 949

Like
0Likes
Like

Posted 23 August 2009 - 08:58 PM

How exactly do you define "angle between points"?

Points are dimensionless entities with no direction in any n-space. The only sane measure between two points that I am aware of (at 10 in the morning) is distance.

Possibly you mean angle between vectors?

### #5_Sauce_  Members   -  Reputation: 116

Like
0Likes
Like

Posted 23 August 2009 - 09:01 PM

phresnel, I'm guessing that you mean that there's not such thing as angle between points on a 1D line... I would have thought specifying points in 2D space would have made it obvious that I meant 2D vectors :)

### #6shadowcomplex  Members   -  Reputation: 445

Like
0Likes
Like

Posted 23 August 2009 - 09:02 PM

It's off of the top of my head, but I'm believe it is correct. Test it though to make sure :)

As for using acos, check here:
http://www.cppreference.com/wiki/c/math/acos

You can use it but you need one more logic step afterwards to determine if your result should be positive or negative (which you can then calc out of 360). I find the atan2 solution to be cleaner, personally.

### #7shadowcomplex  Members   -  Reputation: 445

Like
0Likes
Like

Posted 23 August 2009 - 09:16 PM

Quote:
 Original post by phresnelHow exactly do you define "angle between points"?Points are dimensionless entities with no direction in any n-space. The only sane measure between two points that I am aware of (at 10 in the morning) is distance.Possibly you mean angle between vectors?

Often (and incorrectly so), points and vectors are used interchangeably in game dev, especially 2D game dev. Basically considering a point to be a vector originating from the origin. I've seen this from indies, hobbyists, and commercial shops :)

This becomes a nuisance eventually, though, with "w" values. For the most part, 2D games can save on some code by having a joint "point"/"vector" class, even though it is amathematically incorrect model :D

So in the OP's case, it's really mapping one origin vector to another and determine the angle between them.

### #8phresnel  Members   -  Reputation: 949

Like
0Likes
Like

Posted 23 August 2009 - 10:16 PM

Quote:
Quote:
 Original post by phresnelHow exactly do you define "angle between points"?Points are dimensionless entities with no direction in any n-space. The only sane measure between two points that I am aware of (at 10 in the morning) is distance.Possibly you mean angle between vectors?

Often (and incorrectly so), points and vectors are used interchangeably in game dev, especially 2D game dev. Basically considering a point to be a vector originating from the origin. I've seen this from indies, hobbyists, and commercial shops :)

This becomes a nuisance eventually, though, with "w" values. For the most part, 2D games can save on some code by having a joint "point"/"vector" class, even though it is amathematically incorrect model :D

So in the OP's case, it's really mapping one origin vector to another and determine the angle between them.

I think it is only beneficial to seperate those models (and comes at no extra runtime cost), as I made the experience that it makes code clearer (i.e. intentions) and less error prone (operations that don't make sense are forbidden). Some operations are also distinct, dependent on whether the underlying concept is point, vector, or normal.

As you see, it already confused me to see some angle between points, which only makes sense if we assume some coordinate origin to both.

To give an example of how I am used to it:
   Point A, B;   ...   Vector direction = B-A;   float length = direction.length();   Point newP = A + direction * 3;

Point+Point is not defined, Point-Point yields a Vector, Point[+-]Vector yields a new Point, et cetera.

In picogen, the distinction is important for yet another reason: Points and Vectors are allowed to use different scalar types. I need this so that I can use fixed-point arithmetic for points, but floating point math for directions. Thanks to that distinction and the thorough constraints upon Points (operator+, operator-, distance), only the most basic fixed point math is needed (addition, subtraction, comparison).

...

Probably I am just too drilled by writing ray tracers, where the mathematical model is often common ;)

### #9shadowcomplex  Members   -  Reputation: 445

Like
0Likes
Like

Posted 23 August 2009 - 11:07 PM

Quote:
 Original post by phresnelI think it is only beneficial to seperate those models (and comes at no extra runtime cost), as I made the experience that it makes code clearer (i.e. intentions) and less error prone (operations that don't make sense are forbidden). Some operations are also distinct, dependent on whether the underlying concept is point, vector, or normal.As you see, it already confused me to see some angle between points, which only makes sense if we assume some coordinate origin to both. To give an example of how I am used to it: Point A, B; ... Vector direction = B-A; float length = direction.length(); Point newP = A + direction * 3;Point+Point is not defined, Point-Point yields a Vector, Point[+-]Vector yields a new Point, et cetera. In picogen, the distinction is important for yet another reason: Points and Vectors are allowed to use different scalar types. I need this so that I can use fixed-point arithmetic for points, but floating point math for directions. Thanks to that distinction and the thorough constraints upon Points (operator+, operator-, distance), only the most basic fixed point math is needed (addition, subtraction, comparison)....Probably I am just too drilled by writing ray tracers, where the mathematical model is often common ;)

No doubt, I agree absolutely. I've worked with systems using both approaches and found it's only sane to seperate them in large scale, math heavy projects. Of course, if I feel like writing a 2D shooter in a weekend, it's fun to pretend to add two points together :D

### #10_Sauce_  Members   -  Reputation: 116

Like
0Likes
Like

Posted 24 August 2009 - 12:50 AM

Sorry, but I dont understand the distinction between a vector and a point. Isn't a point just a vector from <0, 0> ?

### #11scgames  Members   -  Reputation: 2073

Like
0Likes
Like

Posted 24 August 2009 - 01:06 AM

Quote:
 Sorry, but I dont understand the distinction between a vector and a point. Isn't a point just a vector from <0, 0> ?
I'm not going to try to answer the question from a rigorous mathematical perspective, but I will say that yes, in practice, points are often considered as displacements from the origin in the context of games and graphics programming.
Quote:
 I would have thought specifying points in 2D space would have made it obvious that I meant 2D vectors :)
Not really. Whether you're using separate point and vectors classes or using a vector class to represent both vectors and points, it's still important to use correct terminology when talking about geometrical problems.

The phrase 'angle between two points' isn't self-explanatory (IMO, at least). I can think of at least two possible interpretations:

1. It's the angle between the vectors from the origin to each point that you're interested in.

2. It's the absolute angle of the vector from one point to the other that you're interested in.

The rest of your post makes it pretty clear that it's number 2 that you're after, in which case the 'atan2' solution suggested by shadowcomplex is probably the most straightforward solution.

The 'acos' method can be used to compute the unsigned relative angle between two vectors. With a little extra work it can be used to compute the signed angle as well, in which case you could solve the stated problem by finding the relative angle between the vector from A to B and the x axis.

However, it's more straightforward to use atan2 if you have it available. (Personally, I prefer to use atan2 for all 'angle between vectors' problems, as it eliminates the need for the input vectors to be unit-length, and sidesteps some numerical issues that have to be dealt with when using the 'acos' method.)

### #12Melekor  Members   -  Reputation: 379

Like
0Likes
Like

Posted 24 August 2009 - 01:08 AM

Quote:
 Original post by _Sauce_Sorry, but I dont understand the distinction between a vector and a point. Isn't a point just a vector from <0, 0> ?

A point is a location

A vector is a direction and a magnitude

It makes sense to talk about the angle between two directions, but there is no such thing as an angle between two locations. What's the angle between New York and Sydney?

### #13ma_hty  Members   -  Reputation: 100

Like
0Likes
Like

Posted 24 August 2009 - 01:20 AM

Well, your high school math teacher really didn't do his job very well.

Points and vectors are two different mathematics concepts. For instance, addition of two vectors is well defined and is having an obvious geometric analogy behind. So, do you know what is the meaning of adding two points together? Frankly, I have no idea. Maybe, you can define a custom meaning on your own. However, it is redundant for you to do that.

Even if points and vectors behave in the same way in your case, you still want to use the technical terms in their conventional way for more effective communication. Just in case you forgot, the primary objective of writing is communication.

### #14phresnel  Members   -  Reputation: 949

Like
0Likes
Like

Posted 24 August 2009 - 01:24 AM

Quote:
 Original post by MelekorIt makes sense to talk about the angle between two directions, but there is no such thing as an angle between two locations. What's the angle between New York and Sydney?

### #15Interesting Dave  Members   -  Reputation: 100

Like
0Likes
Like

Posted 24 August 2009 - 01:47 AM

What are the GPS co-ords of new york and sydney, or should it actually be called GVS?

### #16phresnel  Members   -  Reputation: 949

Like
0Likes
Like

Posted 24 August 2009 - 01:56 AM

Quote:
 Original post by Interesting DaveWhat are the GPS co-ords of new york and sydney, or should it actually be called GVS?

Elaborate.

### #17Interesting Dave  Members   -  Reputation: 100

Like
0Likes
Like

Posted 24 August 2009 - 02:01 AM

GPS would give a point = position = location, right?

the GPS co-ordinates for new york and sydney can be used with atan2 to get the angle between them (angle between two points).

Or are we saying that GPS should actually be called Global Vectoring System (/Satelites)?

### #18Ysaneya  Members   -  Reputation: 1251

Like
0Likes
Like

Posted 24 August 2009 - 02:10 AM

Quote:
 Original post by Interesting DaveGPS would give a point = position = location, right?the GPS co-ordinates for new york and sydney can be used with atan2 to get the angle between them (angle between two points).Or are we saying that GPS should actually be called Global Vectoring System (/Satelites)?

GPS would give a lattitude / longitude per city. You'd get 4 values. Atan2 takes two arguments. See a flaw ?

### #19_Sauce_  Members   -  Reputation: 116

Like
0Likes
Like

Posted 24 August 2009 - 02:12 AM

Thanks jyk, it becomes very clear when you explain it like that :)

ma_hty, I don't know about Hong Kong, but where I went to school vector math wasn't part of the curriculum. That said, I never studied Math C, which probably covered it.

Now, obviously calculating the length of the vector between the two vectors (in order to normalise them) is pretty slow due to the sqrt(), so the atan2() method is likely faster, but would it be faster to calculate the angle using the dot product if the two vectors were already unit-length?

Just as an off-the-side here, I'm not certain I'm understanding the dot product method 100% - do both of the input vectors have to be normalised in order for it to work, or just the vector that lies between them?

### #20phresnel  Members   -  Reputation: 949

Like
0Likes
Like

Posted 24 August 2009 - 02:24 AM

Quote:
 Original post by Interesting DaveOr are we saying that GPS should actually be called Global Vectoring System (/Satelites)?

There are devices that only give you the direction and magnitude to a target entity (a better compass), so I think it's not the perfect example.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS