XYZ两个GLM :: MAT4矩阵的距离点对点

XYZ Distance point-to-point of two glm::mat4 matrices

本文关键字:距离 点对点 MAT4 两个 GLM XYZ      更新时间:2023-10-16

我有两个glm :: mat4 modelView矩阵,我需要比较两个XYZ点之间的距离,我尝试了此代码,但似乎并不准确。

inline GLfloat Dist3D(GLfloat X1, GLfloat Y1, GLfloat Z1,
    GLfloat X2, GLfloat Y2, GLfloat Z2) {
    return sqrt(pow((X2 - X1), 2) + pow((Y2 - Y1), 2) + pow((Z2 - Z1), 2));
}
void PlayerScore::CompareMatrixes(
    glm::mat4 Target,
    glm::mat4 Source) {
    GLfloat dist = Dist3D(
        Target[3][0], Target[3][1], Target[3][2],
        Source[3][0], Source[3][1], Source[3][2]);
    printf("Dist to target %fn", dist);
}

您只需使用glm::length来确定两个分之间的距离。

glm::vec3 v1 = {2.0, 0.0, 0.0};
glm::vec3 v2 = {6.0, 0.0, 0.0};
auto distance = glm::length(v2 - v1);
std::cout << distance << std::endl; // expected output is 4
glm::mat4 identity(1.0);
glm::mat4 m1 = glm::translate(identity, v1);
glm::mat4 m2 = glm::translate(identity, v2);
// note that the operator[] returns an entire column as vec4
distance = glm::length(m2[3] - m1[3]);
std::cout << distance << std::endl; // expected output is 4