通过最短边将角度旋转到目标角度

时间:2013-11-20 23:32:01

标签: c# xna rotation angle

嗨,谢谢你的阅读。


我需要改变我写的这个空白,以便它可以用负角度工作。

此功能的目标是通过向顺时针或逆时针(+或 - )添加INCREMENT将ANGLE旋转到DIRECTION。

然而,我所说的问题是它不适用于小于0或大于360(2pi)的数字。我也需要能够使用负角度。

我尝试了几种东西但是暂时无法工作。任何人都可以帮我一把吗?我将不胜感激。 :d


public void ToDirection(float Increment, float Direction)
{
    if (CurrentAngle != Direction)
    {
        float ClockwiseDifference;
        float CounterClockwiseDifference;
        //Clockwise
        if (Direction < CurrentAngle)
        {
            ClockwiseDifference = CurrentAngle - Direction;
        }
        else
        {
            ClockwiseDifference = Constants.Rotation_360 - (Direction - CurrentAngle);
        }
        //CounterClockwise
        if (Direction > CurrentAngle)
        {
            CounterClockwiseDifference = Direction - CurrentAngle;
        }
        else
        {
            CounterClockwiseDifference = Constants.Rotation_360 - (CurrentAngle - Direction);
        }
        float CurrentFaceSpeed = Increment;
        if (ClockwiseDifference == CounterClockwiseDifference)
        {
            if (Globals.Randomizer.Next(0, 2) == 0)
            {
                if (ClockwiseDifference < CurrentFaceSpeed)
                {
                    CurrentAngle = Direction;
                }
                else
                {
                    CurrentAngle -= CurrentFaceSpeed;
                }
            }
            else
            {
                if (CounterClockwiseDifference < CurrentFaceSpeed)
                {
                    CurrentAngle = Direction;
                }
                else
                {
                    CurrentAngle += CurrentFaceSpeed;
                }
            }
        }
        else if (ClockwiseDifference < CounterClockwiseDifference)
        {
            if (ClockwiseDifference < CurrentFaceSpeed)
            {
                CurrentAngle = Direction;
            }
            else
            {
                CurrentAngle -= CurrentFaceSpeed;
            }
        }
        else
        {
            if (CounterClockwiseDifference < CurrentFaceSpeed)
            {
                CurrentAngle = Direction;
            }
            else
            {
                CurrentAngle += CurrentFaceSpeed;
            }
        }
    }
    if (CurrentAngle >= Constants.Rotation_360)
    {
        CurrentAngle -= Constants.Rotation_360;
    }
    else if (CurrentAngle < 0)
    {
        CurrentAngle += Constants.Rotation_360;
    }
}

2 个答案:

答案 0 :(得分:2)

简单地解开角度。然后你将始终拥有从0到360的角度。展开起始角度和目标角度,然后轮到你。这是一个有效的例子(你真正需要的唯一方法是UnwrapAngle())。

internal class Program {

    private static object UnwrapAngle(double angle) {
        if (angle >= 0) {
            var tempAngle = angle % 360;
            return tempAngle == 360 ? 0 : tempAngle;
        }
        else
            return 360 - (-1 * angle) % 360;
    }

    private static void TestUnwrap(double angle, double expected) {
        Console.WriteLine(String.Format("{0} unwrapped = {1}, expected {2}", angle, UnwrapAngle(angle), expected));
    }

    private static void Main(string[] args) {
        TestUnwrap(0, 0);
        TestUnwrap(360, 0);
        TestUnwrap(180, 180);
        TestUnwrap(360 + 180, 180);
        TestUnwrap(-270, 90);
        TestUnwrap(-270 - 720, 90);
        TestUnwrap(-725, 355);
        Console.ReadLine();
    }
}

答案 1 :(得分:1)

这个答案似乎很好地涵盖了这个主题: Work out whether to turn clockwise or anticlockwise from two angles

RE:小于0或大于360的角度。基本上,-10与350相同.720与360相同。因此,如果转换入射角度使其位于0到360之间,则全部你的问题已经解决了(假设你的代码适用于你建议的0到360之间的值)。

这是我自己做过的事情(以前用不同的语言):

var wantDir;
var currDir;
var directiondiff;
var maxTurn;

// want - this is your target direction \\
wantDir = argument0;

// max turn - this is the max number of degrees to turn \\
maxTurn = argument1;

// current - this is your current direction \\
currDir = direction;

if (wantDir >= (currDir + 180))
{
    currDir += 360;
}
else
{
    if (wantDir < (currDir - 180))
    {
        wantDir += 360;
    }
}

directiondiff = wantDir - currDir;

if (directiondiff < -maxTurn)
{
    directiondiff = -maxTurn
}

if (directiondiff > maxTurn)
{
    directiondiff = maxTurn
}

// return the resultant directional change \\
return directiondiff