Jump to content

Need to rotate vessel when recovering in KCT


Recommended Posts

Hi,

I'm now supporting KCT, and there is a long-standing bug I'm looking in to.  The issue is that when KCT recovers a plane and stores it, when it is relaunched it is vertical on it's tail, same goes for when editing the stored craft.

I'm pretty sure it's a result of the SPH being rotated 90 degrees from the VAB (ie:  load a vessel in the VAB, switch to the SPH and click the root part, the vessel flips 90 degrees).

So, what I think I need to do is to flip the plane -90 degrees when recovering it.

But this is dealing with Quaternions, and I have no idea what to do.

Can anyone help?  Assume the active vessel is being recovered (FlightGlobals.ActiveVessels) and it's a plane.

Thanks in advance

Edited by linuxgurugamer
Link to comment
Share on other sites

Following up on this, I've gotten the following code:

var vessel = FlightGlobals.ActiveVessel;
vessel.GoOnRails();
var originalUp = FlightGlobals.getUpAxis();
var newUp = new Vector3(90,0,0); // Get the vector of the rotation, I suppose it could be 1 here

// adjust vessel rotation based on how much the up direction changed
var diff = Quaternion.FromToRotation(originalUp, newUp);

vessel.SetRotation(diff * vessel.transform.rotation);
vessel.GoOffRails();

which is being run just before the vessel gets recovered.  Unfortunately, it's not doing anything that I can see.  Right now, I just want to see it rotated in any direction, once I have that, it will be easy to get the correct rotation.

Link to comment
Share on other sites

17 minutes ago, peteletroll said:

When a vessel is on rails, the position of the parts is stored in orgPos and orgRot. Probably they have to be changed.

But I don't want the parts.  The whole idea of putting the vessel on rails is to only change the vessel's rotation.

Link to comment
Share on other sites

"Vessel" is just a set of parts, connected by joints when in flight. When you go on rails, the physical part of the vessel disappears, and the vessel becomes a set of parts placed according to orgPos and orgRot. orgPos and orgRot are relative to root part, so root part has orgPos = Vector3.zero and orgRot = Quaternion.identity.

This applies when KSP itself puts a vessel on rails. You're calling goOnRails() explicitly, so YMMV.

Link to comment
Share on other sites

Once you know their secret, rotation with quaternions is actually very easy to understand since they are compressed rotation matrices.

The secret: the x,y,z components form the rotation axis, but scaled by the sine of the half-angle. The w component is the cosine of the half-angle. Thus, if you want to rotate by 90 degrees around the X-axis, you set x,y,z to sqrt(0.5),0,0 and w to sqrt(0.5) (ie, sin and cos of 45 degrees because of the half angle). The direction of rotation follows the left-hand rule in Unity (because Unity defaults to a left-handed system). Thus I think the desired quaternion for this problem is sqrt(0.5), 0, 0, sqrt(0.5) (might need to negate x).

0, 0, 0, 1 = unrotated

0, 0, 1, 0 = 180 degrees around Z

0, 0.5,  0, sqrt(3)/2 = 60 degrees clockwise around Y (remember, left-hand rule, and half angle, so sin(30) and cos(30) instead of 60)

 

Link to comment
Share on other sites

I finally got it working properly, thanks to you.

Turns out that there were two problems.  The first was that I was setting the rotation in one place without realizing that further down in the code, it was resetting the rotation.

The second was that the original code for this was from a very old mod called InflightShipSave, by Claw.  In that one, he doesn't bother to check to see if the vessel is a plane, and this mod and another, the Ship Save Splicer, both use the same bit of code

For anyone who might be interested, here is the method in question:

private ConfigNode FromInFlightVessel(Vessel VesselToSave, KCT_BuildListVessel.ListType listType)
{
    //This code is taken from InflightShipSave by Claw, using the CC-BY-NC-SA license.
    //This code thus is licensed under the same license, despite the GPLv3 license covering original KCT code
    //See https://github.com/ClawKSP/InflightShipSave

    string ShipName = VesselToSave.vesselName;
    // Debug.LogWarning("Saving: " + ShipName);

    ShipConstruct ConstructToSave = new ShipConstruct(ShipName, "", VesselToSave.parts[0]);

    Quaternion OriginalRotation = VesselToSave.vesselTransform.rotation;
    Vector3 OriginalPosition = VesselToSave.vesselTransform.position;

    if (listType == ListType.SPH)
    {
        // This first line is from the original mod:
        //VesselToSave.SetRotation(new Quaternion(0, 0, 0, 1)); //TODO: Figure out the orientation this should be

        // This is the updated line, thanks to @Taniwha
        VesselToSave.SetRotation(new Quaternion((float)Math.Sqrt(0.5), 0, 0, (float)Math.Sqrt(0.5)));
    }
    else
    {
        VesselToSave.SetRotation(new Quaternion(0, 0, 0, 1));
    }
    Vector3 ShipSize = ShipConstruction.CalculateCraftSize(ConstructToSave);
    VesselToSave.SetPosition(new Vector3(0, Math.Min(ShipSize.y + 2, 30), 0)); //Try to limit the max height we put the ship at. 60 is good for the VA but I don't know about the SPH. Lets be conservative with 30

    ConfigNode CN = new ConfigNode("ShipConstruct");
    CN = ConstructToSave.SaveShip();
    SanitizeShipNode(CN);

    VesselToSave.SetRotation(OriginalRotation);
    VesselToSave.SetPosition(OriginalPosition);
    //End of Claw's code. Thanks Claw!
    return CN;
}

 

 

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...