# Angle between two vectors into RotAxis

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 30/03/2008 at 15:58, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   R9-R10.5
Platform:   Windows  ; Mac  ;  Mac OSX  ;
Language(s) :     C++  ;

---------
I'm using this code to rotate a bone which points along the +Z axis to match another vector. Both are made relative to the origin. The results are not as expected. Any ideas (esp. with respect to the angle calculation) :

> `Vector     v1 =     Vector(0.0f,0.0f,1.0f); \> Vector     v2 =     !epBB; \> Vector     axis =     v1%v2; \> // Signed 3D angle between two vectors: atan2(dot(normal,cross(v1,v2)), dot(v1,v2)) \> Real     angle =     atan2((!axis)\*axis, v1\*v2); \> Matrix     bm; \> if (Abs(angle) < 0.00001f) \>      bm =     MatrixMove(bc->GetVector(IPP_ORIGIN)) \* MatrixScale(Vector(epBBlen)); \> else \>      bm =     MatrixMove(bc->GetVector(IPP_ORIGIN)) \* RotAxisToMatrix(!axis, angle) \* MatrixScale(Vector(epBBlen));`

As far as I can tell, the atan2 calculation is returning incorrect angles (first off, all of the bones' angles are positive which doesn't seem possible). This is the *only* algorithm that I could find online to use atan2() for getting the angle between vectors - but I'm not trusting it at the moment.

Thanks

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 31/03/2008 at 00:03, xxxxxxxx wrote:

Not sure, but why don't you use the dot product of two vectors? It returns the cosine of the angle between two vectors.

cheers,
Matthias

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 31/03/2008 at 05:34, xxxxxxxx wrote:

"Not sure, but why don't you use the dot product of two vectors? It returns the cosine of the angle between two vectors."
-A dot product gives the cosine of the angle between two vectors, but not a signed angle. However, in general I don't think that a signed angle between two 3D vectors is a well defined thing. It could be defined in many ways (compare with conversion to polar coordinates).

Often I find that this type of 3D geometry problems are easier to deal with by thinking in terms of matrices and coordinate systems, rather than in explicit rotations, translations etc. If you explain a bit more of what you are trying to achieve, then maybe we can suggest an alternative approach?

regards
/Filip

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 31/03/2008 at 06:50, xxxxxxxx wrote:

Howdy,

Well, I'm not exactly sure, but I used atan2() before, and seems like I had to cast it to a Real to get the correct value, so maybe try this?:

> `Real angle = (Real)atan2((!axis)\*axis, v1\*v2);`

Also, seems like I had to cast the parameters to a double, too, before it would compile.

Don't know if that helps.

Cactus Dan

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 31/03/2008 at 07:05, xxxxxxxx wrote:

Ok, didn't realize what the arctan2 function is doing (being not part of the Cinema SDK).

Maybe there is a better way to do this? Would VectorToHPB() help you?

cheers,
Matthias

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 31/03/2008 at 08:31, xxxxxxxx wrote:

Actually, I stumbled upon two things:

1. There is a VectorAngle() method in the SDK - I thought that there was but it took a bit to refind it. Although it uses ACos(), it appears to be working. I'd rather the atan2() since it covers all possible ranges between two vectors (-180d to 180d). If no anomalies show up, I'll continue with VectorAngle(). If they do, I'll have to retry with atan2().

2. It appears that swapping v1 and v2 in the axis/angle calculations works. The way shown above causes anomalies due to the orthonormal vector being first in the cross product (v2%v1 works, v1%v2 exhibits anomalies).

FilipM: Unfortunately, the only knowns are these two vectors. I have to apply a coordinate system/matrix subsequently so that the 'bone' matches the transformations of a related object (fun in itself). I should note that the axis/angle rotation created by the two vectors is applied to the points of a polygon object ('bone') being displayed by related object's Draw() method.