Skip to content

Commit 719c737

Browse files
committed
fix: Correctly calculate the sun's position in the sky
1 parent 5c51817 commit 719c737

File tree

1 file changed

+7
-23
lines changed

1 file changed

+7
-23
lines changed

Source/RunActivity/Viewer3D/Common/SunMoonPos.cs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,16 @@ public static Vector3 SolarAngle(double latitude, double longitude, EnvironmentF
8989
solarHourAngle = MathHelper.ToRadians((float)(trueSolar / 4) - 180);
9090
}
9191

92-
// Solar zenith cosine. This is the Y COORDINATE of the solar Vector.
93-
var solarZenithCosine = (Math.Sin(latitude) * Math.Sin(solarDeclination))
94-
+ (Math.Cos(latitude) * Math.Cos(solarDeclination) * Math.Cos(solarHourAngle));
92+
var solarZenithCosine = (Math.Sin(latitude) * Math.Sin(solarDeclination)) + (Math.Cos(latitude) * Math.Cos(solarDeclination) * Math.Cos(solarHourAngle));
93+
var solarZenithSine = Math.Sin(Math.Acos(solarZenithCosine));
94+
var solarAzimuthCosine = (Math.Sin(solarDeclination) - (solarZenithCosine * Math.Sin(latitude))) / (Math.Sin(Math.Acos(solarZenithCosine)) * Math.Cos(latitude));
95+
var solarAzimuthSine = -Math.Sin(solarHourAngle) * Math.Cos(solarDeclination) / Math.Sin(Math.Acos(solarZenithCosine));
9596

96-
// Solar elevation angle, radians. Currently not used.
97-
// double solarElevationAngle = MathHelper.PiOver2 - Math.Acos(solarZenithCosine);
98-
99-
// Solar azimuth cosine. This is the Z COORDINATE of the solar Vector.
100-
var solarAzimuthCosine = -((Math.Sin(latitude) * solarZenithCosine) - Math.Sin(solarDeclination))
101-
/ (Math.Cos(latitude) * Math.Sin(Math.Acos(solarZenithCosine)));
102-
103-
// Running at 64 bit solarAzimuthCosine can be slightly below -1, generating NaN results
104-
solarAzimuthCosine = MathHelper.Clamp((float)solarAzimuthCosine, -1, 1);
105-
106-
// Solar azimuth angle, radians. Currently not used.
107-
// double solarAzimuthAngle = Math.Acos(solarAzimuthCosine);
108-
// if (clockTime > 0.5)
109-
// solarAzimuthAngle = MathHelper.TwoPi - solarAzimuthAngle;
110-
111-
// Solar azimuth sine. This is the X COORDINATE of the solar Vector.
112-
var solarAzimuthSine = Math.Sin(Math.Acos(solarAzimuthCosine)) * (clockTime > 0.5 ? 1 : -1);
113-
114-
sunDirection.X = -(float)solarAzimuthSine;
97+
sunDirection.X = (float)solarZenithSine * (float)solarAzimuthSine;
11598
sunDirection.Y = (float)solarZenithCosine;
116-
sunDirection.Z = -(float)solarAzimuthCosine;
99+
sunDirection.Z = -(float)solarZenithSine * (float)solarAzimuthCosine;
117100
sunDirection.Normalize();
101+
118102
return sunDirection;
119103
}
120104

0 commit comments

Comments
 (0)