Angle between vectors
Hi, I am trying to write a C++ program that gets angles between vector.
I know how to get a direction of a vector..due to the degree of rotation.
But what I don't fully understand is the angle between 2 vectors.
For example if I have two vectors at point1(6,4) and point2(10,3)
to make point1 rotate in order to face point2 I have to subtract
new point(10-6, 3-4) atan(4/-1)..but thing is I don't understand how that is the angle between the 2 vectors..
When I subtract 2 vectors it gives me a new vector. which tells me how much I need to move to get to the endpoint. so from the above explanation..to move from point to point2 I need a velocity of newpoint(4,-1)
to get the angle between them..doesn't make much sense to me
atan2(4/-1)..does that give me the angle between the new vector and the origin (0,0) or the origin of one of the points? Can someone please clarify...thanks
If it was only one vector I understand it perfectly.but I just can't visualize the angle between 2 vectors.
The angle in-between is the arccosine of the dot product of the two vectors. Simple! :D
EDIT: But if your vectors aren't normalized, you'll probably get some strange results!
It shouldn't be?
Originally Posted by Sipher
Mathematical formula for those who prefer:
v1 * v2 = |v1||v2|cos(fi)
--> arccos((v1*v2)/(|v1||v2|)) = fi
Where * is dot product and || is the absolute value (or whatever it's called).
You can extend this to find the angle between two planes, by finding the angle between their normals. Also note that if you abs() the result of the dot product in the numerator, you can get the acute angle between two vectors.
I'm not using the arc cos but the arc tan or atan2(y/x)
i have to vectors..to get the angle between them i use
atan2(vector2.y-vector1.y, vector2.x-vector1.x) which gives me the amount of degrees to rotate..and then i use that to get the my new x and y direction. sin(angle)*radius..
what I don't understand where does the angle on that atan2 returns lie on the cartesian coordinate. is it between the origin and one of the vectors? It is doing my head in..
Thanks for fast reply.
by the way Elysia..long time!
A quick computer drawn picture.
Orange is vector 2. Red is vector 1.
The smaller lines are the x and y components of the vectors.
So the purple line would be the vec2.y - vec1.y, and the green would be vec1.x - vec2.x (you'll have to mirror it, sorry).
So that leaves the black line as hypotenuse, and you can see the angle in there.
I hope I got this right.
that's exactly my problem...you can form a right angled triangle...but I can never seem to do that...how do I do that..If I just draw a straight line to connect both heads of the 2 vectors...I don't get a right angled triangle..
and if i form a right triangle for each vectors.it looks like they are individual angles.. :(
Updated picture. This should now be correct.
It's simple. Take vec2.y - vec1.y. That's the length of the orange line minus the length of the red line. Since the red line is negative, it gets longer.
Then draw that line as the height of the triangle.
Then take vec2.x - vec1.x. Since vec1.x is longer than vec2.x, we get a negative length, and since we defined positive x as right, we must draw it to the left.
So draw it as the width of the triangle and connect it with the height. This is the green line.
Then simply draw the hypotenuse to connect the base and the height.
The angle fi will always be the angle between the hypotenuse and the height.
Ok my drawing sucks..the angle shaded in pink is that the angle I am looking for?
And the pink line I use to connect it..is that how I connect two vectors.. it doesn't look like a right angled trangle
You have to understand that vector arithmetics and scalar arithmetics are not the same thing. You can actually derive the cosine formula from vectors. Vectors do not always form right angled triangles.
You should draw out the x and y components of your vectors.
If you want to work purely with vectors in any coordinate system, then it's the formula I posted above that you want to use.
If you normalize the two vectors, (length = 1), then dot them, you'll get a right triangle. The dot product is the projection of the vectors onto each other.
So, http://mathdl.maa.org/images/upload_.../Dray2/dot.gif. Suppose your two vectors are v and w. The dot product is the base of that right triangle in the image. Notice the right triangle it forms. Because v is normalized, the length of the hypotenuse is 1, so the base of the right triangle (dot product) is the cosine of the angle theta. So, take the arccosine of the dot product, and you'll get theta.
Note that this works in any number of dimensions.
I think your understand your explanation..
so basically i subtract the 2 vectors..and resulting vector...i can draw it out as a right angle triangle....if i do that..will the sprite will have to move the whole degree before it settles..i will like to rotate the sprite on its vector
You cannot always form a right triangle with vectors! That only works if they are perpendicular. You can make a right triangle if you subtract their components. This is an important fact.
their components meaning the Vector.x Vector.y and so on, yeah?
That is what I mean..I'm not too big on terminology and stuff....
Yes, a vector contains two or more components. In the Cartesian coordinate system, that would be, say, x, y and z.
There is a big difference between subtracting a vector's components and subtracting vectors.
Actually, I shouldn't have said...
"You can make a right triangle if you subtract their components"
...since this only holds true if the vector's components are perpendicular. Meh. But if you're using Cartesian, they always are.
Well, they are in most coordinate systems, because it's convenient.