Jump to content

Probable Quaternion question re. Navball and where ship is pointing


Recommended Posts

I need to be able to know how far off a vessel is pointing from what one of the modes (selectable).  For example, I would need to know how far off a vessel is from pointing Prograde, and also, would be nice to be able to get the rate at which the vessel is changing orientation.

I believe this would need Quaternions, which I really don't understand :-(

Good news (for me) is that I just need to know how far off it is, doesn't really matter which direction it is off in.

Any help would be appreciated.

I found this code, which I think may be part of what's needed.  It seems to get the angles for pitch, yaw and roll of the vessel, I believe that if I can get the same for the desired direction, then it's a simple matter of subtraction.

private Quaternion updateHeadingPitchRollField (Vessel v)
{
    Vector3d CoM, north, up;
    Quaternion rotationSurface;
    CoM = v.CoM;
    up = (CoM - v.mainBody.position).normalized;
    north = Vector3d.Exclude(up, (v.mainBody.position + v.mainBody.transform.up * (float)v.mainBody.Radius) - CoM).normalized;
    rotationSurface = Quaternion.LookRotation(north, up);
    return Quaternion.Inverse(Quaternion.Euler(90, 0, 0) * Quaternion.Inverse(v.GetTransform().rotation) * rotationSurface);
}

Quaternion attitude = updateHeadingPitchRollField(ActiveVessel);

float pitch = (float) ((attitude.eulerAngles.x > 180) ? (360.0 - attitude.eulerAngles.x) : -attitude.eulerAngles.x);
float yaw = (float) attitude.eulerAngles.y;
float roll = (float) ((attitude.eulerAngles.z > 180) ? (attitude.eulerAngles.z - 360.0) : attitude.eulerAngles.z);

 

Link to comment
Share on other sites

Do you need them separated in pitch, yaw, roll? If not you can simply use Vector3.Angle:

Vector3 currentOrientation = vessel.referenceTransform.up //up is foward
Vector3 targetOrientation //your desired direction
float angle = Vector3.Angle(currentOrientation, targetOrientation)

If you want to know how much your translating (or rotating) in each direction you can use vessel.angularVelocity

Edited by ValiZockt
Link to comment
Share on other sites

4 hours ago, ValiZockt said:

Do you need them separated in pitch, yaw, roll? If not you can simply use Vector3.Angle:

Very helpful, thanks

4 hours ago, ValiZockt said:

Vector3 targetOrientation //your desired direction

So all I need (for now) is to get the correct targetOrientation for each of the various autopilot modes, including a maneuver node

I got the maneuver node angle working.

Regarding the Vector3.Angle, it's not obvious whether the vessel is pointing close to the node or in the opposite direction.  I think I can live without it, but would like to know if there was a way to get a full 0 (pointing straight at th etarget orientation) to 180 (pointing away from the target orientation)

I though I had it working, but it's not perfect.  This seems to be somewhat working:

                Vector3 currentOrientation = FlightGlobals.ActiveVessel.ReferenceTransform.up; //up is foward
                Vector3 targetOrientation = //your desired direction
                        manNode.nodeRotation.eulerAngles;

                manNode.nodeRotation.ToAngleAxis(out float angle2, out targetOrientation);

                float angle = Vector3.Angle(currentOrientation, targetOrientation);
                GUILayout.Label("Angle to  node: ");
                GUILayout.TextField(angle.ToString("F2"));

When pointing to the maneuver node, the angle is 90.

when pointing directly away from the maneuver node, the angle is 90

Anywhere else, it's very odd and not consistent

Edited by linuxgurugamer
Link to comment
Share on other sites

manNode.nodeRotation.eulerAngles is a rotation, not a direction. Use manNode.GetBurnVector(vessel.orbit);

Vector3.Angle is an absolute value, but you can calculate the polarity by using the cross product of both vectors and invert the angle if y is negative

Vector3 currentOrientation = FlightGlobals.ActiveVessel.ReferenceTransform.up; //up is foward
Vector3 targetOrientation = manNode.GetBurnVector(vessel.orbit);

float angle = Vector3.Angle(currentOrientation, targetOrientation);
Vector3 polarity = Vector3.Cross(currentOrientation, targetOrientation);
if (polarity.y < 0) 
    angle = -angle;

 

Edited by ValiZockt
Link to comment
Share on other sites

18 hours ago, ValiZockt said:

manNode.nodeRotation.eulerAngles is a rotation, not a direction. Use manNode.GetBurnVector(vessel.orbit);

Vector3.Angle is an absolute value, but you can calculate the polarity by using the cross product of both vectors and invert the angle if y is negative


Vector3 currentOrientation = FlightGlobals.ActiveVessel.ReferenceTransform.up; //up is foward
Vector3 targetOrientation = manNode.GetBurnVector(vessel.orbit);

float angle = Vector3.Angle(currentOrientation, targetOrientation);
Vector3 polarity = Vector3.Cross(currentOrientation, targetOrientation);
if (polarity.y < 0) 
    angle = -angle;

 

Thank you so much, working like a charm

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...