# GMS 2Calculating a shadow angle increase throughout day

#### OblivionSkull21

##### Member
Hello, I am trying to make the angle of a shadow move from right to left during the course of a whole day. Starting at 6AM, I want the shadow to begin moving from right to left gradually until it is completely left at 9PM (15 hours).

I use an alarm to calculate when an hour changes:
Code:
``move = room_width/(room_speed*7);``
I am merely asking for some help with determining the calculation, as every combination I have tried has made the angle either go too fast or too slow. I basically want the shadow to rotate a complete 180 within that time frame, not too fast or slow but right on time.

Here's my angle variable, though the calculation is not correct right now:
Code:
``dir -= 180/15/move;``

#### flyingsaucerinvasion

##### Member
It depends on what you consider to be a correct angle.

I was imagining a sun rising on the left, and setting on the right, and the shadow cast toward the bottom of the screen. I was also imagining the ground angled in case the game is drawn at an oblique angle.

So taking into account all of that, I came up with this as the end-point of the shadow relative to the thing that is casting it.

var _xoff = height / (dtan(sun_angle_y)*dcos(sun_angle_x));
var _yoff = dtan(sun_angle_x)*dcos(ground_angle) * height;

First of all, you should notice that since the north-south direction is aligned with the y axis, _yoff is constant... what I mean is it doesn't change with time of day, only with sun inclination, ground angle, and height of the thing casting the shadow. I found this counter-intuitive at first, but then when I thought about it, it made sense.

When the sun first rises, sun_angle_y should have a value of zero. It should have a value of 180 when the sun sets. sun_angle_x is the inclination of the sun, 0 means it passes directly overhead, 90 would mean it circles the horizon. ground_angle would have a value of 0 in a top down game, and some value between 0 and 90 in a game with an oblique projection. height is just how tall the thing casting the shadow is.

If you just need the angle of the shadow, it should just be the angle formed by the vector _xoff,_yoff. you can use point_direction(0,0,_xoff,_yoff), or darctan2(-_yoff,_xoff). But using only the angle of the shadow doesn't take into account the fact that the shadow stretches toward sunrise and sunset.

Last edited:

#### OblivionSkull21

##### Member
It depends on what you consider to be a correct angle.

I was imagining a sun rising on the left, and setting on the right, and the shadow cast toward the bottom of the screen. I was also imagining the ground angled in case the game is drawn at an oblique angle.

So taking into account all of that, I came up with this as the end-point of the shadow relative to the thing that is casting it.

var _xoff = height / (dtan(sun_angle_y)*dcos(sun_angle_x));
var _yoff = dtan(sun_angle_x)*dcos(ground_angle) * height;

First of all, you should notice that since the north-south direction is aligned with the y axis, _yoff is constant... what I mean is it doesn't change with time of day, only with sun inclination, ground angle, and height of the thing casting the shadow. I found this counter-intuitive at first, but then when I thought about it, it made sense.

When the sun first rises, sun_angle_y should have a value of zero. It should have a value of 180 when the sun sets. sun_angle_x is the inclination of the sun, 0 means it passes directly overhead, 90 would mean it circles the horizon. ground_angle would have a value of 0 in a top down game, and some value between 0 and 90 in a game with an oblique projection. height is just how tall the thing casting the shadow is.

If you just need the angle of the shadow, it should just be the angle formed by the vector _xoff,_yoff. you can use point_direction(0,0,_xoff,_yoff), or darctan2(-_yoff,_xoff). But using only the angle of the shadow doesn't take into account the fact that the shadow stretches toward sunrise and sunset.
So how would you implement that into a draw event, as in drawing the actual shadow?