我已经写了一个简单的小帮手方法来计算从一个点到一个平面的距离。但是,它似乎正在返回无意义的结果。我创建飞机的代码是这样的:与飞机的距离
Plane = new Plane(vertices.First().Position, vertices.Skip(1).First().Position, vertices.Skip(2).First().Position);
相当简单,我希望你会同意。它使用三个点创建XNA平面结构。
现在,立即在此之后我做的:
foreach (var v in vertices)
{
float d = Math.Abs(v.ComputeDistance(Plane));
if (d > Constants.TOLERANCE)
throw new ArgumentException("all points in a polygon must share a common plane");
}
使用同一套我用来构建平面的顶点,我得到抛出异常!在数学上这是不可能的,因为这三点必须在飞机上。
我ComputeDistance方法是:
public static float ComputeDistance(this Vector3 point, Plane plane)
{
float dot = Vector3.Dot(plane.Normal, point);
float value = dot - plane.D;
return value;
}
ASI的理解,这是正确的。那么我会做错什么呢?或者我可能会遇到XNA实施中的错误?
一些示例数据:
Points:
{X:0 Y:-0.5000001 Z:0.8660254}
{X:0.75 Y:-0.5000001 Z:-0.4330128}
{X:-0.75 Y:-0.5000001 Z:-0.4330126}
Plane created:
{Normal:{X:0 Y:0.9999999 Z:0} D:0.5} //I believe D should equal -0.5?
Distance from point 1 to plane:
1.0
“Constants.TOLERANCE”的值是多少? – DGH 2010-12-14 05:30:20
TOLERANCE = 1e-5f – Martin 2010-12-14 05:31:16
然而,宽容不是问题。正如你从新的例子中看到的,我刚刚在远处编辑的数据相对较大。 – Martin 2010-12-14 05:31:52