Gamedevlog: Simulating day and night
Tags: vrgame, devlog, android, opengles2
Published: 2020-08-30 20:10:05

# All in a days work
To add some movement to the scene and make it more dynamic, I have decided to add a day/night cycle which will include. 
* A 3D model for the sun which will move across the scene. 
* The colour of the sun will change from white at midday, to a reddish orange at dawn and dusk and black at night. 

# The light source
To introduce a light source I created a new <code>SunLight</code> class which will:
* Implement a simple form of the model rendering code to render the sun model. 
* Calculate the lights position and colour based on the time of day. 

## Calculating the suns position
To work out where the sun should be at any given game time, I will take the result of the mod of the current system time in milliseconds the total time per day and then divide it by the total time per day to know how far into the day we are, then multiply that by 360° to get the current angle. 
At the start of the day (0 time) the sun shall be directly below the world so if we set a position directly down on the y axis and then rotate it by the current angle, this will be the position of the sun. 

## Calculating the suns colour
Again we can make use of the angle generated for the suns position to cycle the colour between black at the start of the day, reddish orange at dawn and dusk and white at mid day. 

![Suns Colour Sketch](https://dev-to-uploads.s3.amazonaws.com/i/krpvqa2nitmxch7osajv.png)

I was unsure how best to convert the angle into a colour but I started by setting the colour based on the angle using a <code>when</code> statement with ranges for each of the colours.

![Initial way of determining colour](https://dev-to-uploads.s3.amazonaws.com/i/njva1mxlt9s9so9ckfgb.PNG)

This was a good start but the transitions needed to be smoothed out and the skybox still looks the same throughout the day. 

![Initial day and night gif](https://dev-to-uploads.s3.amazonaws.com/i/hp1053gnm5fb0rybzrh2.gif)

To fix these issues I created a simple interpolation function to gradually merge from one colour to the next, this function only works as expected because at most I am changing two of the 3 colour values each time.

![Interpolation function](https://dev-to-uploads.s3.amazonaws.com/i/a1j4s7t5yyjradfx3eaa.PNG)

This was used in the when block like this:

![example of using the interpolation function](https://dev-to-uploads.s3.amazonaws.com/i/xn7o3n95bqrsmiiwzt9v.PNG)

One final issue with this was that the sun always rotated around the scenes origin and not the player position, therefore the player could walk into the path of the sun, to fix this I passed the players position to the <code>UpdateSunLight</code> function and translated the sun position by the players position.

![Adding the players position to the suns position](https://dev-to-uploads.s3.amazonaws.com/i/alxmykap38iwfvhm65s4.PNG)

This gave the following Day/Night cycle which I am quite happy with at the moment.
 
![Final Day / Night Cycle](https://dev-to-uploads.s3.amazonaws.com/i/dttcwgymx5hdqws25e04.gif)

# Conclusions
The game now has some dynamic lighting in the form of a day / night cycle which is a great improvement on the previous static lighting. 

The source code at this point in the project can be found [here](https://github.com/longevity-software/AndroidVrGameDemo/releases/tag/B1.9).