如何计算相交平面和线(Unity3d)
How calc intersection plane and line (Unity3d)
我尝试计算平面和直线的交点,但我认为得到了错误的结果。
尝试此代码(从 http://wiki.unity3d.com/index.php/3d_Math_functions 获取):
public static bool LinePlaneIntersection(out Vector3 intersection, Vector3 linePoint, Vector3 lineVec, Vector3 planeNormal, Vector3 planePoint)
{
float length;
float dotNumerator;
float dotDenominator;
Vector3 vector;
intersection = Vector3.zero;
//calculate the distance between the linePoint and the line-plane intersection point
dotNumerator = Vector3.Dot((planePoint - linePoint), planeNormal);
dotDenominator = Vector3.Dot(lineVec, planeNormal);
if (dotDenominator != 0.0f)
{
length = dotNumerator / dotDenominator;
vector = SetVectorLength(lineVec, length);
intersection = linePoint + vector;
return true;
}
else
return false;
}
but I think get wrong result.
能具体点吗?
您使用的等式似乎是正确的;尽管我会使用 lineVec.noramlized * length
而不是那个奇怪的 SetVectorLength
函数。
直线与平面相交的基本方程是直线上的点x,其中x的值由下式给出:
a = (point_on_plane - point_on_line) . plane_normal
b = line_direction . plane_normal
if b is 0: the line and plane are parallel
if a is also 0: the line is exactly on the plane
otherwise: x = a / b
因此交点为:x * line_direction + line_point
这正是您的代码所做的……所以……?
您可以在此处的维基页面上阅读更多详细信息:
https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection
编辑:实际上,这段代码假设如果点分母为零,则它们不相交;部分正确;如果 dotNumerator 也为零,则该线恰好位于平面的顶部,并且所有点相交,因此将您想要的任何点作为交点(例如 planePoint
)。
我尝试计算平面和直线的交点,但我认为得到了错误的结果。 尝试此代码(从 http://wiki.unity3d.com/index.php/3d_Math_functions 获取):
public static bool LinePlaneIntersection(out Vector3 intersection, Vector3 linePoint, Vector3 lineVec, Vector3 planeNormal, Vector3 planePoint)
{
float length;
float dotNumerator;
float dotDenominator;
Vector3 vector;
intersection = Vector3.zero;
//calculate the distance between the linePoint and the line-plane intersection point
dotNumerator = Vector3.Dot((planePoint - linePoint), planeNormal);
dotDenominator = Vector3.Dot(lineVec, planeNormal);
if (dotDenominator != 0.0f)
{
length = dotNumerator / dotDenominator;
vector = SetVectorLength(lineVec, length);
intersection = linePoint + vector;
return true;
}
else
return false;
}
but I think get wrong result.
能具体点吗?
您使用的等式似乎是正确的;尽管我会使用 lineVec.noramlized * length
而不是那个奇怪的 SetVectorLength
函数。
直线与平面相交的基本方程是直线上的点x,其中x的值由下式给出:
a = (point_on_plane - point_on_line) . plane_normal
b = line_direction . plane_normal
if b is 0: the line and plane are parallel
if a is also 0: the line is exactly on the plane
otherwise: x = a / b
因此交点为:x * line_direction + line_point
这正是您的代码所做的……所以……?
您可以在此处的维基页面上阅读更多详细信息:
https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection
编辑:实际上,这段代码假设如果点分母为零,则它们不相交;部分正确;如果 dotNumerator 也为零,则该线恰好位于平面的顶部,并且所有点相交,因此将您想要的任何点作为交点(例如 planePoint
)。