|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using UnityEngine; |
| 7 | + |
| 8 | +namespace FreeIva.InternalModules |
| 9 | +{ |
| 10 | + // TODO: provide angle limits for rotation |
| 11 | + |
| 12 | + internal class GravityGimbal : InternalModule |
| 13 | + { |
| 14 | + [KSPField] |
| 15 | + public Vector3 rotationAxis = new Vector3(1, 0, 0); // in prop space |
| 16 | + |
| 17 | + [KSPField] |
| 18 | + public string transformName = string.Empty; |
| 19 | + |
| 20 | + [KSPField] |
| 21 | + public float minAccel = 0.05f; |
| 22 | + |
| 23 | + [KSPField] |
| 24 | + public float smoothingFactor = 0.2f; |
| 25 | + |
| 26 | + [SerializeField] Transform m_controlledTransform; |
| 27 | + |
| 28 | + InternalModuleFreeIva m_freeIvaModule; |
| 29 | + Vector3 m_rotationAxisInternalSpace; |
| 30 | + Quaternion m_defaultRotation; |
| 31 | + |
| 32 | + public override void OnLoad(ConfigNode node) |
| 33 | + { |
| 34 | + base.OnLoad(node); |
| 35 | + |
| 36 | + if (HighLogic.LoadedScene == GameScenes.LOADING) |
| 37 | + { |
| 38 | + if (transformName != string.Empty) |
| 39 | + { |
| 40 | + m_controlledTransform = TransformUtil.FindPropTransform(internalProp, transformName); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + m_controlledTransform = internalProp.hasModel ? transform : internalModel.transform; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + protected void Start() |
| 50 | + { |
| 51 | + m_freeIvaModule = InternalModuleFreeIva.GetForModel(internalModel); |
| 52 | + m_rotationAxisInternalSpace = transform.TransformDirection(rotationAxis); |
| 53 | + m_defaultRotation = transform.rotation; |
| 54 | + } |
| 55 | + |
| 56 | + void FixedUpdate() |
| 57 | + { |
| 58 | + if (m_controlledTransform != null) |
| 59 | + { |
| 60 | + Vector3 subjectiveGravity = FreeIva.GetInternalSubjectiveAcceleration(m_freeIvaModule, m_controlledTransform.position); |
| 61 | + Quaternion targetRotation; |
| 62 | + |
| 63 | + if (KerbalIvaController.UseHorizon(subjectiveGravity, m_freeIvaModule.Centrifuge != null)) |
| 64 | + { |
| 65 | + Vector3 forward = Vector3.Cross(subjectiveGravity, m_rotationAxisInternalSpace); |
| 66 | + Vector3 up = Vector3.Cross(forward, m_rotationAxisInternalSpace); |
| 67 | + |
| 68 | + targetRotation = Quaternion.LookRotation(forward, up); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + targetRotation = m_defaultRotation; |
| 73 | + } |
| 74 | + |
| 75 | + m_controlledTransform.rotation = Quaternion.Lerp(m_controlledTransform.rotation, targetRotation, smoothingFactor); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments