【问题标题】:How calc intersection plane and line (Unity3d)如何计算相交平面和线(Unity3d)
【发布时间】:2019-10-28 18:57:33
【问题描述】:

我尝试计算相交平面和线,但我认为得到错误的结果。 试试这段代码(来自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;
}

【问题讨论】:

  • 如果你这样做是为了学习,那就继续吧。如果你打算在你的游戏中使用它,那么也许你应该停下来,改用 Physics.Raycast/Linecast。

标签: c# unity3d intersection


【解决方案1】:

但我认为得到错误的结果。

你能说得更具体点吗?

您使用的方程式似乎是正确的;虽然我会使用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

这正是您的代码所做的......所以......?

您可以在此处阅读 wiki 页面上的更多详细信息:

https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection

编辑:实际上,看起来这段代码假设如果 dotDenominator 为零,则它们不相交;部分正确;如果 dotNumerator 也为零,则线正好在平面的顶部,并且所有点都相交,因此将您想要的任何点作为交点(例如 planePoint)。

【讨论】:

  • 我正在尝试为我在 Unity 中编写的着色器计算这个计算:对于 point_on_plane 和 point_on_line 值,如果我的平面以 0、0、0 为中心,我可以使用'-point_on_line' 部分?这只是线上的任意点,还是“开始”或“结束”?我很困惑……
猜你喜欢
  • 1970-01-01
  • 2011-11-02
  • 2011-08-05
  • 2012-02-14
  • 2015-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多