【发布时间】:2011-11-02 08:47:56
【问题描述】:
我正在尝试实现线段和平面相交测试,该测试将根据它是否与平面相交而返回真或假。它还将返回直线相交的平面上的接触点,如果直线不相交,则如果线段是射线,该函数仍应返回相交点。我使用了 Christer Ericson 的实时碰撞检测中的信息和代码,但我认为我没有正确实现它。
im 使用的平面来源于三角形的法线和顶点。找到平面上的交点位置是我想要的,不管它是否位于我用来推导平面的三角形上。
函数的参数如下:
contact = the contact point on the plane, this is what i want calculated
ray = B - A, simply the line from A to B
rayOrigin = A, the origin of the line segement
normal = normal of the plane (normal of a triangle)
coord = a point on the plane (vertice of a triangle)
这是我使用的代码:
bool linePlaneIntersection(Vector& contact, Vector ray, Vector rayOrigin, Vector normal, Vector coord) {
// calculate plane
float d = Dot(normal, coord);
if (Dot(normal, ray)) {
return false; // avoid divide by zero
}
// Compute the t value for the directed line ray intersecting the plane
float t = (d - Dot(normal, rayOrigin)) / Dot(normal, ray);
// scale the ray by t
Vector newRay = ray * t;
// calc contact point
contact = rayOrigin + newRay;
if (t >= 0.0f && t <= 1.0f) {
return true; // line intersects plane
}
return false; // line does not
}
在我的测试中,它永远不会返回 true...有什么想法吗?
【问题讨论】:
-
你最后解决了吗?
标签: c++ algorithm math vector collision-detection