我正在尝试实施Tomas Moller的三角形 - 三角形交叉测试(http://fileadmin.cs.lth.se/cs/Personal/Tomas_Akenine-Moller/pubs/tritri.pdf)。
目前,我要过滤掉从一个三角形顶点到另一个三角形所在平面的距离不为零的所有情况,并且所有距离都具有相同的符号。在我的checkForCollision
函数中,我有一条线来计算一个三角形的法线和其他顶点的点积。但Visual Studio告诉我:
Error 7 error C2440: '=' : cannot convert from 'glm::detail::tvec3<glm::mediump_float>' to `'float'`
这是我的代码:
struct triangle
{
glm::vec3 vertex0, vertex1, vertex2;
triangle(glm::vec3 vert1, glm::vec3 vert2, glm::vec3 vert3)
{
vertex0 = vert1;
vertex1 = vert2;
vertex2 = vert3;
}
};
struct triangleDistance
{
float vertex0, vertex1, vertex2;
};
int sign(const double inputNumber)
{
if (inputNumber >= 0) return 1;
else return -1;
}
bool signIsTheSame(const triangleDistance inputDistance)
{
return (sign(inputDistance.vertex0) == sign(inputDistance.vertex1) &&
sign(inputDistance.vertex1) == sign(inputDistance.vertex2));
}
bool noneAreZero(const triangleDistance inputDistance)
{
return (inputDistance.vertex0 == 0 || inputDistance.vertex1 == 0 || inputDistance.vertex2 == 0);
}
glm::vec3 computeTriangleNormal(const triangle inputTriangle)
{
glm::vec3 crossTerm1, crossTerm2;
return glm::normalize(glm::cross((inputTriangle.vertex1 - inputTriangle.vertex0),
(inputTriangle.vertex2 - inputTriangle.vertex0)));
}
bool checkForCollision(const triangle triangle1, const triangle triangle2)
{
glm::vec3 triangle2Normal = computeTriangleNormal(triangle2);
glm::vec3 triangle2d = -triangle2Normal * triangle2.vertex0;
triangleDistance triangle1Distance;
triangle1Distance.vertex0 = glm::dot(triangle2Normal, triangle1.vertex0) + triangle2d;
triangle1Distance.vertex1 = glm::dot(triangle2Normal, triangle1.vertex1) + triangle2d;
triangle1Distance.vertex2 = glm::dot(triangle2Normal, triangle1.vertex2) + triangle2d;
return (signIsTheSame(triangle1Distance) && noneAreZero(triangle1Distance));
return false;
}
答案 0 :(得分:0)
我想你可能误解了三角形 - 三角形碰撞测试算法。两个向量的点积是标量。向标量添加标量没有意义,因为编译错误。
我认为问题在于你对d_2的理解 - 来自你的网站:
d_2 = - N_2 * V_2_0
其中:
但是,在您的代码中,d_2是triangle2d,您已将其解释为矢量并计算为每个组件的矢量积:
glm::vec3 triangle2d = -triangle2Normal * triangle2.vertex0;
相反,请考虑d_2确实应该是一个标量,并计算为三角形2法线与其顶点之一的点积:
float triangle2d = glm::dot(-triangle2Normal, triangle2.vertex0);