Local Time Calculations
This commit is contained in:
parent
e379d1593a
commit
8d0d0701ba
2 changed files with 96 additions and 5 deletions
58
src/Main.cs
58
src/Main.cs
|
|
@ -38,6 +38,7 @@ public partial class Main : Control
|
|||
UpdateStats();
|
||||
Projector.GatherPoints(_planetHelper, int.Parse(GetNode<LineEdit>("%Resolution").Text));
|
||||
AxialTiltChanged(GetNode<LineEdit>("%AxialTilt").Text);
|
||||
UpdateTime();
|
||||
}
|
||||
|
||||
Vector3 _pointerPosition = Vector3.Zero;
|
||||
|
|
@ -112,6 +113,7 @@ public partial class Main : Control
|
|||
vertexPos *= _meshInstance.Transform.Basis.Inverse().Orthonormalized();
|
||||
_pointerPosition = (Vector3)vertexPos + (vertexPos * (_vertex.Height + 0.01f) * 0.03f); // vertexPos * 1.01f;
|
||||
GetNode<Node3D>("%Pointer").GlobalPosition = _pointerPosition;
|
||||
UpdateTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -128,16 +130,65 @@ public partial class Main : Control
|
|||
_planetHelper.Process();
|
||||
if (_sunRotating)
|
||||
{
|
||||
_sunRotation = (double)(GetNode<HSlider>("%SunAngle").Value / 365.0);
|
||||
GetNode<Node3D>("%Sun").Rotation -= GetNode<Node3D>("%Sun").Rotation;
|
||||
GetNode<Node3D>("%Sun").Rotation += new Vector3(0, (float)Mathf.DegToRad(GetNode<HSlider>("%SunAngle").Value), 0);
|
||||
GetNode<Node3D>("%Sun").Rotation += new Vector3(0, Mathf.DegToRad((float)_sunRotation * 360f), 0);
|
||||
UpdateTime();
|
||||
}
|
||||
if (_planetRotating)
|
||||
{
|
||||
_planetRotation = (double)(GetNode<HSlider>("%PlanetAngle").Value / 24.0);
|
||||
_meshInstance.Rotation = Vector3.Zero;
|
||||
_meshInstance.RotateY((float)Mathf.DegToRad(GetNode<HSlider>("%PlanetAngle").Value));
|
||||
_meshInstance.RotateY((float)Mathf.DegToRad(_planetRotation * 360.0));
|
||||
_meshInstance.RotateZ(Mathf.DegToRad(_axialTilt));
|
||||
UpdateTime();
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateTime()
|
||||
{
|
||||
double hours = GetLocalTime(GetNode<Node3D>("%Pointer").GlobalPosition, GetNode<Node3D>("%Sun").GlobalTransform.Basis.Z,
|
||||
_meshInstance);
|
||||
GetNode<Label>("%LocalTime").Text = DaysToTime(_sunRotation * 365.0, hours);
|
||||
|
||||
GetNode<Label>("%Date").Text = DaysToDate(_sunRotation * 365.0, _planetRotation*24.0);
|
||||
}
|
||||
|
||||
public static double GetLocalTime(Vector3 surfacePoint, Vector3 sunPos, Node3D planet)
|
||||
{
|
||||
Vector3 center = planet.GlobalTransform.Origin;
|
||||
Vector3 axis = -planet.GlobalTransform.Basis.Y.Normalized();
|
||||
|
||||
Vector3 surfaceDir = (surfacePoint - center).Normalized();
|
||||
Vector3 sunDir = (sunPos - center).Normalized();
|
||||
|
||||
surfaceDir = (surfaceDir - axis * surfaceDir.Dot(axis)).Normalized();
|
||||
sunDir = (sunDir - axis * sunDir.Dot(axis)).Normalized();
|
||||
|
||||
double angle = Mathf.Atan2(
|
||||
axis.Dot(surfaceDir.Cross(sunDir)),
|
||||
surfaceDir.Dot(sunDir)
|
||||
);
|
||||
|
||||
double hours = Mathf.PosMod(angle / Mathf.Tau * 24.0 + 12.0, 24.0);
|
||||
|
||||
return hours;
|
||||
}
|
||||
|
||||
public string DaysToDate(double days, double hours)
|
||||
{
|
||||
DateTime dt = new DateTime(1, 1, 1);
|
||||
dt = dt.AddDays(days);
|
||||
dt = dt.AddHours(hours);
|
||||
return dt.ToString("'y'yyyy/'m'MM/'d'dd - 'h'HH':''m'mm':''s'ss");
|
||||
}
|
||||
public string DaysToTime(double days, double hours)
|
||||
{
|
||||
DateTime dt = new DateTime(1, 1, 1);
|
||||
dt = dt.AddDays(days);
|
||||
dt = dt.AddHours(hours);
|
||||
return dt.ToString("'h'HH':''m'mm':''s'ss");
|
||||
}
|
||||
public void UpdateStats()
|
||||
{
|
||||
if (_vertex != null)
|
||||
|
|
@ -239,6 +290,7 @@ public partial class Main : Control
|
|||
}
|
||||
|
||||
private bool _sunRotating = false;
|
||||
private double _sunRotation = 0.0;
|
||||
private float _axialTilt = 0f;
|
||||
public void SunAngleStart()
|
||||
{
|
||||
|
|
@ -250,7 +302,7 @@ public partial class Main : Control
|
|||
_sunRotating = false;
|
||||
}
|
||||
private bool _planetRotating = false;
|
||||
private float _planetRotation = 0f;
|
||||
private double _planetRotation = 0.0;
|
||||
public void PlanetAngleStart()
|
||||
{
|
||||
_planetRotating = true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue