Angle between two vectors

Started by
8 comments, last by dande 18 years, 2 months ago
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!!
Dan FeketeThe Shadowhand Co.If everybody has it's own matrix, then mine is an identity
Advertisement
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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));
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!
Dan FeketeThe Shadowhand Co.If everybody has it's own matrix, then mine is an identity
Quote:Original post by jyk
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));


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. :(((
Could you please help me, what the heck I am doing wrong??

Dan FeketeThe Shadowhand Co.If everybody has it's own matrix, then mine is an identity
Here is what i have used

float CVector2D::getAngle(CVector2D & rVector)
{
return atan2(rVector.m_y - m_y , rVector.m_x - m_x);
}
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 :)
Quote:Original post by jyk
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 :)


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
Dan FeketeThe Shadowhand Co.If everybody has it's own matrix, then mine is an identity
dir = Math.toDegrees(Math.atan2(pos.perpDot(dirVector), pos.dot(dirVector)));


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

We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
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!
Dan FeketeThe Shadowhand Co.If everybody has it's own matrix, then mine is an identity

This topic is closed to new replies.

Advertisement