can anyone see whats wrong with this angle between vectors function?

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

Recommended Posts

this function is supposed to get angle between vectors, but it doesnt. it gets a tiny number. something like 0.0000034221. when i know the angle is 45 degrees. can anyone help
public static float AngleBetweenVectors(Vector point1, Vector point2)
//finds the angle between two vectors
{
float theta = (float)Dot(point1,point2) / (Length(point1) * Length(point2));

theta = (float)Math.Acos((double)theta);
return theta;
}


heres my DOT function, in case its needed:
public static double Dot(Vector point1, Vector point2)
//gets the dot product of two vectors
{
double dot = (point1.Xval * point2.Xval + point1.Yval * point2.Yval);
return dot;
}


and heres the LENGTH function in case thats needed!
public static float Length(Vector point)//gets the length of a vector
{
float length = (float)(Math.Sqrt(point.Xval*point.Xval + point.Yval*point.Yval));
return length;
}



Fruny - Formatting

Share on other sites
For an angle of 45 degrees, the function should return about 0.7854, that is, π/4 radians.

Share on other sites
needs to be normalised before dotproduct!

Share on other sites
Changing your "CODE" tags to lowercase should work (or use "source").

One thing, acos functions often return radian units for an angle, but that won't be your only problem if that 0.0000034221 value is really what you got for 45 deg. Not sure what units your Math.Acos returns. Other than the possible unit issue, everything appears fine to me.

You should be able to run your debugger and just look at each calculation. Is Length returning right value?, Is Dot?, etc. If you don't know how to use debugger, put in some cout statements at appropriate places.

Give us the X and Y values for point1 and point2 you are using in your test.

I'm not sure about all the type casting. Maybe need extra parens here:
theta = (float)(Math.Acos(...));
(That would match what you did in Length for Math.Sqrt.)
What precision are your vector.Xval and Yval stored as?

Share on other sites
please post the 2 vectors that you are using for your test.

-me

Share on other sites
Quote:
 Original post by Anonymous Posterneeds to be normalised before dotproduct!

float theta = (float)Dot(point1,point2) / (Length(point1) * Length(point2));

Share on other sites
Yea either, your vectors are wrong or you are not intepreting your results correctly. Java == BLAH, C++ is my choice [grin]

#include <iostream.h>#include <math.h>struct Vector{	float Xval, Yval;};double Dot(Vector point1, Vector point2);float Length(Vector point);float AngleBetweenVectors(Vector point1, Vector point2);float AngleBetweenVectors(Vector point1, Vector point2)//finds the angle between two vectors{  float theta = (float)Dot(point1,point2) / (Length(point1) * Length(point2));  theta = (float)acos((double)theta);  return theta;}double Dot(Vector point1, Vector point2)//gets the dot product of two vectors{  double dot = (point1.Xval * point2.Xval + point1.Yval * point2.Yval);  return dot;}float Length(Vector point)//gets the length of a vector{  float length = (float)(sqrt(point.Xval*point.Xval + point.Yval*point.Yval));  return length;}int main(int argc, char* argv[]){	Vector V1;	V1.Xval = 0;	V1.Yval = 5;	Vector V2;	V2.Xval = 5;	V2.Yval = 0;	cout << (180*AngleBetweenVectors(V1,V2)/3.14159265);	return 0;}

Displays: 90 so your code is correct! Make sure you convert to degrees from radians.

- Drew

Share on other sites
Quote:
 Original post by Anonymous Posterneeds to be normalised before dotproduct!

[EDIT - this is wrong - see other replies below]
While technically his Dot function is only a valid dot product for a unit vector, he is still using it correctly since he normalizes before taking the arccos. However, I would agree that that could be dangerous in the future if you don't realize that the Dot function isn't normalizing internally. But should still work fine in this example - I see Fruny beat me - again. :)

[Edited by - reana1 on March 14, 2005 3:00:57 PM]

Share on other sites
Quote:
 Original post by reana1While technically his Dot function is only a valid dot product for a unit vector, he is still using it correctly since he normalizes before taking the arccos.

Computing a dot product never required your vectors to be normalized.

Quote:
 However, I would agree that that could be dangerous in the future if you don't realize that the Dot function isn't normalizing internally.

If it did so, it wouldn't be a dot product anymore... indeed, it would only be one if the vectors were unit vectors. Precisely the opposite of what you assert.

Share on other sites
Quote:
Original post by Fruny
Quote:
 Original post by reana1While technically his Dot function is only a valid dot product for a unit vector, he is still using it correctly since he normalizes before taking the arccos.

Computing a dot product never required your vectors to be normalized.

Quote:
 However, I would agree that that could be dangerous in the future if you don't realize that the Dot function isn't normalizing internally.

If it did so, it wouldn't be a dot product anymore.

Strange, I guess it depends on the true definition of dot product, but I had always understood it to include the normalization. And in my reference that's how it's defined. But as long as it's used correctly I guess it's just semantics. I'll have to check more refs later ...

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• Forum Statistics

• Total Topics
634102
• Total Posts
3015534
×