DirectionToQuaternion() public static method

public static DirectionToQuaternion ( Transform transf, Vector3 nodeDirection ) : Quaternion
transf UnityEngine.Transform
nodeDirection Vector3
return UnityEngine.Quaternion
Beispiel #1
0
 private void SetEvaNode(Vessel kerbalEvaVessel)
 {
     //Get eva transform
     evaCollider = KAS_Shared.GetEvaCollider(kerbalEvaVessel, evaTransformName);
     if (evaNodeTransform)
     {
         Destroy(evaNodeTransform.gameObject);
     }
     evaNodeTransform               = new GameObject("KASEvaNode").transform;
     evaNodeTransform.parent        = evaCollider.transform;
     evaNodeTransform.localPosition = evaPartPos;
     evaNodeTransform.rotation      = KAS_Shared.DirectionToQuaternion(evaCollider.transform, evaPartDir);
 }
Beispiel #2
0
 public static void AddNodeTransform(Part p, AttachNode attachNode)
 {
     if (attachNode.nodeTransform == null)
     {
         Transform nodeTransform = new GameObject("KASNodeTransf").transform;
         nodeTransform.parent        = p.transform;
         nodeTransform.localPosition = attachNode.position;
         nodeTransform.rotation      = KAS_Shared.DirectionToQuaternion(p.transform, attachNode.orientation);
         attachNode.nodeTransform    = nodeTransform;
     }
     else
     {
         attachNode.nodeTransform.localPosition = attachNode.position;
         attachNode.nodeTransform.rotation      = KAS_Shared.DirectionToQuaternion(p.transform, attachNode.orientation);
         KAS_Shared.DebugLog("AddTransformToAttachNode - Node : " + attachNode.id + " already have a nodeTransform, only update");
     }
 }
Beispiel #3
0
        public void Grab(Vessel kerbalEvaVessel)
        {
            KAS_Shared.DebugLog("Grab - Grabbing part :" + this.part.partInfo.name);

            //Get eva transform
            evaCollider = KAS_Shared.GetEvaCollider(kerbalEvaVessel, evaTransformName);
            if (!evaCollider)
            {
                KAS_Shared.DebugLog("Grab - " + evaTransformName + "transform not found on eva !");
                return;
            }

            //Get attach node
            if (attachNodeName == null || attachNodeName == "")
            {
                if (this.part.srfAttachNode == null)
                {
                    KAS_Shared.DebugLog("Grab - surface attach node cannot be found on the part !");
                    return;
                }
                KAS_Shared.AddNodeTransform(this.part, this.part.srfAttachNode);
                partNode = this.part.srfAttachNode;
            }
            else
            {
                AttachNode an = this.part.findAttachNode(attachNodeName);
                if (an == null)
                {
                    KAS_Shared.DebugLog("Grab - " + attachNodeName + " node cannot be found on the part !");
                    return;
                }
                KAS_Shared.AddNodeTransform(this.part, an);
                partNode = an;
            }

            //Send message to other modules
            base.SendMessage("OnPartGrab", kerbalEvaVessel, SendMessageOptions.DontRequireReceiver);

            //Drop grabbed part on eva if needed
            KASModuleGrab tmpGrabbbedPartModule = KAS_Shared.GetGrabbedPartModule(kerbalEvaVessel);

            if (tmpGrabbbedPartModule)
            {
                KAS_Shared.DebugWarning("Grab - Drop current grabbed part");
                tmpGrabbbedPartModule.Drop();
            }

            evaNodeTransform               = new GameObject("KASEvaNode").transform;
            evaNodeTransform.parent        = evaCollider.transform;
            evaNodeTransform.localPosition = evaPartPos;
            evaNodeTransform.rotation      = KAS_Shared.DirectionToQuaternion(evaCollider.transform, evaPartDir);

            KAS_Shared.MoveAlign(this.part.transform, partNode.nodeTransform, evaNodeTransform);

            //Grab winch connected head if any
            KASModuleWinch moduleWinch = KAS_Shared.GetConnectedWinch(this.part);

            if (moduleWinch)
            {
                KASModulePort modulePort = this.part.GetComponent <KASModulePort>();
                moduleWinch.UnplugHead(false);
                moduleWinch.GrabHead(kerbalEvaVessel, modulePort);
            }

            List <Collider> allColliders = new List <Collider>(this.part.GetComponentsInChildren <Collider>() as Collider[]);

            foreach (Collider col in allColliders)
            {
                col.isTrigger = true;
            }

            Detach();
            KAS_Shared.DecoupleFromAll(this.part);
            this.part.Couple(kerbalEvaVessel.rootPart);
            //Destroy joint to avoid buggy eva move
            Destroy(this.part.attachJoint);

            this.part.rigidbody.velocity = kerbalEvaVessel.rootPart.rigidbody.velocity;

            if (physicJoint)
            {
                if (evaJoint)
                {
                    Destroy(evaJoint);
                }
                evaJoint = this.part.gameObject.AddComponent <FixedJoint>();
                evaJoint.connectedBody = evaCollider.attachedRigidbody;
                evaJoint.breakForce    = 5;
                evaJoint.breakTorque   = 5;
            }
            else
            {
                this.part.physicalSignificance  = Part.PhysicalSignificance.NONE;
                this.part.transform.parent      = evaNodeTransform;
                this.part.rigidbody.isKinematic = true;
            }

            //Add grabbed part mass to eva
            if (addPartMass && !physicJoint)
            {
                orgKerbalMass = kerbalEvaVessel.rootPart.mass;
                kerbalEvaVessel.rootPart.mass += this.part.mass;
            }

            evaHolderVesselName = kerbalEvaVessel.vesselName;
            evaHolderPart       = kerbalEvaVessel.rootPart;
            grabbed             = true;

            RefreshContextMenu();

            //Play grab sound
            fxSndGrab.audio.Play();
            base.SendMessage("OnPartGrabbed", kerbalEvaVessel, SendMessageOptions.DontRequireReceiver);
        }
