- Home /
Question by
Fede192 · Jun 03, 2020 at 02:16 AM ·
rotationvector3quaternionmagnitude
Rotate vector towards another vector around perpendicular vector
Given three vectors in 3D Space V0, V1 and V2, where V1 is perpendicular to V0. How could I rotate V0 around V1's axis so that it points in the direction of V2?
In other words, rotate V0 around V1 so that the projection vector Vp between V0 and V2 is of minimum magnitude.
Figure 1 is the starting point, and Figure 2 is the result I want.
vectors.png
(91.6 kB)
Comment
Best Answer
Answer by Fede192 · Jun 03, 2020 at 02:41 AM
I got the answer I was looking for here:
https://forum.unity.com/threads/vector3-angle-on-relative-axis.381886/
The code is the following:
public static float AngleOffAroundAxis(Vector3 v, Vector3 forward, Vector3 axis)
{
Vector3 right = Vector3.Cross(axis, forward).normalized;
forward = Vector3.Cross(right, axis).normalized;
return Mathf.Atan2(Vector3.Dot(v, right), Vector3.Dot(v, forward)) * (180.0f/3.14f);
}
In this case, the answer is AngleOffAroundAxis(v2, v0, v1)
Credit goes to user HiddenMonk.