Normalise orientation between 0 and 360

前端 未结 9 1631
不知归路
不知归路 2021-01-30 10:58

I\'m working on a simple rotate routine which normalizes an objects rotation between 0 and 360 degrees. My C# code seems to be working but I\'m not entirely happy with it. Can a

9条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 11:34

    I'd recommend making separate function for normalizing angle - it's a cleaner solution.

    public static float NormalizeEulerAngle(float angle){
        var normalized = angle % 360;
        if(normalized < 0)
            normalized += 360;
        return normalized;
    }
    

    Fiddle proving that such function works as intended: https://dotnetfiddle.net/Vh4CUa

    And then you can use it like here:

    public void Rotate(int degrees){
        orientation = NormalizeEulerAngle(orientation + degrees);
    }
    

提交回复
热议问题