Beispiel #4
0
        private bool AlignEvaPosition(Vessel kerbalEvaVessel)
        {
            //Get eva transform
            evaCollider = KAS_Shared.GetEvaCollider(kerbalEvaVessel, evaTransformName);
            if (!evaCollider)
            {
                KAS_Shared.DebugLog("Grab - " + evaTransformName + "transform not found on eva !");
                return(false);
            }

            //Get attach node
            if (attachNodeName == null || attachNodeName == "")
            {
                if (this.part.srfAttachNode == null)
                {
                    KAS_Shared.DebugLog("Grab - surface attach node cannot be found on the part !");
                    return(false);
                }
                KAS_Shared.AddNodeTransform(this.part, this.part.srfAttachNode);
                partNode = this.part.srfAttachNode;
            }
            else
            {
                AttachNode an = this.part.findAttachNode(attachNodeName);
                if (an == null)
                {
                    KAS_Shared.DebugLog("Grab - " + attachNodeName + " node cannot be found on the part !");
                    return(false);
                }
                KAS_Shared.AddNodeTransform(this.part, an);
                partNode = an;
            }

            //Send message to other modules
            base.SendMessage("OnPartGrab", kerbalEvaVessel, SendMessageOptions.DontRequireReceiver);

            //Drop grabbed part on eva if needed
            KASModuleGrab tmpGrabbbedPartModule = KAS_Shared.GetGrabbedPartModule(kerbalEvaVessel);

            if (tmpGrabbbedPartModule)
            {
                if (tmpGrabbbedPartModule.part.packed)
                {
                    KAS_Shared.DebugWarning("Grab - cannot drop an incompletely grabbed part!");
                    return(false);
                }

                KAS_Shared.DebugWarning("Grab - Drop current grabbed part");
                tmpGrabbbedPartModule.Drop();
            }

            if (evaNodeTransform)
            {
                Destroy(evaNodeTransform.gameObject);
            }

            evaNodeTransform               = new GameObject("KASEvaNode").transform;
            evaNodeTransform.parent        = evaCollider.transform;
            evaNodeTransform.localPosition = evaPartPos;
            evaNodeTransform.rotation      = KAS_Shared.DirectionToQuaternion(evaCollider.transform, evaPartDir);

            KAS_Shared.MoveAlign(this.part.transform, partNode.nodeTransform, evaNodeTransform);

            if (this.part.vessel == kerbalEvaVessel)
            {
                this.part.UpdateOrgPosAndRot(kerbalEvaVessel.rootPart);
            }

            return(true);
        }
