c++ - 3D 空间中的射线/矩形相交

标签 c++ geometry intersection raytracing

我无法计算线/射线是否与 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
}

这就是我卡住的地方......

我知道之前有人回答过这个问题,但我无法让它在我的系统上运行,所以非常感谢您的帮助。

最佳答案

"Could you show me the method of projecting onto 2D?"

也许这会有所帮助。请注意矩形的投影是一个凸四边形(通常不是矩形),因此您需要四个 LeftOf( ) 测试来验证是否包含 的投影点。


RectProjection
你可以在很多地方找到LeftOf( ),包括this textbook .

不要忘记测试矩形是否位于与 xy 平面正交的平面内,在这种情况下,您应该投影到 xz- 或yz-平面。

关于c++ - 3D 空间中的射线/矩形相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30785435/

相关文章:

c++ - 交换单链表节点创建一个无限循环

c++ - 错误 : invalid conversion from 'int' to 'const char*' [-fpermissive]

c++ - QT QRgb 构建错误

c++ - 奇怪的构造函数

javascript - Three.js 仅从一侧增加盒子几何形状

python - 3D 旋转

r - R中的不相交值

javascript - 在 three.js 中合并几何

string - 获取字符串向量元素之间的最小共享部分

python - 根据一列中的公共(public)值从两个或多个 2d numpy 数组创建交集