forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoCoordinates.cs
More file actions
73 lines (59 loc) · 2.28 KB
/
GeoCoordinates.cs
File metadata and controls
73 lines (59 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace kOS
{
public class GeoCoordinates : SpecialValue
{
public float Lat;
public float Lng;
public Vessel Vessel;
public CelestialBody Body;
public GeoCoordinates(Vessel vessel, float lat, float lng)
{
this.Lat = lat;
this.Lng = lng;
this.Vessel = vessel;
Body = vessel.mainBody;
}
public GeoCoordinates(Vessel vessel, double lat, double lng)
{
this.Lat = (float)lat;
this.Lng = (float)lng;
this.Vessel = vessel;
Body = vessel.mainBody;
}
public float GetBearing(Vessel vessel)
{
return VesselUtils.AngleDelta(VesselUtils.GetHeading(vessel), GetHeadingFromVessel(vessel));
}
public float GetHeadingFromVessel(Vessel vessel)
{
var up = vessel.upAxis;
var north = VesselUtils.GetNorthVector(vessel);
var targetWorldCoords = vessel.mainBody.GetWorldSurfacePosition(Lat, Lng, vessel.altitude);
var vector = Vector3d.Exclude(vessel.upAxis, targetWorldCoords - vessel.GetWorldPos3D()).normalized;
var headingQ = Quaternion.Inverse(Quaternion.Euler(90, 0, 0) * Quaternion.Inverse(Quaternion.LookRotation(vector, up)) * Quaternion.LookRotation(north, up));
return headingQ.eulerAngles.y;
}
public float DistanceFrom(Vessel Vessel)
{
return (float)Vector3d.Distance(Vessel.GetWorldPos3D(), Body.GetWorldSurfacePosition(Lat, Lng, Vessel.altitude));
}
public override object GetSuffix(string suffixName)
{
if (suffixName == "LAT") return (float)Lat;
if (suffixName == "LNG") return (float)Lng;
if (suffixName == "DISTANCE") return DistanceFrom(Vessel);
if (suffixName == "HEADING") return GetHeadingFromVessel(Vessel);
if (suffixName == "BEARING") return GetBearing(Vessel);
return base.GetSuffix(suffixName);
}
public override string ToString()
{
return "LATLNG(" + Lat + ", " + Lng + ")";
}
}
}