Skip to content

Commit 867233d

Browse files
author
Chris Jakeman
committed
Fix for offset not effective soon after midnight
Also code was difficult to read.
1 parent 71c4e89 commit 867233d

File tree

1 file changed

+24
-17
lines changed
  • Source/RunActivity/Viewer3D

1 file changed

+24
-17
lines changed

Source/RunActivity/Viewer3D/Sky.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,10 @@ public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
137137
// Current solar and lunar position are calculated by interpolation in the lookup arrays.
138138
// The arrays have intervals of 1200 secs or 20 mins.
139139
// Using the Lerp() function, so need to calculate the in-between differential
140-
float diff = GetCelestialDiff();
141140
// The rest of this increments/decrements the array indices and checks for overshoot/undershoot.
142-
while (Viewer.Simulator.ClockTime >= (oldClockTime + 1200)) // Plus key, or normal forward in time; <CSComment> better so in case of fast forward
141+
while (Viewer.Simulator.ClockTime >= (oldClockTime - DaylightOffsetS + 1200)) // Plus key, or normal forward in time; <CSComment> better so in case of fast forward
143142
{
144143
oldClockTime = oldClockTime + 1200;
145-
diff = GetCelestialDiff();
146144
step1++;
147145
step2++;
148146
if (step2 >= maxSteps) // Midnight.
@@ -154,10 +152,9 @@ public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
154152
step1 = 0;
155153
}
156154
}
157-
if (Viewer.Simulator.ClockTime <= (oldClockTime - 1200)) // Minus key
155+
if (Viewer.Simulator.ClockTime <= (oldClockTime - DaylightOffsetS)) // Minus key
158156
{
159-
oldClockTime = Viewer.Simulator.ClockTime;
160-
diff = 0;
157+
oldClockTime = oldClockTime - 1200;
161158
step1--;
162159
step2--;
163160
if (step1 < 0) // Midnight.
@@ -169,27 +166,37 @@ public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
169166
step2 = maxSteps - 1;
170167
}
171168
}
172-
solarDirection.X = MathHelper.Lerp(solarPosArray[step1].X, solarPosArray[step2].X, diff);
173-
solarDirection.Y = MathHelper.Lerp(solarPosArray[step1].Y, solarPosArray[step2].Y, diff);
174-
solarDirection.Z = MathHelper.Lerp(solarPosArray[step1].Z, solarPosArray[step2].Z, diff);
175-
lunarDirection.X = MathHelper.Lerp(lunarPosArray[step1].X, lunarPosArray[step2].X, diff);
176-
lunarDirection.Y = MathHelper.Lerp(lunarPosArray[step1].Y, lunarPosArray[step2].Y, diff);
177-
lunarDirection.Z = MathHelper.Lerp(lunarPosArray[step1].Z, lunarPosArray[step2].Z, diff);
169+
solarDirection.X = MathHelper.Lerp(solarPosArray[step1].X, solarPosArray[step2].X, CelestialDiff);
170+
solarDirection.Y = MathHelper.Lerp(solarPosArray[step1].Y, solarPosArray[step2].Y, CelestialDiff);
171+
solarDirection.Z = MathHelper.Lerp(solarPosArray[step1].Z, solarPosArray[step2].Z, CelestialDiff);
172+
lunarDirection.X = MathHelper.Lerp(lunarPosArray[step1].X, lunarPosArray[step2].X, CelestialDiff);
173+
lunarDirection.Y = MathHelper.Lerp(lunarPosArray[step1].Y, lunarPosArray[step2].Y, CelestialDiff);
174+
lunarDirection.Z = MathHelper.Lerp(lunarPosArray[step1].Z, lunarPosArray[step2].Z, CelestialDiff);
178175

179176
frame.AddPrimitive(Material, Primitive, RenderPrimitiveGroup.Sky, ref XNASkyWorldLocation);
180177
}
181178

182179
/// <summary>
183-
/// Returns the advance of time in seconds in units of 20 mins (1200 seconds).
180+
/// Returns the advance of time in units of 20 mins (1200 seconds).
184181
/// Allows for an offset in hours from a control in the DispatchViewer.
185182
/// This is a user convenience to reveal in daylight what might be hard to see at night.
186183
/// </summary>
187184
/// <returns></returns>
188-
private float GetCelestialDiff()
185+
private float CelestialDiff
189186
{
190-
var diffS = (Viewer.Simulator.ClockTime - oldClockTime);
191-
diffS += (double)(Program.DebugViewer?.DaylightOffsetHrs ?? 0) * 60 * 60;
192-
return (float)diffS / 1200;
187+
get
188+
{
189+
var diffS = Viewer.Simulator.ClockTime - (oldClockTime - DaylightOffsetS);
190+
return (float)diffS / 1200;
191+
}
192+
}
193+
194+
private float DaylightOffsetS
195+
{
196+
get
197+
{
198+
return (Program.DebugViewer == null) ? 0f : (float)Program.DebugViewer.DaylightOffsetHrs * 60 * 60;
199+
}
193200
}
194201

195202
public void LoadPrep()

0 commit comments

Comments
 (0)