I recently found this question on stack exchange with a great answer that showed how to condense game time into a sine wave, where 1 = midday and -1 = midnight

Converting time of day into a smooth day/night variable?

I've implemented this fine as far as I can tell but I'm a little confused as to how one would get a clean rotation from this to drive a directional light.

Currently I'm using arc-sine to revert the wave to radians and then converting it to degrees but I feel like this is going against the whole process of getting the sine in the first place. It doesn't actually work correctly anyway because we can't tell what side of the unit circle we're on, at least not with my current setup.

Is there a clean way for me to extract a rotation from this sine value so a sun would rotate smoothly? Thanks!


just my impression, but are you sure that you are not over-complicating things?

Why invert the sine function at all, when you have the argument you have passed to it? That's precisely the value you are looking for, in your quest for trigonometric functions inversion...

Just either cache the sin argument somewhere before applying the function to it, or just re-create the argument. Convert it to degrees, and you are set.

Keep in mind the DRY principle: the sin result is useful to get right things like "ambient light" values, or to feed, for instance, a proper value to NPC agents, but it's just a derivative value. The proper authoritative source for the time of the day in your game, is still the double/int/whatever variable where you keep track of your game cycles/wall time, and as long as you have it, you can get from it all kind of different time manifestations, whether is -1/+1 for day/night, or something as complex as a quaternion keeping track of true ephemeral for the sun position.

Hope this helps!

UPDATE Cursory explanation of the algorithm behind time/rotation/sin, as requested in the comments.

Suppose you start with the game time, that is, the number of seconds within your simulation.

The sine function you use is just a mathematical construct (one of many actually) that turns your linear, ever increasing time (0 at the start of the simulation, 3600 after 1 hour of game time, 36000 after 10 hours, ecc) into a periodic function.

The nice thing is that if you multiply your time for a proper constant before applying the sin function, that is 2PI/day_length_in_seconds, the result will oscillate with a period that's exactly with the same period of your day/night cycle, and will be, save for a time offset, 1 at noon and -1 ad midnight, and this is very useful in a number of situations, as stated in the first part of the answer, and is what you already have. But...

The argument of sin is an angle in radians (at least on most math libraries implementations...) and it happens to be, after a rad2deg conversion (180 x the argument / PI) and normalization (x % 360) exactly the angle you are looking for, that is 90deg at noon, -90 at midnight.

You don't need the sin function at all to compute the angle, because it's the other way around: you already have to have the angle to compute the sin function.

As for lighting, just a few tips: you can use the result of sin to compute the ambient light, with some rescaling/biasing, and then use the angle to put the sun into the sky and also to compute the diffuse and specular components (if you use the phong model), but first, do read a good tutorial/primer on trigonometry and get the hang of it: it's very basic stuff, and you definitely need that (and more...) for even casual game development ;-)

  • \$\begingroup\$ Thanks R1ck, this is great. As you may guess my math isn't the best and I often overlook things like that haha. I like the idea of using the sine value to drive shader properties and will definitely keep this in mind. I'm curious how you would get a rotation from a wall time, would you be able to explain this to me? I store seconds as a float and it wraps when hitting a constant seconds in minute value. Minutes are stored as an int currently. \$\endgroup\$ – Liam Feb 16 '16 at 11:05
  • \$\begingroup\$ Hi Liam, I'll update the answer. \$\endgroup\$ – R1ck77 Feb 16 '16 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.