Example #1
0
    public virtual void CreateBaseAnchor()
    {
        //create anchor base
        servoBase      = new GameObject().transform;
        servoBase.name = "Servo Base";
        servoBase.SetParent(transform);
        //servoBase.localEulerAngles = Vector3.zero;
        servoBase.localPosition = Vector3.zero;
        // servoBase.localEulerAngles -= new Vector3(kspStartAngle, 0, 0);
        servoBase.localRotation = Quaternion.Inverse(memoryBridge.GetQuaternion(servoName + "servoLocalRot"));
        servoBase.SetParent(transform.parent);
        transform.SetParent(servoBase);
        //servoBase.SetParent(transform.parent);
        //servoBase.SetParent(transform);

        transform.localEulerAngles = Vector3.zero;


        //works
        //servoBase.SetParent(transform.parent);
        //servoBase.rotation = transform.parent.rotation;
        //servoBase.localPosition = transform.localPosition;
        //servoBase.name = servoName + " base";

        //DebugVector.DrawVector(servoBase, DebugVector.Direction.all, .5f, .05f, Color.red, Color.green, Color.blue);

        //Vector3 lookAtPoint = Vector3.zero;
        //if (hostPart.kspPartName == "IR.Rotatron.Basic")
        //    servoBase.localEulerAngles = new Vector3(0, 90, 90);
        //lookAtPoint = servoBase.position + servoBase.rotation * (new Vector3(0, 1, 0));
        //servoBase.LookAt(lookAtPoint, Vector3.up);
    }
    public void VesselUpdate()
    {
        var vesselOffst = memoryBridge.GetVector3("AdjustedVesselCOMoffset" + memoryBridge.fileName);

        vessel.meshOffset.localPosition         = vesselOffst;
        targetVessel.vesselOffset.localPosition = vesselOffst;

        var kspOffset = memoryBridge.GetQuaternion("VesselGimbleOffset" + memoryBridge.fileName);

        vessel.transform.rotation = kspOffset;

        adjustedGimbal.rotation = vessel.transform.rotation;

        var tempRot = adjustedGimbal.localRotation;

        tempRot.x = 0;
        tempRot.z = 0;
        adjustedGimbal.localRotation = tempRot;

        if (controlMode == FlightControlMode.RawStick)
        {
            targetVessel.vessel.rotation = adjustedGimbal.rotation;

            System.TimeSpan deltaTime = TimeSpan.FromSeconds(Time.fixedDeltaTime);

            bool recievingPitchInput = false;
            bool recievingRollInput  = false;
            bool recievingYawInput   = false;

            var rollInput  = Input.GetAxis("Roll");
            var pitchInput = Input.GetAxis("Pitch");
            var yawInput   = Input.GetAxis("Yaw");

            //  Debug.Log(pitcInput);

            if (rollInput != 0)
            {
                recievingRollInput = true;
            }
            if (pitchInput != 0)
            {
                recievingPitchInput = true;
            }
            if (yawInput != 0)
            {
                recievingYawInput = true;
            }

            // var tempVesselRot = targetVessel.vessel.transform.rotation;
            if (recievingYawInput)
            {
                memoryBridge.SetFloat("VesselYaw", yawInput);
                // tempVesselRot.y = mirrorVessel.vessel.transform.rotation.y;
            }
            else
            {
                memoryBridge.SetFloat("VesselYaw", 1.1f);
            }
            if (recievingPitchInput)
            {
                memoryBridge.SetFloat("VesselPitch", pitchInput);
                // tempVesselRot.x = mirrorVessel.vessel.transform.rotation.x;
            }
            else
            {
                memoryBridge.SetFloat("VesselPitch", 1.1f);
            }
            if (recievingRollInput)
            {
                memoryBridge.SetFloat("VesselRoll", rollInput);
                // tempVesselRot.z = mirrorVessel.vessel.transform.rotation.z;
            }
            else
            {
                memoryBridge.SetFloat("VesselRoll", 1.1f);
            }
            //  targetVessel.vessel.transform.rotation = tempVesselRot;

            var targetVesselOffset = Quaternion.Inverse(targetVessel.vessel.rotation) * vessel.transform.rotation;

            //if (rollInput > 0 & Math.Abs(rollInput) <= 1)
            //{
            //    memoryBridge.SetFloat("VesselRoll", rollInput);
            //}
            //else
            //{
            //    memoryBridge.SetFloat("VesselRoll", PIDroll.CalculateResult(targetVesselOffset.z * 10, deltaTime));
            //}

            //memoryBridge.SetFloat("VesselPitch", pitchInput);
            //memoryBridge.SetFloat("VesselYaw", yawInput);

            // var targetVesselOffset = targetVessel.vessel.localEulerAngles - mirrorVessel.vessel.localEulerAngles;

            //   Debug.Log(targetVesselOffset);



            // memoryBridge.SetFloat("VesselPitch", PIDpitch.CalculateResult(targetVesselOffset.x * 10, deltaTime));
        }


        //  memoryBridge.SetFloat("VesselYaw", PIDyaw.CalculateResult(targetVesselOffset.y, deltaTime));

        //var rawRoll = targetVesselOffset.x;
        //float rollError = 0;

        //if (rawRoll > 180)
        //{
        //    rollError = 360 - rawRoll;
        //}
        //else
        //{
        //    rollError = -rawRoll;
        //}



        //if (autoPilot)
        //{
        //    //Debug.Log(Quaternion.Angle(adjustedGimbal.rotation, transform.rotation));
        //    var targetAngle = Quaternion.Angle(adjustedGimbal.rotation, transform.rotation);
        //    if (targetAngle < 3 & !vesselInRange)
        //    {
        //        OnVesselEnterTargerRange();
        //    }
        //    if (targetAngle >= 3 & vesselInRange)
        //    {
        //        OnVesselExitTargetRange();
        //    }
        //}
    }