How to calculate rotation from 0 to 360?

890 views Asked by At

I am trying to write an expression in After Effects, to display the degrees of rotation of an object. The problem is than, when the element is rotated counter clockwise, it doesn't start at 360 and than will count down to 0. It will show me negative numbers from 0 to - negative infinite.

When rotate clockwise it will start from 0 and after it passed the 360 it will start over again from 0.

How can I solve the counter clockwise rotation to the correct way?

x = Math.floor(thisComp.layer("Ellipse 2").transform.rotation)


if (x >= 0) x + "°"
if (x >= 360) x%360 + "°"

The code is applied to an text layer, that will display the correct degrees of the element "Ellipse 2".

3

There are 3 answers

0
Shaamuji On

You can use the following function to convert the value to positive to make sure you logic works

if (x<0)
   flag true;
x = Maths.abs(x)
if flag
    x = 360-x
2
Ronald On

As an answer now. Since 360 == 0 mod 360, you want an x in [0, 360). The modulo operator might return a negative value if the argument is negative. You'll have to test that for your programming language.

I'll assume, that -1 % 5 == -1, hence the modulo operator doesn't change sign. Then

x = Math.floor(thisComp.layer("Ellipse 2").transform.rotation)

if (x >= 0) x%360 + "°"
if (x < 0) x = x%360 + 360 + "°"

should do the trick. If the modulo operator returns positive values, you can omit the "+ 360" in the last statement.

0
Xavier Gomez On

This should do it:

r = thisComp.layer("Ellipse 2").transform.rotation;
x = r%360;
if (x<0) x+= 360;
Math.floor(x) + "°";