Day and Night Cycle with moving Sun

By LadylexUK (Feb 2015)
Copied from ProjectSpark.com

This tutorial will create a day to night cycle. It can be run at whatever speed you choose.

First let’s understand what happens in the real world during the day. As we revolve round the sun, the sun appears to travel across the sky changing the day from morning to noon to evening to night. Luckily for us there are 4 sky boxes that will also show us that.
So let’s start by changing the angle of the sun.

Code:

WHEN [once] DO [numvar:sun][=][-90] // we will start the sun at sunrise
WHEN [countdown timer][1][loop] DO [numvar:sun][=][numvar:sun][+][1]
…WHEN DO [sun angle][=][numvar:sun] // this will add 1 degree to the sun’s angle every second

WHEN [numvar:sun][=][91] DO [sun angle][=][90] // the sun angle can only go from -90 to 90, but our sun would have travelled on round the planet to create nighttime. So we set the sun at its setting point for the night
WHEN [numvar:sun][=][180] DO [numvar:sun][=][-90] // and this sets it back to morning again

For testing put this line in

WHEN DO [display][numvar:sun][screen top center] // this will show the angle of the sun

Now this shows the sun travelling across the sky but it doesnt affect the light levels.

Add

Code:

WHEN [once] DO [brightness][=][-1]

Add after line 3

…WHEN [numvar:sun][<][-50][and][brightness][<=][0] DO [brightness][=][brightness][+][0.01]
…WHEN [numvar:sun][>=][50][and][brightness][=>][-2] DO [brightness][=][brightness][-][0.01]

Change/Add

WHEN [numvar:sun][=][91] DO [sun angle][=][90]
…WHEN DO [brightness][=][-2]
WHEN [numvar:sun][=][180] DO [numvar:sun][=][-90]
…WHEN DO [brightness][=][-1]

This code will make it brighten up early in the day and darken down late in the day. But it still uses the same sky boxes. So now add

Code:

WHEN [numvar:sun][=][-90] or [numvar:sun][=][180] DO [sky][=][Morning clear]
WHEN [numvar:sun][=][-50] DO [sky][=][Day clear]
WHEN [numvar:sun][=][50][DO [sky][=][Evening clear]
WHEN [numvar:sun][=][91] DO [sky][=][Night clear]

//you can alter when the sky boxes change to taste.

And that’s pretty much it.
The speed of the day can be changed by altering the countdown timer loop. You will also have to change the incremental values of the sun number variable and brightness to match the speed of the sun movement.

Comments are closed.

Powered by WordPress.com.

Up ↑

%d bloggers like this: