suggest and question?why calcAngle(SimpleVector vec) only less than 180°

Started by Uncle Ray, July 31, 2014, 09:34:35 AM

Previous topic - Next topic

Uncle Ray

i know the most important feature of simplevector is that,every simplevector have a direction.
for example ,if i want cal the angle of a:(0,0,-1),b(0,0,1).
if use a.calcAngle(b),
the result is 0.
in fact the result is wrong,in the reality,the result is pi/2.

may be i can use other math method,but it is not convenient.

Any suggest?i just want cal the more correcter angle of simplevector,which has a direction.




Uncle Ray


EgonOlsen

Quote from: Uncle Ray on July 31, 2014, 09:34:35 AM
if i want cal the angle of a:(0,0,-1),b(0,0,1).
if use a.calcAngle(b),
the result is 0.
in fact the result is wrong,in the reality,the result is pi/2.
No, actually the result whould by PI, not PI/2. And that's what the method returns. I'm not sure where you are getting this 0 from. This little test case:


SimpleVector s1 = new SimpleVector(0, 0, 1);
SimpleVector s2 = new SimpleVector(0, 0, -1);
System.out.println(s1.calcAngle(s2));
System.out.println(s2.calcAngle(s1));


prints out PI in both cases.

Uncle Ray

it is my fault,i made a mistake,pi=180 actually,but i  thought it is 360,before..
the question is,i want cal the angle of two SimpleVector.which is 270.
but use calAngle,it is only,90.
how can i cal?whose angle more than 180?

EgonOlsen

That's because the calculation makes no assumptions about the relation between the two vectors and the history of their rotations. It calculates the angle between the two vectors without any sign or order. That's not always what one wants, but that's out of the scope of this method. In your case, it depends on your use case. For example: If your vectors lie in the xz-plane and only rotate around the y-axis, you can simply do a check if the x value of vector 2 is smaller than the x value of vector 1 and if it is, you can subtract the returned value from 2*PI to get the angle that you want.