Beispiel #5
0
        public void UpdatePointer()
        {
            if (!running)
            {
                if (pointer)
                {
                    UnityEngine.Object.Destroy(pointer);
                }
                return;
            }

            //Cast ray
            Ray        ray = FlightCamera.fetch.mainCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (!Physics.Raycast(ray, out hit, 500, 557059))
            {
                if (pointer)
                {
                    UnityEngine.Object.Destroy(pointer);
                }
                return;
            }

            //Create pointer if needed
            if (!pointer)
            {
                GameObject modelGo = partToAttach.FindModelTransform("model").gameObject;
                pointer = Mesh.Instantiate(modelGo) as GameObject;
                foreach (Collider col in pointer.GetComponentsInChildren <Collider>())
                {
                    UnityEngine.Object.Destroy(col);
                }

                allModelMr = new List <MeshRenderer>();
                // Remove attached tube mesh renderer if any
                List <MeshRenderer> tmpAllModelMr = new List <MeshRenderer>(pointer.GetComponentsInChildren <MeshRenderer>() as MeshRenderer[]);
                foreach (MeshRenderer mr in tmpAllModelMr)
                {
                    if (mr.name == "KAStube" || mr.name == "KASsrcSphere" || mr.name == "KASsrcTube" || mr.name == "KAStgtSphere" || mr.name == "KAStgtTube")
                    {
                        Destroy(mr);
                        continue;
                    }
                    allModelMr.Add(mr);
                    mr.material = new Material(Shader.Find("Transparent/Diffuse"));
                }
                pointerNodeTransform               = new GameObject("KASPointerPartNode").transform;
                pointerNodeTransform.parent        = pointer.transform;
                pointerNodeTransform.localPosition = partToAttach.srfAttachNode.position;
                pointerNodeTransform.rotation      = KAS_Shared.DirectionToQuaternion(pointer.transform, partToAttach.srfAttachNode.orientation);
            }

            //Set default color
            Color color = Color.green;

            // Check if object is valid
            bool      isValidObj = false;
            Part      hitPart    = null;
            KerbalEVA hitEva     = null;

            if (hit.rigidbody)
            {
                hitPart = hit.rigidbody.GetComponent <Part>();
                hitEva  = hit.rigidbody.GetComponent <KerbalEVA>();
                if (hitPart && allowPart && !hitEva & hitPart != partToAttach)
                {
                    isValidObj = true;
                }
                if (hitEva && allowEva)
                {
                    isValidObj = true;
                }
            }
            if (!hitPart && !hitEva && allowStatic)
            {
                isValidObj = true;
            }

            //Check distance
            bool isValidDist = false;

            if (sourceTransform)
            {
                float distToPointer = Vector3.Distance(sourceTransform.position, hit.point);
                if (distToPointer > maxDist)
                {
                    isValidDist = false;
                }
                else
                {
                    isValidDist = true;
                }
            }
            else
            {
                isValidDist = true;
            }

            //Set color
            if (!isValidObj)
            {
                color = Color.red;
            }
            if (!isValidDist)
            {
                color = Color.yellow;
            }
            color.a = 0.5f;
            foreach (MeshRenderer mr in allModelMr)
            {
                mr.material.color = color;
            }

            //Rotation keys
            if (Input.GetKeyDown(KASAddonControlKey.rotateLeftKey.ToLower()))
            {
                RotatePointer(-15);
            }
            if (Input.GetKeyDown(KASAddonControlKey.rotateRightKey.ToLower()))
            {
                RotatePointer(+15);
            }

            KAS_Shared.MoveAlign(pointer.transform, pointerNodeTransform, hit);
            pointer.transform.rotation *= Quaternion.Euler(customRot);

            //Attach on click
            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                KAS_Shared.DebugLog("Attachment started...");
                if (!isValidObj)
                {
                    ScreenMessages.PostScreenMessage("Can't attach, target is not allowed !");
                    audioBipWrong.Play();
                    return;
                }

                if (!isValidDist)
                {
                    ScreenMessages.PostScreenMessage("Can't attach, too far from target !");
                    audioBipWrong.Play();
                    return;
                }

                KASModuleGrab modulegrab = partToAttach.GetComponent <KASModuleGrab>();


                //Move and attach mode
                if (pointerMode == PointerMode.MoveAndAttach)
                {
                    // Drop and detach part if needed
                    if (modulegrab)
                    {
                        if (modulegrab.grabbed)
                        {
                            modulegrab.Drop();
                        }
                        modulegrab.Detach();
                    }

                    KASModuleWinch connectedWinch = KAS_Shared.GetConnectedWinch(partToAttach);
                    if (!connectedWinch)
                    {
                        KAS_Shared.DecoupleFromAll(partToAttach);
                    }

                    //Move part
                    partToAttach.transform.position = pointer.transform.position;
                    partToAttach.transform.rotation = pointer.transform.rotation;

                    if (connectedWinch)
                    {
                        //Set cable lenght to real lenght
                        connectedWinch.cableJointLength = connectedWinch.cableRealLenght;
                    }

                    if (msgOnly)
                    {
                        KAS_Shared.DebugLog("UpdatePointer(Pointer) Attach using send message");
                        if (hitPart)
                        {
                            partToAttach.SendMessage("OnAttachPart", hitPart, SendMessageOptions.DontRequireReceiver);
                        }
                        else
                        {
                            partToAttach.SendMessage("OnAttachStatic", SendMessageOptions.DontRequireReceiver);
                        }
                    }
                    else
                    {
                        KAS_Shared.DebugLog("UpdatePointer(Pointer) Attach with couple or static method");
                        if (!hitPart && !hitEva)
                        {
                            if (modulegrab)
                            {
                                modulegrab.AttachStatic();
                                modulegrab.fxSndAttachStatic.audio.Play();
                            }
                            else
                            {
                                KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, part cannot be attached on static");
                            }
                        }
                        else
                        {
                            partToAttach.Couple(hitPart);
                            if (modulegrab)
                            {
                                modulegrab.fxSndAttachPart.audio.Play();
                            }
                            else
                            {
                                KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, cannot fire sound");
                            }
                        }
                        partToAttach.SendMessage("OnAttach", SendMessageOptions.DontRequireReceiver);
                    }
                }

                if (pointerMode == PointerMode.CopyAndAttach)
                {
                    // Not tested !
                    Part newPart = KAS_Shared.CreatePart(partToAttach.partInfo, pointer.transform.position, pointer.transform.rotation, partToAttach);
                    if (msgOnly)
                    {
                        if (hitPart)
                        {
                            StartCoroutine(WaitAndSendMsg(newPart, pointer.transform.position, pointer.transform.rotation, hitPart));
                        }
                        else
                        {
                            StartCoroutine(WaitAndSendMsg(newPart, pointer.transform.position, pointer.transform.rotation));
                        }
                    }
                    else
                    {
                        if (!hitPart && !hitEva)
                        {
                            StartCoroutine(WaitAndAttach(newPart, pointer.transform.position, pointer.transform.rotation, hitPart));
                        }
                        else
                        {
                            StartCoroutine(WaitAndAttach(newPart, pointer.transform.position, pointer.transform.rotation));
                        }
                    }
                }
                running = false;
            }
        }