【发布时间】:2015-08-27 10:08:46
【问题描述】:
我无法计算线/射线是否与 3d 空间中的矩形(即平面上)相交。
我已经搜索过,唯一找到的是Ray and square/rectangle intersection in 3D,但我只是不太了解最后的步骤以及如何将其应用于我的系统。
所以我有一条射线
struct Ray
{
Vector3 m_startPoint; // P0
Vector3 m_direction; // Direction Unit Vector
float m_length; // Ray length
};
我有一个由
定义的四边形struct Quad
{
Vector3 p1;
Vector3 p2;
Vector3 p3;
Vector3 p4;
Vector3 normal;
}
我首先做的是使用点积计算光线是否会击中平面
float dotProd = D3DXVec3Dot(&ray.m_direction, &quad.normal);
if (dotProd < 0) // if <0 ray will travel into the plane
{
// Get the point of intersection
float distToIntersection
//Vector3D intersectPoint = ray.m_startPoint + (distToIntersection * ray.m_direction);
// Check whether the point of intersection is within the bounds of the Quad
}
这就是我卡住的地方......
我知道它之前已经回答过,但我无法让它适用于我的系统,所以非常感谢一些帮助。
【问题讨论】:
-
我不确定您的
Quad结构是否完全定义了一个四边形。即使我将它们折叠到飞机上,我怎么知道例如? (0, 0) 和 (1, 1) 是否定义了以 (1, 1) 为基向量的零高度四边形或以 (0, 1) 和 (1, 0) 为基向量的 1x1 四边形,或任何完全其他四边形?您是否假设轴对齐? -
@Tommy 嗨,嗯,可能。我将调整 Quad 定义,使其更易于使用。
-
我建议您首先执行ray-plane intersection 计算,然后,一旦您在平面上确定了交点,确定它是在矩形内部还是外部(通过投影到 2D)。
-
@JosephO'Rourke 这就是我想做的事情,你能告诉我投影到 2D 的方法吗?谢谢
标签: c++ geometry intersection raytracing