From 6a688a29911914f35e6f567c651be8652ac85c05 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 1 Aug 2024 01:27:23 +0800 Subject: [PATCH] Added GetRotateMatrix() in Matrix.h --- inc/hgl/math/Matrix.h | 2 ++ src/Math/Matrix4f.cpp | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/inc/hgl/math/Matrix.h b/inc/hgl/math/Matrix.h index 9d87d32..e88bc31 100644 --- a/inc/hgl/math/Matrix.h +++ b/inc/hgl/math/Matrix.h @@ -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 diff --git a/src/Math/Matrix4f.cpp b/src/Math/Matrix4f.cpp index 6624b56..5da0110 100644 --- a/src/Math/Matrix4f.cpp +++ b/src/Math/Matrix4f.cpp @@ -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