2

右のアナログ スティックを使用して遅延回転効果を作成しようとしています。以下のコードは、右アナログ スティックの入力に基づいて角度を取得し、オブジェクトを着実に近づけます。atan2 は -pi から pi の範囲であるため、変化する回転は常に pi ではなく 0 ラジアンを移動することを優先します。角度を反対方向に移動させる方法はありますか?

    private void Angle()
    {
        //Angle to go to
        RotationReference = -(float)(Math.Atan2(YR, XR));

        //Adds on top of rotation to steadily bring it closer
        //to the direction the analog stick is facing
        Rotation += (RotationReference - Rotation) * Seconds *15;
        Console.WriteLine(RotationReference);
    }

編集:

2pi から 0 への移行に問題を引き起こした InBetween の提案された方法を使用してみました。これにより、私は別のことを試すようになりました。なぜうまくいかないのかわかりません。

    private void Angle()
    {
        //Angle to go to
        RotationReference = -(float)(CorrectedAtan2(YR, XR));

        //Adds on top of rotation to steadily bring it closer
        //to the direction the analog stick is facing
        if (Math.Abs(RotationReference - Rotation) > Math.PI)
            Rotation += ((float)(RotationReference + Math.PI * 2) - Rotation) * Seconds * 15;
        else Rotation += (RotationReference - Rotation) * Seconds *15;
        Console.WriteLine(RotationReference);
    }

    public static double CorrectedAtan2(double y, double x)
    {
        var angle = Math.Atan2(y, x);
        return angle < 0 ? angle + 2 * Math.PI: angle;
    }

この背後にある考え方は、180 度を超えて移動する必要がある場合、移動する角度を 360 度より大きくするということです。これにより、方向を逆にする必要がなくなります。

4

2 に答える 2

0

いくつかの実験の後、私はそれを機能させることができました。CorrectedAtan2 メソッドの InBetween に感謝します。for ループは、耳障りな遷移を回避するために pi を追加する必要があるすべてのインスタンスを通過するために使用されます。

    private float Angle(float y, float x)
    {
        //Angle to go to
        RotationReference = -(float)(CorrectedAtan2(y, x));

        for (int i = 0; i < 60; i++)
        {
            if (Math.Abs(RotationReference - Rotation) > Math.PI)
                RotationReference = -(float)(CorrectedAtan2(y, x) +
                    (Math.PI * 2 * i));
        }
        //Adds on top of rotation to steadily bring it closer
        //to the direction the analog stick is facing
        return Rotation += (RotationReference - Rotation) * Seconds * 15;
    }

    public static double CorrectedAtan2(double y, double x)
    {
        var angle = Math.Atan2(y, x);
        return angle < 0 ? angle + 2 * Math.PI: angle;
    }
于 2016-06-11T00:58:15.213 に答える
0

[0, 2·pi]範囲に収まるように角度を修正するだけです。

public static double CorrectedAtan2(double y, double x)
{
    var angle = Math.Atan2(y, x);
    return angle < 0 ? angle + 2 * Math.PI : angle;
}
于 2016-06-10T13:06:28.147 に答える