Unity坐标系输入两个方向向量求四元数
inline glm::quat FromToRotation(glm::vec3 fromDirection, glm::vec3 toDirection)
{
if (fromDirection == glm::vec3(0.0) || toDirection == glm::vec3(0.0))
return glm::quat(1.0, 0.0, 0.0, 0.0);
fromDirection = glm::normalize(fromDirection);
toDirection = glm::normalize(toDirection);
if (fromDirection == toDirection)
return glm::quat(1.0, 0.0, 0.0, 0.0);
glm::vec3 aixs = glm::cross(fromDirection, toDirection);//叉乘的aixs的模长即为正弦值
float sinThelta = glm::length(aixs);
float cosThelta = glm::dot(fromDirection, toDirection);
float sinHalfThelta = glm::sqrt((1 - cosThelta) / 2);//计算sin半角 可以避免cos半角的取正负号问题
float cosHalfThelta = sinThelta / (2 * sinHalfThelta);
aixs = glm::normalize(aixs);//规范化一下
return glm::quat(cosHalfThelta, sinHalfThelta*aixs);
}//输入2个方向 转四元数