【问题标题】:Rotate angle to target angle via shortest side通过最短边将角度旋转到目标角度
【发布时间】:2013-11-20 23:32:00
【问题描述】:

您好,感谢您的阅读。


我需要更改我写的这个空白,以便它可以处理负角。

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

但是,正如我所说的,它不适用于小于 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;
    }
}

【问题讨论】:

  • 我没看出问题,你可以加减360,直到你的角度在[0-360]区间内。

标签: c# xna rotation angle


【解决方案1】:

只需展开角度。然后您将始终拥有从 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();
    }
}

【讨论】:

    【解决方案2】:

    这个答案似乎很好地涵盖了这个主题: 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多