2016-06-10 4 views
2

나는 올바른 아날로그 스틱으로 지연된 회전 효과를 만들려고 시도해 왔습니다. 아래의 코드는 올바른 아날로그 스틱의 입력을 기반으로 한 각도를 취하고 개체를 꾸준히 가깝게 만듭니다. atan2가 -pi에서 pi의 범위이기 때문에 변화하는 회전은 항상 pi가 아닌 0 라디안을 통해 이동하는 것이 좋습니다. 각도를 반대 방향으로 움직이는 방법이 있습니까?지연 회전에 atan2 사용

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); 
    } 

편집 : 0 문제에 2pi 사이의 전환을 발생 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도 여행 할 수있는 각도를 만들 것입니다. 이렇게하면 방향을 바꿀 필요가 없습니다.

+0

여기서 찾고있는 키워드는 트위닝입니다. – craftworkgames

답변

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; 
    } 
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; 
} 
+0

나는 이것과 비슷한 것을 시도했다. (당신의 코드는 나의 것보다 낫다.) 그러나 이것은 2PI에서 다시 0으로 갈 때의 문제를 반전시킨다. 내가 생각할 수있는 유일한 해결책은 0보다 작고 2PI보다 큰 라디안을 사용하여 2PI에서 0으로의 어색한 전환을 제거하는 것입니다. 슬프게도, 제한된 프로그래밍 경험으로 인해 어떻게해야하는지 알지 못합니다. 그런데 도움을 주셔서 감사합니다. –