Jump to content

Navball working inside cockpit module part


Recommended Posts

Hello all.

I'm implementing a functional control panel in my mod Omicron, but it's not a internal that you can see with "C" key when playing KSP, but is on the vessel panel externally.

So, my question is, can i use the stock internal modules to control the parts of my panel, like navball, speed, etc, describing the relation transforms in there?

Can any one point me to some help?

Thanks.

Edited by Climberfx
Link to comment
Share on other sites

So, since no one helps me around here, i go dig myself an answer.

Find helping codes to learn on Github, other stuff on Kerbal API ref, etc.

I did not test yet, but there are no errors on Visual Basic/references. Gone try it soon.

using System;
using UnityEngine;
using KSP.UI.Screens.Flight;

// KSP 1.7.3 e Unity 2017.1.3p1

namespace Omicron
{
    public class Omicron : PartModule
    {
        private NavBall stockNavball;
        private Transform myNavballTrans;

        [KSPField]
        public string NavBallName = "navball_node";

        public void Start()
        {
            if (HighLogic.LoadedSceneIsEditor)
            {
                return;
            }
            stockNavball = FindObjectOfType<NavBall>();
            myNavballTrans = part.transform.FindRecursive(NavBallName);
        }

        public void Update()
        {
            if (HighLogic.LoadedSceneIsEditor)
            {
                return;
            }
            Quaternion attitudeGymbal = stockNavball.navBall.rotation;
            var delta = Quaternion.Inverse(attitudeGymbal);
            delta.z = -delta.z;
            myNavballTrans.localRotation = delta;

            print("Navball Rotation = " + myNavballTrans.eulerAngles + ", Local Rotation = " + myNavballTrans.localEulerAngles + "Delta = " + delta);
        }
    }

    public static class Utils
    {
        public static Transform FindRecursive(this Transform transform, String name)
        {
            if (transform.name == name) { return transform; }
            Transform tr = transform.Find(name);
            if (tr != null) { return tr; }
            foreach (Transform child in transform)
            {
                tr = child.FindRecursive(name);
                if (tr != null) { return tr; }
            }
            return null;
        }
    }
}

 

Edited by Climberfx
Link to comment
Share on other sites

Last update to the code, that is now checked inflight and working inside Omicron panel.

using System;
using UnityEngine;
using KSP.UI.Screens.Flight;

// KSP 1.7.3 e Unity 2017.1.3p1

namespace Omicron
{
    public class Omicron : PartModule
    {
        private NavBall stockNavball;
        private Transform myNavballTrans;

        [KSPField]
        public string NavBallName = "navball_node";

        public void Start()
        {
            if (HighLogic.LoadedSceneIsEditor)
            {
                return;
            }
            stockNavball = FindObjectOfType<NavBall>();
            myNavballTrans = part.transform.FindRecursive(NavBallName);
        }

        public void Update()
        {
            if (HighLogic.LoadedSceneIsEditor)
            {
                return;
            }
            Quaternion attitudeGymbal = stockNavball.navBall.rotation;
            var delta = Quaternion.Inverse(attitudeGymbal);
            delta.z = -delta.z;
            myNavballTrans.localRotation = delta;

            print("Navball Rotation = " + myNavballTrans.eulerAngles + ", Local Rotation = " + myNavballTrans.localEulerAngles + "Delta = " + delta);
        }
    }

    public static class Utils
    {
        public static Transform FindRecursive(this Transform transform, String name)
        {
            if (transform.name == name) { return transform; }
            Transform tr = transform.Find(name);
            if (tr != null) { return tr; }
            foreach (Transform child in transform)
            {
                tr = child.FindRecursive(name);
                if (tr != null) { return tr; }
            }
            return null;
        }
    }
}

Updated the code in first post too.

 

Edited by Climberfx
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...