Added GetRotateMatrix() in Matrix.h

This commit is contained in:
2024-08-01 01:27:23 +08:00
parent 7165984d22
commit 6a688a2991
2 changed files with 19 additions and 0 deletions

View File

@@ -215,6 +215,8 @@ namespace hgl
return root*child;
}
const Matrix4f GetRotateMatrix(const Vector3f &world_position,const Vector3f &cur_direction,const Vector3f &new_direction);
/**
* 带误差的比较两个浮点矩阵是否相等
* @param m1 矩阵1

View File

@@ -178,4 +178,21 @@ namespace hgl
return result;
}
/**
* 计算一个方向旋转成另一个方向的变换矩阵
*/
const Matrix4f GetRotateMatrix(const Vector3f &world_position,const Vector3f &cur_direction,const Vector3f &new_direction)
{
Vector3f axis=glm::cross(cur_direction,new_direction);
if(glm::length2(axis)<0.0001)
return Matrix4f(1.0f);
axis=glm::normalize(axis);
float angle=acos(glm::dot(cur_direction,new_direction));
return glm::rotate(Matrix4f(1.0f),angle,axis);
}
}//namespace hgl