# Angle between two vectors

This topic is 4584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! I have this very simple question. I've totally forgot how dot product in 2D space works. I am using OpenGL to render something in 2D, the origo is in the middle of the screen, everything below is negative y, and everything to the left of the origo is negative x. So I have two positions given as 2 2D vectors pos1, and pos2 for example. I dot product the pos1 with (pos2-pos1), acos -> convert to degrees, but I get totally wrong angles. And if my pos2 is ie. (200, 100) and when it is (100, 200) the angle equals. Do you guys can help me out, and explain what I am doing wrong. btw: I am using glRotatef(angle, 0.0f, 0.0f, 1.0f) for rotating the sprite. Thank you!!

##### Share on other sites
The formula is
    A dot B = |A| |B| cos θ
Thus
    θ = cos-1 ( A dot B ) / ( |A| |B| )
You probably need to divide A dot B by the lengths of the vectors (or normalize the vectors first, it's the same) before doing cos-1.

##### Share on other sites
Remember that acos() returns the angle in radians, so you'll have to convert before calling glRotate(). Also, an alternate way of finding the (signed) angle between vectors of arbitrary (non-zero) length in 2d is:
angle = atan2(perpdot(a,b), dot(a,b));

##### Share on other sites
Sorry, forgot to add, that I normalized the vectors. Do I have to use the current position vector, and the pos2-pos1 vector? Or other vectors? :s
pos1 is my current position, pos2 is the goal position.

Thanks!

##### Share on other sites
Quote:
 Original post by jykRemember that acos() returns the angle in radians, so you'll have to convert before calling glRotate(). Also, an alternate way of finding the (signed) angle between vectors of arbitrary (non-zero) length in 2d is:angle = atan2(perpdot(a,b), dot(a,b));

I have no idea why, but still doesn't work :(((((
I've tried it your way, but still no luck. I am posting some code:

public double dot(Vector2D v1) {		double dot = (this.dX*v1.dX + this.dY*v1.dY);   	return dot;}   public double perpDot(Vector2D v1) {   	double pdot =  this.dX*v1.dY-this.dY*v1.dX;	return pdot;}//...pos = new Vector2D((double)x, (double)y);goal_pos = new Vector2D(201.0, 101.0);		Vector2D dirVector = goal_pos.sub(pos);		dir = Math.toDegrees(Math.atan2(pos.perpDot(dirVector), pos.dot(dirVector)));//...gl.glRotated(dir, 0.0, 0.0, 1.0);

I've tried negating values, tried using goal_pos instead of dirVector, also tried changing parameters in the atan2 function, but still no-no for luck. :(((

##### Share on other sites
Here is what i have used

float CVector2D::getAngle(CVector2D & rVector)
{
return atan2(rVector.m_y - m_y , rVector.m_x - m_x);
}

##### Share on other sites
Ah, I see. The solutions John and I posted are for (signed or unsigned) relative angles. It looks like you want an absolute angle, in which case you should use MTclip's solution. (If you ever need the relative angle though, at least you have the answer :)

##### Share on other sites
Quote:
 Original post by jykAh, I see. The solutions John and I posted are for (signed or unsigned) relative angles. It looks like you want an absolute angle, in which case you should use MTclip's solution. (If you ever need the relative angle though, at least you have the answer :)

Thanks for all your help, for the first tries it went great, then when I did some random going there, going there it failed. Here is what I want to implement. I have a littleobject, I want it to face a position defined by a 2D vector. I worked many times with 3D engines, wrote some engines using pixelshader, and etc... never thought that in 2D finding two coordinates between an object and a point is so hard. :D:D

##### Share on other sites
dir = Math.toDegrees(Math.atan2(pos.perpDot(dirVector), pos.dot(dirVector)));

Should be
dir = Math.toDegrees(Math.atan2(dirVector.y,dirVector.x);

##### Share on other sites
Thanks all for your help. Actually I needed signed relative angle between the two vectors. The equation pointed out by jyk was working fine, I did a mistake that I wasn't multiplying like heading x directionVector, but I was using position x directionVector. I first get the current heading vector like
xh = Math.cos(dir);
yh = Math.sin(dir);
Then I used jyk's equation to multiply that with the direction vector pointing to goal position. Thanks again for your help!

1. 1
2. 2
frob
16
3. 3
4. 4
Rutin
11
5. 5

• 13
• 13
• 61
• 14
• 15
• ### Forum Statistics

• Total Topics
632125
• Total Posts
3004250

×