我无法计算线条/光线是否与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
}
这就是我被困的地方......
我知道它之前已经得到了回答,但我不能让它对我的系统起作用所以非常感谢一些帮助。
您现有的dotProd测试只是告诉您光线的方向是沿着法线还是远离它。
你可能想做的是:
s
,结束距离是e
那么它必然是沿着线的s / (s - e)
点;“你能告诉我投影到2D的方法吗?”
也许这会有所帮助。请注意,矩形的投影是凸四边形(通常不是矩形),因此您需要进行四次LeftOf( )
测试以验证是否包含投影点。
LeftOf( )
in many place, including this textbook.
不要忘记测试矩形是否位于与xy平面正交的平面中,在这种情况下,您应该投影到xz或yz平面。