我有两个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 %f\n", dist);
}
答案 0 :(得分:1)
您只需使用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