• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Math- Radians to Degrees (C)

P

PlayLight

Guest
I've put together the below formula to convert radians to degrees while clamping to 360º in a C project.
similar to how 'direction' is handled in GM:S.

Code:
float rad, deg;
rad        = 7.34543 // radian input
rad = abs(rad);
if ( rad >= 6.28319 )
    {
    rad = fmod(rad, 6.28319);
    }
deg = rad *(180 /M_PI);
This works perfectly fine, however i'm sure i've seen a method suggested on the old forums which gives the exact same result without using an if statement.
Any Math Magicians who can think of a more efficient method to use?
 
Last edited by a moderator:
M

MishMash

Guest
well, if you are using mod, no point using an if statement :p! Aside from minor efficiency gains (which probably get optimised by the compiler anyway).

Also, you shouldn't be rounding degrees, whilst they are in a range of 0 to 360, you can still have decimals :p!
 

Mercerenies

Member
Code:
float rad, deg;
rad        = 7.34543 // radian input
rad = abs(rad);
if ( rad >= 6.28319 )
    {
    rad = rad mod 6.28319;
    }
deg = round(rad *(180 /pi));
Is this meant to be C or GML? You declare your variables like C but use "mod" like GML. It's not valid code in either language.

Anyway, when you have numerical expressions, it's always possible to eliminate if statements. It's a trick used by more experienced shader programmers.
Code:
// This
if (cond) {
    expr = tval;
} else {
  expr = fval;
}
// Is the same as this
expr = cond * tval + (1 - cond) * fval;
Obviously, the former is more readable. The only time you would want to use the latter is if your code is running on the GPU, in which case you have good reason to want to avoid if-statements.
 
P

PlayLight

Guest
@MishMash
i really only want to use modulo if the abs value is greater than 6.28319, otherwise it will return 0 on radian values between (and incl) -6.28319 and 6.28319.

but i will adjust the code to include decimals. thanks bud.


@Misu
hey mate, yeah i threw it in here as it's not related to GM at all. It's a C project i'm working on and just a basic enquiry on math efficiency.


@Mercerenies
yes sorry about that, i used gml mod by accident when i typed it out here.

fmod(rad, 6.28319)

No wont be running on the GPU but your example is very interesting. thanks for the input.
 
Last edited by a moderator:
M

Misu

Guest
@Misu
hey mate, yeah i threw it in here as it's not related to GM at all. It's a C project i'm working on and just a basic enquiry on math efficiency.
It is still valid as a thread for the programming subforum, even if it is not gm related. You can get a ton of help and quick responses at that subforum. ;)
 
Top