• 12
• 12
• 9
• 10
• 13

# time to angle

This topic is 829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im trying to position a sun by passing hour and minute to a function that should return the angle for the sun zenith (altitude)

so:

at 5:24 i want the angle to be 0.0,

at 12:00 i want the angle to be 75.0 <-cant exceed that value (can be greater)

at 22:00 i want the angle to be 0.0

between 0:00 and 5:24 the angle must be negative.

I am sorta out of ideas now how should i proceed with that.

##### Share on other sites

Convert the time to H*60+M, and split the problem into <= 12:00, and >= 12:00

In the morning, you have two points (5*60+24, 0), and (12*60, 75) = (324, 0) and (720, 75)

In the afternoon, you have two points (12*60, 75), and (22*60, 0) = (720, 75) and (1320, 0)

You haven't said how the angle is supposed to change between those two points.

(I only do the morning case, the afternoon case is similar)

For linear interpolation (which is the simplest, but not realistic):

A line with unknown parameters a and b:

angle = a * time + b

Fill in (324, 0):  0 = 324 * a + b   [eq1]

Fill in (720, 75): 75 = 720 * a + b  [eq2]

2 equations, with 2 variables, solvable!

[eq1] gives you   b = -324 * a   [eq3]

Substitute that in [eq2]

75 = 720 * a - 324 * a

75 = 396 * a

a = 75 / 396

Put that in [eq3]:

b = -324 * 75 / 396

thus

a = 0.1893939393939394

b = -61.36363636363637

angle = 0.1893939393939394 * time - 61.36363636363637

It's likely you don't want a linear interpolation though, since it means the angle increases at the same rate the entire morning

More realistic, you want a big increase  in the morning, and a slow increase near mid-day.

This calls for a quadratic formula

angle = a * time * time + b * time + c [eq4]

[eq4] has 3 parameters a, b, and c, so you either need 3 points, or you need 2 points and 1 rate of angle. We have the latter (rate of angle at time 720 = 0).

The d(angle)/d(time) of [eq4] is

angle' = 2 * a * time + b [eq5]

0 = a * 324 * 324 + b * 324 + c    (324, 0) in [eq4] gives [eq6]

75 = a * 720 * 720 + b * 720 + c  (720, 75) in [eq4] gives [eq7]

0 = 2 * a * 720 + b  (720, 0) in [eq5] gives [eq8]

[eq8] says b = -2 * a * 720   [eq9]

[eq7]-[eq6]:  75 - 0 = a * 720 * 720 - a * 324 * 324 + b * 720 - b * 324 + c - c

75 = a * (720*720 - 324*324) + b * (720 - 324)   [eq10]

Substitute [eq9] into [eq10]:

75 = a * (720*720 - 324*324) - 2 * a * 720 * (720 - 324)

75 = a * (720*720 - 324*324 - 2 * 720 * (720 - 324))

a = 75 / (720*720 - 324*324 - 2 * 720 * (720 - 324))

a = -75 / 156816 = -0.0004782675237220692

apply in [eq9] gives you b = 0.6887052341597796

apply in [eq6] gives you c = -(a * 324 * 324 + b * 324) = -172.93388429752065

So it's  angle = -0.0004782675237220692 * time * time + 0.6887052341597796 * time -172.93388429752065

[attachment=29927:sun_rise.png]

Edited by Alberth

##### Share on other sites

btw i came with that just few mins ago and wanted to post it;p

inline float TimeToAngle(int hr, int min)
{
float time = hr + (min/60.0);

float ttime[9]  = {-1000.0, 0.0,   5.4, 5.65, 12.0, 22.0, 22.25, 24.0, 28.0};
float tangle[9] = {-90.0,  -10.0, -1.0, 8.0,  80.0, 8.0,  -1.0, -10.0, -90.0};
float angle = -10.0;

for (i=0; i<9; i++)
if( (ttime[i] <= time) && (ttime[i+1] > time) )
angle = tangle[i] - (ttime[i] - time) * (tangle[i] - tangle[i+1]) / (ttime[i] - ttime[i+1]);

return angle;
}

it simply lerps between given times (i convert minutes to fraction of the number)

Edited by WiredCat

##### Share on other sites

Splitting the graph into several linear pieces works too, you may want to add some more points around noon to simulate slower increase/decrease of the angle around that time.

My time wasn't exactly wasted. I had some exercises in solving sets of linear equations, which I hadn't done in ages

It also gave me an idea how to do these kind of conversions in a more generic way (but that needs more work to straighten out all the things first).

In the end, all that matters is that you find a solution that works for you. In this case, you even found it yourself, which is the best message you can have with this kind of questions. I have no problem playing the teddy bear role in such a case

Edited by Alberth

##### Share on other sites

Edited by WiredCat

Looking good :)

##### Share on other sites
Can't you just use sin or cos?

##### Share on other sites

its not that easy as you think it is, the skydome has dimension of earth radius + 24 kilometers, i am acutally positioning camera at north pole, and i need to position the sun somehow so i had to lerp between some angles (in this case up donw angle)