1+ //Copyright 2013 MichaelTaylor3D
2+ //www.michaeltaylor3d.com
3+ using UnityEngine ;
4+ using Communicator ;
5+ namespace AirControl
6+ {
7+
8+ //Copyright 2013 MichaelTaylor3D
9+ //www.michaeltaylor3d.com
10+ //https://github.com/MichaelTaylor3D/UnityGPSConverter/
11+
12+ public class GPSEncoder : MonoBehaviour
13+ {
14+
15+ #region Instance Variables
16+ public Rigidbody rb ;
17+
18+ private Vector2 _localOrigin = Vector2 . zero ;
19+ private float _LatOrigin { get { return _localOrigin . x ; } }
20+ private float _LonOrigin { get { return _localOrigin . y ; } }
21+ private float MaxLongitude = 180f ;
22+ private float MaxLatitude = 90f ;
23+
24+ private float metersPerLat ;
25+ private float metersPerLon ;
26+ #endregion
27+
28+ void Update ( ) {
29+ HandleLocation ( ) ;
30+ }
31+
32+
33+ #region Instance Functions
34+ public void FindMetersPerLat ( float lat ) // Compute lengths of degrees
35+ {
36+ float m1 = 1000f ; // reducing the gloabe equatorail diameter, original : 111132.92f; // latitude calculation term 1
37+ float m2 = - 559.82f ; // latitude calculation term 2
38+ float m3 = 1.175f ; // latitude calculation term 3
39+ float m4 = - 0.0023f ; // latitude calculation term 4
40+ float p1 = 1000f ; // reducing the globe polar diameter, original : 111412.84f; // longitude calculation term 1
41+ float p2 = - 93.5f ; // longitude calculation term 2
42+ float p3 = 0.118f ; // longitude calculation term 3
43+
44+ lat = lat * Mathf . Deg2Rad ;
45+
46+ // Calculate the length of a degree of latitude and longitude in meters
47+ metersPerLat = m1 + ( m2 * Mathf . Cos ( 2 * ( float ) lat ) ) + ( m3 * Mathf . Cos ( 4 * ( float ) lat ) ) + ( m4 * Mathf . Cos ( 6 * ( float ) lat ) ) ;
48+ metersPerLon = ( p1 * Mathf . Cos ( ( float ) lat ) ) + ( p2 * Mathf . Cos ( 3 * ( float ) lat ) ) + ( p3 * Mathf . Cos ( 5 * ( float ) lat ) ) ;
49+ }
50+
51+ public Vector3 ConvertGPStoUCS ( Vector2 gps )
52+ {
53+ FindMetersPerLat ( _LatOrigin ) ;
54+ float zPosition = metersPerLat * ( gps . x - _LatOrigin ) ; //Calc current lat
55+ float xPosition = metersPerLon * ( gps . y - _LonOrigin ) ; //Calc current lat
56+ return new Vector3 ( ( float ) xPosition , 0 , ( float ) zPosition ) ;
57+ }
58+
59+ public Vector2 ConvertUCStoGPS ( Vector3 position )
60+ {
61+ FindMetersPerLat ( _LatOrigin ) ;
62+ Vector2 geoLocation = new Vector2 ( 0 , 0 ) ;
63+ geoLocation . x = ( _LatOrigin + ( position . z ) / metersPerLat ) ; //Calc current lat
64+ geoLocation . y = ( _LonOrigin + ( position . x ) / metersPerLon ) ; //Calc current lon
65+ return geoLocation ;
66+ }
67+
68+
69+ /// <summary>
70+ /// Provides the location of the Aircraft in 3D coordinate system
71+ /// </summary>
72+ void HandleLocation ( )
73+ {
74+ //future functionality to provide the xyz location of the plane
75+ Vector3 currentLocation = rb . position ;
76+ Vector2 latlong = ConvertUCStoGPS ( currentLocation ) ;
77+ // Debug.Log(latlong);
78+ float normalizedLatitude = latlong . x / MaxLatitude ;
79+ float normalizedLongitude = latlong . y / MaxLongitude ;
80+ // Add location to static function
81+ StaticOutputSchema . Latitude = normalizedLatitude ;
82+ StaticOutputSchema . Longitude = normalizedLongitude ;
83+ }
84+ #endregion
85+ }
86+ }
0 commit comments