-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGyroController.cs
More file actions
112 lines (95 loc) · 4.24 KB
/
GyroController.cs
File metadata and controls
112 lines (95 loc) · 4.24 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
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
112
using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;
using SpaceEngineers.Game.ModAPI.Ingame;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System;
using VRage.Collections;
using VRage.Game.Components;
using VRage.Game.GUI.TextPanel;
using VRage.Game.ModAPI.Ingame.Utilities;
using VRage.Game.ModAPI.Ingame;
using VRage.Game.ObjectBuilders.Definitions;
using VRage.Game;
using VRage;
using VRageMath;
namespace IngameScript
{
partial class Program
{
//The GyroController module is based on Flight Assist's GyroController and HoverModule, sharing code in places.
public class GyroController
{
const float dampeningFactor = 25.0f;
private IMyShipController controller;
private List<IMyGyro> gyroscopes;
public GyroController(IMyShipController controller, List<IMyGyro> gyroscopes)
{
this.controller = controller;
this.gyroscopes = new List<IMyGyro>(gyroscopes);
}
public void Update(IMyShipController controller, List<IMyGyro> gyroscopes)
{
SetController(controller);
AddGyroscopes(gyroscopes);
}
public void AddGyroscopes(List<IMyGyro> gyroscopes)
{
this.gyroscopes.AddList(gyroscopes);
this.gyroscopes = this.gyroscopes.Distinct().ToList();
}
public void SetController(IMyShipController controller)
{
this.controller = controller;
}
public void SetEnabled(bool setEnabled)
{
foreach (var gyroscope in gyroscopes)
{
gyroscope.Enabled = setEnabled;
}
}
public void SetOverride(bool setOverride)
{
foreach (var gyroscope in gyroscopes)
{
gyroscope.GyroOverride = setOverride;
}
}
public Vector2 CalculatePitchRollToAchiveVelocity(Vector3 targetVelocity)
{
Vector3 diffrence = Vector3.Normalize(controller.GetShipVelocities().LinearVelocity - targetVelocity);
Vector3 gravity = -Vector3.Normalize(controller.GetNaturalGravity());
float velocity = (float)controller.GetShipSpeed();
float proportionalModifier = (float)Math.Pow(Math.Abs(diffrence.Length()), 2);
float pitch = NotNaN(Vector3.Dot(diffrence, Vector3.Cross(gravity, controller.WorldMatrix.Right)) * velocity) * proportionalModifier / dampeningFactor;
float roll = NotNaN(Vector3.Dot(diffrence, Vector3.Cross(gravity, controller.WorldMatrix.Forward)) * velocity) * proportionalModifier / dampeningFactor;
pitch = MinAbs(pitch, 90.0f * degToRad);
roll = MinAbs(roll, 90.0f * degToRad);
return new Vector2(roll, pitch);
}
public Vector3 CalculateVelocityToAlign(float offsetPitch = 0.0f, float offsetRoll = 0.0f)
{
var gravity = -Vector3.Normalize(Vector3.TransformNormal(controller.GetNaturalGravity(), Matrix.Transpose(controller.WorldMatrix)));
var target = Vector3.Normalize(Vector3.Transform(gravity, Matrix.CreateFromAxisAngle(Vector3.Right, offsetPitch) * Matrix.CreateFromAxisAngle(Vector3.Forward, offsetRoll)));
var pitch = Vector3.Dot(Vector3.Forward, target);
var roll = Vector3.Dot(Vector3.Right, target);
return new Vector3(pitch, 0, roll);
}
public void SetAngularVelocity(Vector3 velocity)
{
var cockpitLocalVelocity = Vector3.TransformNormal(velocity, controller.WorldMatrix);
foreach (var gyro in gyroscopes)
{
var gyroLocalVelocity = Vector3.TransformNormal(cockpitLocalVelocity, Matrix.Transpose(gyro.WorldMatrix));
gyro.Pitch = gyroLocalVelocity.X;
gyro.Yaw = gyroLocalVelocity.Y;
gyro.Roll = gyroLocalVelocity.Z;
}
}
}
}
}