开发者

Why does this simple algorthim return always same value?

开发者 https://www.devze.com 2023-02-25 02:05 出处:网络
I\'ve written a simple algrithm to do aiming in 3D space. It should return a X, Y and Z rotation that is needed to point from Start to End.

I've written a simple algrithm to do aiming in 3D space. It should return a X, Y and Z rotation that is needed to point from Start to End. For some reason it always returns the same values, regardless how I manipulate the End.

Something's gone really wrong here, but I just can't figure it out. Could someone please tell me what I am doing wrong?

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开发者_JAVA技巧 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);
    }

The result should be in radians. Thanks in advice.


Do you notice that you use ylen twice in CalcAngle2D?

return (float)Math.Atan2((double)ylen, (double)ylen); 

Use xlen where appropriate in Math.Atan2(double y, double x) and evaluate the correctness of the program.


Shouldn't you be returning xlen in CalcAngle2D?

i.e.

return (float)Math.Atan2((double)**xlen**, (double)ylen);
0

精彩评论

暂无评论...
验证码 换一张
取 消