Merge branch 'seybur-master' #43
[VesselSimulator.git] / KerbalEngineer / Flight / Readouts / Surface / Slope.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 
//     Kerbal Engineer Redux
// 
//     Copyright (C) 2014 CYBUTEK
// 
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
 
#region Using Directives
 
using System;
 
using KerbalEngineer.Helpers;
 
using UnityEngine;
 
#endregion
 
namespace KerbalEngineer.Flight.Readouts.Surface
{
    public class Slope : ReadoutModule
    {
        #region Constructors
 
        public Slope()
        {
            this.Name = "Slope";
            this.Category = ReadoutCategory.GetCategory("Surface");
            this.HelpString = "Shows the slope of the terrain below your vessel.";
            this.IsDefault = true;
        }
 
        #endregion
 
        #region Methods: public
 
        public override void Draw()
        {
            this.DrawLine(this.GetSlopeAngleAndHeading());
        }
 
        #endregion
 
        #region Methods: private
 
        private string GetSlopeAngleAndHeading()
        {
            try
            {
                var result = "--° @ ---°";
                var mainBody = FlightGlobals.ActiveVessel.mainBody;
                var rad = (FlightGlobals.ActiveVessel.CoM - mainBody.position).normalized;
                RaycastHit hit;
                if (Physics.Raycast(FlightGlobals.ActiveVessel.CoM, -rad, out hit, Mathf.Infinity, 1 << 15)) // Just "Local Scenery" please
                {
                    var norm = hit.normal;
                    norm = norm.normalized;
                    var raddotnorm = Vector3d.Dot(rad, norm);
                    if (raddotnorm > 1.0)
                    {
                        raddotnorm = 1.0;
                    }
                    else if (raddotnorm < 0.0)
                    {
                        raddotnorm = 0.0;
                    }
                    var slope = Math.Acos(raddotnorm) * 180 / Math.PI;
                    result = Units.ToAngle(slope, 1);
                    if (slope < 0.05)
                    {
                        result += " @ ---°";
                    }
                    else
                    {
                        var side = Vector3d.Cross(rad, norm).normalized;
                        var east = Vector3d.Cross(rad, Vector3d.up).normalized;
                        var north = Vector3d.Cross(rad, east).normalized;
                        var sidedoteast = Vector3d.Dot(side, east);
                        var direction = Math.Acos(sidedoteast) * 180 / Math.PI;
                        var sidedotnorth = Vector3d.Dot(side, north);
                        if (sidedotnorth < 0)
                        {
                            direction = 360 - direction;
                        }
                        result += " @ " + Units.ToAngle(direction, 1);
                    }
                }
 
                return result;
            }
            catch (Exception ex)
            {
                Logger.Exception(ex, "Surface->Slope->GetSlopeAngleAndHeading");
                return "--° @ ---°";
            }
        }
 
        #endregion
    }
}