DEV Community

Graham Long
Graham Long

Posted on

1

Gamedevlog: Simulating day and night

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 SunLight 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

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 when statement with ranges for each of the colours.

Initial way of determining colour

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

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

This was used in the when block like this:

example of using the interpolation function

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 UpdateSunLight function and translated the sun position by the players position.

Adding the players position to the suns position

This gave the following Day/Night cycle which I am quite happy with at the moment.

Final Day / Night Cycle

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.

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay