Skip to content

Commit 4f64cab

Browse files
authored
Add GravityGimbal and InstanceInternal modules (#526)
it's the age of alliteration! Fixes #515 and #524
1 parent 7ec9330 commit 4f64cab

6 files changed

Lines changed: 164 additions & 9 deletions

File tree

FreeIva.sln

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.1.32210.238
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeIva", "FreeIva\FreeIva.csproj", "{AEB6EEAB-46BC-49EE-8941-3833E1B1171B}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeIva", "FreeIva\FreeIva.csproj", "{AEB6EEAB-46BC-49EE-8941-3833E1B1171B}"
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4D82D2DE-C252-46FC-852E-3CFB355CA3A6}"
99
ProjectSection(SolutionItems) = preProject
1010
.editorconfig = .editorconfig
1111
CHANGELOG.md = CHANGELOG.md
12+
FreeIva\FreeIva.csproj.user = FreeIva\FreeIva.csproj.user
1213
GameData\FreeIva\FreeIva.version = GameData\FreeIva\FreeIva.version
1314
README.md = README.md
1415
EndProjectSection
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
class ConfigNodeHolder : ScriptableObject
11+
{
12+
public ConfigNode Node;
13+
}
14+
15+
internal class InstanceInternal : InternalModule
16+
{
17+
[SerializeReference]
18+
ConfigNodeHolder m_prefabInternalNode;
19+
ConfigNode m_internalNode;
20+
21+
[KSPField]
22+
public string parentTransformName = string.Empty;
23+
[SerializeField] Transform m_parentTransform;
24+
25+
public override void OnLoad(ConfigNode moduleNode)
26+
{
27+
base.OnLoad(moduleNode);
28+
29+
var internalNode = moduleNode.GetNode("INTERNAL");
30+
31+
if (HighLogic.LoadedScene == GameScenes.LOADING)
32+
{
33+
m_prefabInternalNode = ScriptableObject.CreateInstance<ConfigNodeHolder>();
34+
m_prefabInternalNode.Node = internalNode;
35+
36+
if (parentTransformName != string.Empty)
37+
{
38+
m_parentTransform = TransformUtil.FindPropTransform(internalProp, parentTransformName);
39+
}
40+
else
41+
{
42+
m_parentTransform = internalProp.hasModel ? transform : internalModel.transform;
43+
}
44+
}
45+
46+
if (internalNode != null)
47+
{
48+
m_internalNode = internalNode;
49+
}
50+
}
51+
52+
void Start()
53+
{
54+
m_internalNode = m_internalNode ?? m_prefabInternalNode?.Node;
55+
string internalName = m_internalNode?.GetValue("name");
56+
57+
if (internalName == null || m_parentTransform == null)
58+
{
59+
return;
60+
}
61+
62+
var internalPrefab = PartLoader.GetInternalPart(internalName);
63+
if (internalPrefab != null)
64+
{
65+
var internalModel = GameObject.Instantiate(internalPrefab);
66+
internalModel.transform.SetParent(m_parentTransform, false);
67+
internalModel.part = part;
68+
internalModel.gameObject.SetActive(true);
69+
internalModel.Load(m_internalNode);
70+
}
71+
}
72+
}
73+
}

FreeIva/KerbalIvaController.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,15 +426,20 @@ public void UpdatePosition(Vector3 flightAccel, Vector3 movementThrottle, bool j
426426
#endif
427427
}
428428

429-
Vector3 UpdateGravity()
429+
internal static bool UseHorizon(Vector3 flightAccel, bool inCentrifuge)
430430
{
431-
Vector3 flightAccel = FreeIva.GetInternalSubjectiveAcceleration(FreeIva.CurrentInternalModuleFreeIva, transform.position);
432-
433-
float minAccelForHorizon = (FlightGlobals.ActiveVessel.LandedOrSplashed || currentCentrifuge != null)
431+
float minAccelForHorizon = (FlightGlobals.ActiveVessel.LandedOrSplashed || inCentrifuge)
434432
? MIN_ACCEL_FOR_HORIZON_LANDED
435433
: MIN_ACCEL_FOR_HORIZON_AIRBORNE;
436434

437-
usingRelativeMovement = flightAccel.magnitude >= minAccelForHorizon;
435+
return flightAccel.magnitude >= minAccelForHorizon;
436+
}
437+
438+
Vector3 UpdateGravity()
439+
{
440+
Vector3 flightAccel = FreeIva.GetInternalSubjectiveAcceleration(FreeIva.CurrentInternalModuleFreeIva, transform.position);
441+
442+
usingRelativeMovement = UseHorizon(flightAccel, currentCentrifuge != null);
438443
horizonDownVector = usingRelativeMovement ? flightAccel : Vector3.zero;
439444

440445
// update attaching/detaching to things (note that detaching from a centrifuge is handled in ExitCentrifuge)

GameData/FreeIva/HabTechProps/htProps_HatchCBM.cfg

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ PROP
1111
{
1212
model = FreeIva/Props/HatchBoxCollider
1313
position = 0, 0.00826506782, 0
14-
rotation = 0, 0, 0, -1 //quaternion
1514
scale = 0.604978085, 0.0175083745, 0.605912209
1615
}
1716

1817
MODEL
1918
{
2019
model = HabTechProps/Props/doorHatchWindowPlug
2120
position = 0, 0.0130386753, -0.221190825
22-
rotation = 0, 0, 0, -1 //quaternion
2321
}
2422
}
2523

GameData/FreeIva/HabTechProps/htProps_HatchDoor.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ PROP
1010
MODEL
1111
{
1212
model = FreeIva/Props/HatchBoxCollider
13-
rotation = 0, 0, 0, -1
1413
scale = 0.774956346, 0.0422763638, 1
1514
}
1615

0 commit comments

Comments
 (0)