右のアナログ スティックを使用して遅延回転効果を作成しようとしています。以下のコードは、右アナログ スティックの入力に基づいて角度を取得し、オブジェクトを着実に近づけます。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 度より大きくするということです。これにより、方向を逆にする必要がなくなります。