OrbitViewer math: less ridiculous way to simplify ry?

I’m trying to store an OrbitViewer’s ry value between runs of my program.

When a program’s running, however, OrbitViewer seems to just cumulatively record the amount of rotation, even when it’s more than 180 or less than -180; for example, rotating the viewer fully clockwise three times would make the ry value 1,080, and three times counter-clockwise would be -1,080.

I’m using this code to convert that number to the normal range:

    local rySignRememberer = 1
    local adjustedRy = viewer.ry
    if viewer.ry < 0 then
        rySignRememberer = -1 -- because lua doesn’t preserve the sign of the first number in a modulus operation
    if math.abs(viewer.ry) > 180 then
        adjustedRy = ((viewer.ry % 180) - 180) * rySignRememberer

…it works but it seems like there has to be a simpler way.

Is there?

@UberGoober Let me know if I have this right or wrong about what you’re trying to do. If 0 degrees is at the top of a circle and you move counterclockwise, you want the degrees to be from 0 to -180. If you go clockwise, you want the degrees to be from 0 to +180. If you go 190 degrees counterclockwise, you want the final value to be +170 degrees. If you go 190 degrees clockwise, you want the final value to be -170 degrees. If that’s the case, I don’t think your code is correct. Going 190 degrees clockwise gives -170, but going 190 degrees counterclockwise gives 10 degrees, not +170. I see 180 in your code that’s why I’m referring to 0 to 180 clockwise and 0 to -180 counterclockwise. And that it doesn’t matter how many times you go around, you want the answer to be 0 to -180 or 0 to +180 depending on which half you end up at.

@UberGoober I have code if the left half of the circle is negative (0 to -180) and the right half is positive (0 to +180).

Do you want to convert any angle to the equivalent 0-360 degrees (starting on the positive x axis and counting up anticlockwise)?

-- Angle Test
function setup()

-- This function gets called once every frame
function draw()
    -- This sets a dark background color
    background(40, 40, 50)
    ang = ang + rotatespeed
    -- Do your drawing here
    ang2=ang --ang2 to hold constrained angle between 0 & 360
    if ang2>0 then ang2=ang2%360 elseif ang2<0 then ang2=ang2%360 end

@dave1707 I’m just working with the OrbitViewer’s ry value, and all I know is what I got from experimentation.

-180 and 180 seem to both be half a rotation.

90 seems to be a quarter rotation clockwise, -90 seems to be a quarter rotation counter-clockwise.

When I use parameter.watch to track the viewer’s ry, multiple turns in either direction just keep adding to the value, so after many turns you could have an ry of 18,992, for example.

When I’m storing the ry it seems ridiculous to me to store the massively redundant value, so I want to bring it back to the range of 180 to -180.

All I can say is that this calculation works in my tests.

@dave1707 no, no, you’re right, it only works clockwise. Rats. If you’ve cracked it, I’d love to see it.

I assume it also works if the right half is negative and the left half is positive?

@UberGoober Heres the code. Just pass it an angle (+/-) and it returns a value between 0 to 180 (+/-). Try it out and see if it does what you want.

function reduce(ang)
    local a=ang%180
    if (ang//180)%2~=0 then

Brilliant. Works a charm! That’s some blue-ribbon gray matter you got there. Thank you very much.

I just recently happened to have put an icon on my copy of an old game you made, in case you might enjoy a trivial little modification like that I’m attaching the zip file for it.

@UberGoober I usually make a lot of random/useless stuff while I’m watching TV. If I think it’s interesting enough, I usually post it. That Color Puzzle was one of them. I also like taking code and seeing if I can reduce it as much as possible or rewrite it. So your question up at the top was something that I enjoyed working on. Putting icons on code is easy enough, but I never do it. It would probably make it easier for me to find a specific program when looking thru 600+ projects, but I just leave the default icons.