3D空间中的射线/矩形交叉

问题描述 投票:2回答:2

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

这就是我被困的地方......

我知道它之前已经得到了回答,但我不能让它对我的系统起作用所以非常感谢一些帮助。

c++ geometry intersection raytracing
2个回答
0
投票

您现有的dotProd测试只是告诉您光线的方向是沿着法线还是远离它。

你可能想做的是:

  1. 得到起点和终点距离的距离;
  2. 如果两个距离都有相同的符号然后停止 - 没有交叉点;
  3. 否则计算交点 - 如果起始距离是s,结束距离是e那么它必然是沿着线的s / (s - e)点;
  4. 对该点执行一个矩形点测试:如果它的x在盒子的x范围内,并且它的y在盒子的y范围内,则它在内部并且光线照射到矩形。否则它没有。

0
投票

“你能告诉我投影到2D的方法吗?”

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


          RectProjection
You can find LeftOf( ) in many place, including this textbook.

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

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.