# How can I simulate the sun's position for 3D lighting?

I'm using a directional light as a light source. It dosn't have a positon, only a direction, represented by a vec3.

I'm trying to figure out how to make the light direction to simulate sun movement and I'm clueless where to start. There's a related question here on gamedev but its 2d and only on suns position, which I am not interested in. Any suggestions on how to start?

You will need to explain a bit better, a vector can be described as a diference between 2 points A-B => C C will be your direction

So if you want to get the sun direction you need to assume that you SUN is at some position, lets call it

SunPos = Vector(0,0,0);


To get direction from the sun to whatever surface you are , just do

Direction = CurrentPoint - SunPosition


Unless I am not understanding what you are saying.

To simulate the Sun movement just make the Sun position Change based on Cos and Sin function, this will simulate the sun position across the earth and calculate the direction based on that position.

SunPos = Vector(Cos(time),Sin(Time);


This for 2D, if 3D you need to decide which axes is the sun moving, but the principle is the same.

Hope this helps.

• Vector(Sin(time),Cos(Time);  - Swapping Cos and Sin created clockwise movement for me :D Thanks.. although I have no idea how to calculate real time into the time variable there +1 – Piotr Kula Jan 10 '17 at 21:30

Another definition of a vector is as a normal to a plane or primative face. The sun's rays can be simulated by the same sin/cos motion as described in Morphex' answer, but the sun's apparent motion can be applied as a scaled dot product with the normal to the ground plane. The scaling is a time-dependent matrix of cos and sin so that the direction vector is long (ie. the dot product to the ground normal is small) near sunrise and sunset and approaches unity (parallels the ground normal) at midday.

Of course this is a crude approximation, but can be improved by multiplying an azmuthal angle based on your scene's latitude. This helps simulate the roughly 18deg. off-axis tilt of the earth and adjusts the sun normal for the scene's distance from the equator. (The effect would be more in New York than in Mexico City, for example.) Yet another angle can be applied to account for season of the year at that latitude.

Google 'modeling apparent sun direction' for some more details.

Lastly, remember to reverse the sign of the vector you get from this process when you apply it as light because you first derive it as outward from the ground, but use it as an inbound light vector!