diff --git a/plugin_template/swinfo.json b/plugin_template/swinfo.json index 8b809b2..6002341 100644 --- a/plugin_template/swinfo.json +++ b/plugin_template/swinfo.json @@ -5,7 +5,7 @@ "name": "Docking Alignment Display", "description": "Adds a new instrument providing necessary information to dock with precision.", "source": "https://github.com/Safarte/DockingAlignmentDisplay", - "version": "0.4.2", + "version": "0.4.3", "version_check": "https://raw.githubusercontent.com/Safarte/DockingAlignmentDisplay/main/plugin_template/swinfo.json", "ksp2_version": { "min": "0.1.5", diff --git a/src/DockingAlignmentDisplay/Target.cs b/src/DockingAlignmentDisplay/Target.cs index 9a4274c..ac685ec 100644 --- a/src/DockingAlignmentDisplay/Target.cs +++ b/src/DockingAlignmentDisplay/Target.cs @@ -29,10 +29,10 @@ internal class Target // Target's orbit private PatchedConicsOrbit _targetOrbit; - private Vector3d _tgtFwd; - private Vector3d _tgtLeft; // Target's basis + private Vector3d _tgtFwd; + private Vector3d _tgtLeft; private Vector3d _tgtUp; public Vector3 RelativePosition @@ -48,13 +48,17 @@ public Vector3 RelativePosition Vector3 localTargetCenter = _targetFrame.ToLocalPosition(targetCenter); // Relative position vector - var tgtToVessel = localCenter - localTargetCenter; + var tgtToVessel = localTargetCenter - localCenter; // Compute offset var offset = Vector3.ProjectOnPlane(tgtToVessel, _tgtUp); + // Use the active vessel orientation as a reference + var localLeft = _targetFrame.ToLocalVector(_activeVessel.ControlTransform.left); + var localFwd = _targetFrame.ToLocalVector(_activeVessel.ControlTransform.forward); + // Full relative position vector - var relPos = new Vector3(-Vector3.Dot(_tgtFwd, offset), -Vector3.Dot(_tgtLeft, offset), + var relPos = -new Vector3(Vector3.Dot(localLeft, offset), Vector3.Dot(localFwd, offset), Vector3.Dot(_tgtUp, tgtToVessel)); return relPos; @@ -74,14 +78,14 @@ public Vector3 RelativeVelocity Vector3 localTargetVel = _targetFrame.ToLocalVector(targetVel); // Relative position vector - var velDiff = localVel - localTargetVel; + var velDiff = localTargetVel - localVel; // Project onto docking port plane var velProj = Vector3.ProjectOnPlane(velDiff, _tgtUp); // Relative velocity - var relVel = new Vector3(Vector3.Dot(_tgtFwd, velProj), Vector3.Dot(_tgtLeft, velProj), - -Vector3.Dot(_tgtUp, velDiff)); + var relVel = new Vector3(Vector3.Dot(_tgtLeft, velProj), -Vector3.Dot(_tgtFwd, velProj), + Vector3.Dot(_tgtUp, velDiff)); return relVel; } @@ -101,7 +105,7 @@ public Vector3 RelativeOrientation var upProj = Vector3.ProjectOnPlane(localUp, _tgtUp); // Full relative orientation - var relOrientation = new Vector3(Vector3.Dot(_tgtFwd, upProj), -Vector3.Dot(_tgtLeft, upProj), + var relOrientation = new Vector3(-Vector3.Dot(_tgtLeft, upProj), -Vector3.Dot(_tgtFwd, upProj), Vector3.Dot(_tgtUp, localUp)); return relOrientation; @@ -120,7 +124,7 @@ public float RelativeRoll // Relative roll in radians ([-PI, PI]) var relRoll = -Mathf.Sign(Vector3.Dot(localFwd, _tgtLeft.normalized)) * - Mathf.Acos(-Vector3.Dot(localFwd, _tgtFwd.normalized)); + Mathf.Acos(Vector3.Dot(localFwd, _tgtFwd.normalized)); return relRoll; } @@ -146,10 +150,10 @@ public void Update() if (_currentTarget.IsPart) _targetOrbit = _currentTarget.Part.PartOwner.SimulationObject.Orbit as PatchedConicsOrbit; - // Target frame & up + // Target frame & basis _targetFrame = _currentTarget.transform.coordinateSystem; - _tgtUp = _targetFrame.ToLocalVector(_currentTarget.transform.up); _tgtFwd = _targetFrame.ToLocalVector(_currentTarget.transform.forward); _tgtLeft = _targetFrame.ToLocalVector(_currentTarget.transform.left); + _tgtUp = _targetFrame.ToLocalVector(_currentTarget.transform.up); } } \ No newline at end of file