I'm trying to make a smooth day/night cycle for a game project that I've been working on. I've already set up a clock that adds deltaTime to seconds, wraps seconds to gameMinutes, etc. The issue that I'm having is that I want a signed normalized float to represent day (+1) and night (-1), and I'm not exactly sure what the most effective way of doing that is.
Here's my time class in pseudocode:
class GameTime {
float snDayNight; //signed normalized
float seconds;
int gameMinutes;
int gameHours;
int gameDays;
int gameMonths;
//Omitted consts used for wrapping (SEC_PER_MIN, HR_PER_DAY, etc.)..
//Per-frame update..
public void Update(){
seconds += Timer.GetDeltaTime();
WrapTimeValues (); //Wraps sec to min, min to hr, etc.
UpdateDayNightCycle ();
}
private void UpdateDayNightCycle(){
//?????
}
//...//
}
So, my GameTime has been tested and seems to be working fine; when the value in seconds is >= the value of SEC_PER_MIN it adds another minute and stores the leftover fraction of a second. It then goes on to check minutes, hours, days, etc.
I've been a little bit stuck at UpdateDayNightCycle() though. I want to be able to convert my time into a smooth decimal value where -1 represents absolute night and 1 represents absolute day. I've normalized values between 0~1 and -1~1 before, but I've never tried to use a normalized value to represent a repeating/periodic value like night~day before..
I tried drawing a simple graph where time of day is on the x-axis and daynight value is on the y-axis. What I get is essentially a graph of a triangle wave. Sadly my math skills are a little bit underdeveloped and even after looking into formulas for triangle waves, I've been having trouble implementing it in my code..
So, am I even on the right track? How can I smoothly convert my game world's time into a single decimal value between -1 and 1?