Skip to content

Commit

Permalink
Change various axes orientations for docking instrument
Browse files Browse the repository at this point in the history
  • Loading branch information
Safarte committed Jan 2, 2024
1 parent dde9c6a commit 5d2d345
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion plugin_template/swinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 15 additions & 11 deletions src/DockingAlignmentDisplay/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}
}

0 comments on commit 5d2d345

Please sign in to comment.