- Home /
Whats the source code of Quaternion.FromToRotation?
Hello,
id like to know the source code of FromToRotation, or if someone knows the FromToRotation equivalent of ue4 c++ would be better ; )
Answer by Bunny83 · Sep 30, 2019 at 07:14 PM
How is that relevant? That's a pure mathematical thing. It's probably just something along the lines:
public static Quaternion FromToRotation(Vector3 aFrom, Vector3 aTo)
{
Vector3 axis = Vector3.Cross(aFrom, aTo);
float angle = Vector3.Angle(aFrom, aTo);
return Quaternion.AngleAxis(angle, axis.normalized);
}
Of course AngleAxis should be pretty straight forward as well
public static Quaternion AngleAxis(float aAngle, Vector3 aAxis)
{
aAxis.Normalize();
float rad = aAngle * Mathf.Deg2Rad * 0.5f;
aAxis *= Mathf.Sin(rad);
return new Quaternion(aAxis.x, aAxis.y, aAxis.z, Mathf.Cos(rad));
}
All untested.
@Bunny83 Thanks its working, as i mentioned i mainly search for a c++ equivalent of it but cause i havent found one ill try get the source of it to ue4 c++.
Why do you ask unreal related questions on Unity answers? Please note that Unity answers is for questions related to development with the Unity engine. Abusing this site might get your account banned. I see you asked several questions about unreal already . It's a bit impudent to ask such questions here.
Sry, I did not think anything about it. I thought its ok cause there isnt really a website for command equivalents.
This code doesn't handle the case, when two vectors are in the opposite direction.
That's because it's a degenerate case. There are infinite solutions when the vectors are opposite.
II understand, but Unity's Quaternion.FromToRotation somehow handled these cases and return 180 degrees rotations.
Your answer
![](https://koobas.hobune.stream/wayback/20220612212828im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
3D nested Turret Prefab Rotation 1 Answer
Random perpendicular vector3 after rotation 1 Answer
Problem with rotation around axis 1 Answer
Quaternion into Vector3 1 Answer
Rotation, Vector3 and transform.forward 2 Answers