我写了一个简单的算法来做3D空间瞄准。它应该返回指示从Start
到End
所需的X,Y和Z旋转。 出于某种原因,它始终返回相同的值,无论我如何操纵End
。
这里有些事情真的不对,但我无法弄清楚。有人能告诉我我做错了什么吗?为什么这个简单的algorthim总是返回相同的值?
public static void CalcAngle(Vector3 start, Vector3 end, out float xAngle,
out float yAngle, out float zAngle, bool Radians)
{
Vector2 xzPlaneStart = new Vector2(start.X, start.Z);
Vector2 xzPlaneEnd = new Vector2(end.X, end.Z);
Vector2 xyPlaneStart = new Vector2(start.X, start.Y);
Vector2 xyPlaneEnd = new Vector2(end.X, end.Y);
Vector2 zyPlaneStart = new Vector2(start.Z, start.Y);
Vector2 zyPlaneEnd = new Vector2(end.Z, end.Y);
float xrot, yrot, zrot;
xrot = yrot = zrot = float.NaN;
xrot = CalcAngle2D(zyPlaneStart, zyPlaneEnd); //Always 0.78539
yrot = CalcAngle2D(xzPlaneStart, xzPlaneEnd); //Always -2.3561945
zrot = CalcAngle2D(xyPlaneStart, xyPlaneEnd); //Always 0.78539
if (Radians)
{
xAngle = xrot;
yAngle = yrot;
zAngle = zrot;
}
else
{
xAngle = MathHelper.ToDegrees(xrot);
yAngle = MathHelper.ToDegrees(yrot);
zAngle = MathHelper.ToDegrees(zrot);
}
}
public static float CalcAngle2D(Vector2 v, Vector2 end)
{
float xlen = end.X - v.X;
float ylen = end.Y - v.Y;
return (float)Math.Atan2((double)ylen, (double)ylen);
}
结果应该是弧度。 谢谢你的建议。
你传递相同的参数CalcAngle()? – 2011-04-13 12:37:18
在你的问题或你的代码中错字? 'Math.Atan2((double)ylen,(double)ylen)' - 即你在这里似乎没有使用xlen。 – 2011-04-13 12:38:39
这是错误的! 我是个白痴。认真:) – alex 2011-04-13 12:45:07