Sun Position Logic
Modeling the sun's position
Of course, we need to be able to calculate the sun’s position before we can determine its effects on the shadows we see. To give you some control over the time of day in the demo you can increase and decrease the time by pressing the A and Z keys.
Sun Azimuth
For simplicity I've decided to assume that the sun's position in azimuth varies linearly with time. In reality it would be a function of your latitude and the time of year, but that would be far too much work and wouldn't be noticed by a game player. So if the sun covers an arc of 180 degrees over 12 hours then it will move in our simulation at a rate of 15 degrees per hour and rise at 0 degrees game angle (due east). So, at any given time, the sun's azimuth is:
Sun Elevation
We need a way of determining when the sun is at maximum height (for the shortest shadows) and when the sun is lowest to the horizon (for the longest shadows). The maximum elevation of the sun will determine how long or short the midday shadows are, but reality is less important than visual appearance and ease of coding. For the method I’ve chosen here we first calculate the sun’s elevation factor (0 to 1, 0 being sun set and 1 being midday or maximum elevation). From that value we can then do the work we need. One easy way to model how the sun travels across the sky is to use a sine equation: