From 29053eeaec5ba174277f647d8fcbd814af203d7c Mon Sep 17 00:00:00 2001 From: adrien Date: Sat, 3 Jun 2023 14:06:13 +0200 Subject: [PATCH] random start hour --- main.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index c8cccf0..fe3d3e2 100644 --- a/main.js +++ b/main.js @@ -276,6 +276,7 @@ const diff = today - startOfYear; const oneDay = 1000 * 60 * 60 * 24; const dayOfYear = Math.floor(diff / oneDay); const declination = 0.40928 * Math.sin(2*Math.PI*(dayOfYear+284)/365) +const startHour = 24 * Math.random() function updateSun() { @@ -288,10 +289,10 @@ function updateSun() { } else { const time = clock.elapsedTime ; - const hour = ( 14 + time / 1440 ) % 24 + const hour = ( startHour + time / 1440 ) % 24 const hourAngle = Math.PI * (1-hour/12) elevation = Math.asin( Math.sin(declination)*Math.sin(latitude) + Math.cos(declination)*Math.cos(latitude)*Math.cos(hourAngle) ) - azimuth = 0.6 + Math.asin( Math.cos(declination)*Math.sin(hourAngle)/Math.cos(elevation) ) + azimuth = -Math.PI/2 + Math.asin( Math.cos(declination)*Math.sin(hourAngle)/Math.cos(elevation) ) } @@ -324,7 +325,7 @@ function updateSun() { } updateSun(); -setInterval( updateSun, 100 ); +const updateSunIntervalId = setInterval( updateSun, 100 ); // showGUI @@ -482,6 +483,7 @@ function playerCollisions() { addEventListener("animationend", (event) => { stopAnimation = true; + clearInterval( updateSunIntervalId ); document.exitPointerLock(); });