Search code examples
mathangledegrees

How to count down after reaching max


I'm working with degrees and I need a separate value to stay within a specific range.

The degrees I'm working with are from 0 to 180 and I need the separate variable to interpret it as:

- 0.0f degrees = 0.0f

- 90.0f degrees = 1.0f

- 180.0f degrees = 0.0f

Currently, I have this that goes from 0.0f to 1.0f for degrees 0.0f to 90.0f:

float unit = 1.0f / 90.0f; // equivalent of 1 degree
value = unit * maths::toDegrees(angle);

What I'm trying to figure out is to have `value count down back to 0 the closer it gets 180 degrees.


Solution

  • Use absolute value of difference with 90:

    value = 1.0 - unit * math::Abs(90 - maths::toDegrees(angle));

    P.S. If you want smooth dependence, consider

    value = maths::Sin(angle);

    It is equal to zero in 0, reaches max value 1 at Pi/2=90, and diminishes to zero at Pi=180