0

ok, so a bit of an odd question, I have implemented a real-time system using Date() and Date.getTime(), And I have a plugin installed on my project (mz3d), I know how to use it, however I have run into a problem with the sun,

I need to convert 24 hour time from getTime() into a rotational value for mz3d's sun. however, the sun's topmost position in the sky is at 0 degrees and beneath the floor is 180 degrees.

my code is:

//variables
var d = new Date();
var hr= d.getHours();
var mn= d.getMinutes();
//conversion
var sun;
//mz3d command
mz3d.command('sun pitch',sun,0);

how would I calculate it so 12 to is 0 degrees and 24 or 0 is 180 degrees?

  • It's simply proportional: 360° = 24 hrs, 1° = 15 minutes. So for every 15 minutes after noon (12:00) the sun goes 1° from vertical (21:00 = 90°, 24:00/00:00 = 180°, etc.). Of course in real life that's not exactly how it works, but likely close enough for your needs. – RobG Sep 22 '21 at 03:03

1 Answers1

0

Thanks to RobG i found the following code works

var d = new Date();
minSinceMidnight = d.getTime()- d.setHours(
    0, 0, 0, 0
);
minSinceMidnight = Math.round(
    minSinceMidnight / 60000
);
var math1 = minSinceMidnight/1440;
var math2 = math1*360
//console.log(d);
//console.log(minSinceMidnight);
//console.log(math2);
var sun = math2+180
//console.log(
//    "sun pos = ", sun
//);
mz3d.command('sun pitch',sun,0);