Пример #1
0
        /// <summary>
        /// Rotates down by the specified amount. "Down" is subjective; an engine nacelle might rotate horizontal, while an arm might rotate right.
        /// </summary>
        /// <param name="rotationDelta">The amount to rotate, in degrees.</param>
        public void RotateDown(float rotationDelta)
        {
            if (targetAngle - rotationDelta < 0f && minRotateAngle == 0f)
            {
                return;
            }

            //Clear any current rotations
            if (rotationState == ERotationStates.RotatingUp)
            {
                targetAngle   = currentRotationAngle;
                rotationState = ERotationStates.Locked;
            }

            targetAngle -= rotationDelta;
            if (targetAngle < 0f)
            {
                targetAngle = 360f - Mathf.Abs(targetAngle);
            }

            //Make sure the new target angle is in bounds: between minRotateAngle and maxRotateAngle.
            if (targetAngle > maxRotateAngle && targetAngle < minRotateAngle)
            {
                targetAngle = minRotateAngle;
            }

            if (currentRotationAngle == targetAngle)
            {
                return;
            }

            SetRotation(targetAngle);
        }
Пример #2
0
        public override void OnStart(StartState state)
        {
            base.OnStart(state);

            if (string.IsNullOrEmpty(rotationTransformName))
            {
                Debug.Log("No rotation transform!");
                return;
            }

            //Get the rotation target
            rotationTarget = part.FindModelTransform(rotationTransformName);

            //Get the rotation axis
            rotationAxis = new Vector3(rotateAxisX, rotateAxisY, rotateAxisZ);

            //Get the rotation state
            if (string.IsNullOrEmpty(currentRotationState) == false)
            {
                rotationState = (ERotationStates)Enum.Parse(typeof(ERotationStates), currentRotationState);
            }
            else
            {
                rotationState = ERotationStates.Neutral;
            }

            //Calculate degrees per update
            degPerUpdate = rotationDegPerSec * TimeWarp.fixedDeltaTime;
        }
Пример #3
0
        /// <summary>
        /// Rotates up by the specified amount. "Up" is subjective; an engine nacelle might rotate vertical, while an arm might rotate left.
        /// </summary>
        /// <param name="rotationDelta">The amount to rotate, in degrees.</param>
        public void RotateUp(float rotationDelta)
        {
            //Clear any current rotations
            if (rotationState == ERotationStates.RotatingDown)
            {
                targetAngle   = currentRotationAngle;
                rotationState = ERotationStates.Locked;
            }

            targetAngle += rotationDelta;
            targetAngle  = targetAngle % 360.0f;

            //Make sure the new target angle is in bounds: between minRotateAngle and maxRotateAngle.
            if (targetAngle > maxRotateAngle && targetAngle < minRotateAngle)
            {
                targetAngle = maxRotateAngle;
            }
            else if (targetAngle > maxRotateAngle && minRotateAngle == 0f)
            {
                targetAngle = maxRotateAngle;
            }

            if (currentRotationAngle == targetAngle)
            {
                return;
            }

            SetRotation(targetAngle);
        }
        public override void OnStart(StartState state)
        {
            base.OnStart(state);

            //Symmetry controls
            Events["MirrorRotation"].guiActiveEditor = canMirrorRotation;
            if (mirrorRotation)
            {
                Events["MirrorRotation"].guiName = mirrorRotationName;
            }
            else
            {
                Events["MirrorRotation"].guiName = normalRotationName;
            }

            if (string.IsNullOrEmpty(rotationMeshName))
            {
                Debug.Log("No rotation transform!");
                return;
            }

            //Get the rotation target
            rotationTarget = part.FindModelTransform(rotationMeshName);

            //Get the rotation axis
            if (string.IsNullOrEmpty(rotationMeshAxis) == false)
            {
                string[] axisValues = rotationMeshAxis.Split(',');
                float    value;
                if (axisValues.Length == 3)
                {
                    if (float.TryParse(axisValues[0], out value))
                    {
                        rotationVector.x = value;
                    }
                    if (float.TryParse(axisValues[1], out value))
                    {
                        rotationVector.y = value;
                    }
                    if (float.TryParse(axisValues[2], out value))
                    {
                        rotationVector.z = value;
                    }
                }
            }

            //Get the rotation state
            rotationState = (ERotationStates)rotationStateInt;

            //Calculate degrees per update
            degPerUpdate = rotationDegPerSec * TimeWarp.fixedDeltaTime;

            //Set initial rotation
            setInitialRotation();

            //Set gui controls
            SetGUIVisible(guiVisible);
        }
Пример #5
0
        /// <summary>
        /// Tells the servo to stop moving.
        /// </summary>
        public void StopMoving()
        {
            rotationState    = ERotationStates.Locked;
            rotationStateInt = (int)rotationState;
            state            = kLocked;

            //Update display
            currentAngleDisplay = currentRotationAngle;
        }
Пример #6
0
        public virtual void FixedUpdate()
        {
            //calculate the new rotation angle
            switch (rotationState)
            {
            case ERotationStates.RotatingUp:
                currentRotationAngle += degPerUpdate;
                currentRotationAngle  = currentRotationAngle % 360.0f;
                if (!string.IsNullOrEmpty(runningEffectName))
                {
                    this.part.Effect(runningEffectName, 1.0f);
                }
                break;

            case ERotationStates.RotatingDown:
                currentRotationAngle -= degPerUpdate;
                if (currentRotationAngle < 0f)
                {
                    currentRotationAngle = 360f - currentRotationAngle;
                }
                if (!string.IsNullOrEmpty(runningEffectName))
                {
                    this.part.Effect(runningEffectName, 1.0f);
                }
                break;

            case ERotationStates.Locked:
                currentAngleDisplay = currentRotationAngle;
                return;
            }

            //See if we've met our target
            if (currentRotationAngle > targetAngle - degPerUpdate && currentRotationAngle < targetAngle + degPerUpdate)
            {
                currentRotationAngle = targetAngle;
                rotationState        = ERotationStates.Locked;
                rotationStateInt     = (int)rotationState;
                state = kLocked;
            }

            //Update display
            currentAngleDisplay = currentRotationAngle;

            //Rotate the mesh
            if (!mirrorRotation)
            {
                rotationTarget.transform.localEulerAngles = (rotationVector * currentRotationAngle);
            }
            else
            {
                rotationTarget.transform.localEulerAngles = (rotationVector * -currentRotationAngle);
            }
        }
Пример #7
0
        public void FixedUpdate()
        {
            if (rotationState == ERotationStates.Neutral)
            {
                return;
            }

            switch (rotationState)
            {
            case ERotationStates.RotatingToNeutral:
                if (currentRotationAngle < 0)
                {
                    currentRotationAngle += degPerUpdate;
                }
                else if (currentRotationAngle > 0)
                {
                    currentRotationAngle -= degPerUpdate;
                }

                if (Math.Abs(currentRotationAngle) <= degPerUpdate)
                {
                    currentRotationAngle = 0.0f;
                    rotationState        = ERotationStates.Neutral;
                    currentRotationState = rotationState.ToString();
                }
                break;

            case ERotationStates.RotatingToMin:
                currentRotationAngle -= degPerUpdate;
                if (currentRotationAngle < minRotateAngle)
                {
                    currentRotationAngle = minRotateAngle;
                    rotationState        = ERotationStates.MinRotation;
                    currentRotationState = rotationState.ToString();
                }
                break;

            case ERotationStates.RotatingToMax:
                currentRotationAngle += degPerUpdate;
                if (currentRotationAngle > maxRotateAngle)
                {
                    currentRotationAngle = maxRotateAngle;
                    rotationState        = ERotationStates.MaxRotation;
                    currentRotationState = rotationState.ToString();
                }
                break;
            }

            rotationTarget.transform.localEulerAngles = new Vector3(currentRotationAngle, 0, 0);
        }
Пример #8
0
        /// <summary>
        /// Tells the rotator to rotate to the neutral angle. Typically this angle is 0.
        /// </summary>
        /// <param name="applyToCounterparts">True if the rotator should tell its counterparts to rotate to the neutral angle as well, false if not.</param>
        public void RotateNeutral(bool applyToCounterparts = true)
        {
            if (currentRotationAngle == 0f)
            {
                return;
            }

            //Clear any current rotations
            if (rotationState != ERotationStates.Locked)
            {
                targetAngle   = currentRotationAngle;
                rotationState = ERotationStates.Locked;
            }

            SetRotation(0f);
            if (applyToCounterparts)
            {
                updateCounterparts();
            }
        }
        public void SetRotation(float rotationAngle)
        {
            if (rotationAngle == currentRotationAngle)
            {
                return;
            }

            //Angles go from 0 to 360
            rotationAngle = rotationAngle % 360.0f;
            targetAngle   = rotationAngle;

            //If we have no min/max limits, then just find the shortest path to the target angle.
            if (minRotateAngle == -1f && maxRotateAngle == -1f)
            {
                if ((targetAngle - currentRotationAngle + 360f) % 360f <= 180f)
                {
                    rotationState = ERotationStates.RotatingUp;
                }
                else
                {
                    rotationState = ERotationStates.RotatingDown;
                }

                //Update state
                rotationStateInt = (int)rotationState;
                state            = kRotating;
                return;
            }

            //We need to figure out the shortest direction to rotate in.
            //If we have limits to our rotation, then that affects which direction we can rotate.
            //EX
            //min --------- max
            //270 --- 0 --- 90
            //If we're at 90 degrees and we want to go to 270, we have to rotate down instead of up
            //because going up would move us past our max rotation limit.

            //We're at 0, we going to 90: rotate up
            if (targetAngle > currentRotationAngle && targetAngle <= maxRotateAngle)
            {
                rotationState = ERotationStates.RotatingUp;
            }

            //We're at 0, we going to 270: rotate down
            else if (targetAngle > currentRotationAngle && targetAngle <= minRotateAngle)
            {
                rotationState = ERotationStates.RotatingDown;
            }

            else
            {
                if ((targetAngle - currentRotationAngle + 360f) % 360f <= 180f)
                {
                    rotationState = ERotationStates.RotatingUp;
                }
                else
                {
                    rotationState = ERotationStates.RotatingDown;
                }
            }

            //Update state
            rotationStateInt = (int)rotationState;
            state            = kRotating;
        }
Пример #10
0
 public void RotateToNeutral()
 {
     rotationState = ERotationStates.RotatingToNeutral;
     currentRotationState = rotationState.ToString();
 }
Пример #11
0
 public void SetRotationAngle(float angle)
 {
     rotationState        = ERotationStates.Locked;
     currentRotationAngle = angle;
     setInitialRotation();
 }
Пример #12
0
 public void RotateToMax()
 {
     rotationState        = ERotationStates.RotatingToMax;
     currentRotationState = rotationState.ToString();
 }
Пример #13
0
        public override void OnStart(StartState state)
        {
            base.OnStart(state);

            if (string.IsNullOrEmpty(rotationTransformName))
            {
                Debug.Log("No rotation transform!");
                return;
            }

            //Get the rotation target
            rotationTarget = part.FindModelTransform(rotationTransformName);

            //Get the rotation axis
            rotationAxis = new Vector3(rotateAxisX, rotateAxisY, rotateAxisZ);

            //Get the rotation state
            if (string.IsNullOrEmpty(currentRotationState) == false)
                rotationState = (ERotationStates)Enum.Parse(typeof(ERotationStates), currentRotationState);
            else
                rotationState = ERotationStates.Neutral;

            //Calculate degrees per update
            degPerUpdate = rotationDegPerSec * TimeWarp.fixedDeltaTime;
        }
Пример #14
0
 public void RotateToNeutral()
 {
     rotationState        = ERotationStates.RotatingToNeutral;
     currentRotationState = rotationState.ToString();
 }
Пример #15
0
        protected void rotatePropellersRunning()
        {
            rotationState = ERotationStates.Spinning;
            float minRatio = minThrustRotorBlur / 100.0f;

            if (enabledByRCS || isThrottleControlled)
            {
                minRatio = minThrottleBlur / 100.0f;
            }

            //If the thrust/throttle ratio is >= minimum ratio then show the blurred rotors
            float thrustThrottleRatio = getThrustThrottleRatio();

            if (thrustThrottleRatio >= minRatio || (isBlurred && isHovering))
            {
                if (!isBlurred)
                {
                    isBlurred        = true;
                    currentSpoolRate = 1.0f;
                    setupRotorTransforms();
                }

                //Spin the rotor (blades should be hidden at this point)
                float rotationPerFrame = ((rotorRPM * 60.0f) * TimeWarp.fixedDeltaTime) * blurredRotorFactor;
                currentRotationAngle += rotationPerFrame;
                currentRotationAngle  = currentRotationAngle % 360.0f;
                if (mirrorRotation)
                {
                    rotationPerFrame *= -1.0f;
                }

                rotorTransform.Rotate(rotationAxis * rotationPerFrame);

                //Now spin the blurred rotor
                rotationPerFrame = ((blurredRotorRPM * 60.0f) * TimeWarp.fixedDeltaTime);
                if (mirrorRotation)
                {
                    rotationPerFrame *= -1.0f;
                }

                if (reverseThrust)
                {
                    rotationPerFrame *= -1.0f;
                }
                blurredRotorTransform.Rotate(rotationAxis * rotationPerFrame);
            }

            //Rotate the non-blurred rotor until thrust/throttle ratio >= minRatio
            else
            {
                if (isBlurred)
                {
                    isBlurred = false;
                    setupRotorTransforms();
                }

                //Standard props will spin even when idle.
                if (!isThrottleControlled)
                {
                    currentSpoolRate = Mathf.Lerp(currentSpoolRate, 1.0f, TimeWarp.fixedDeltaTime / rotorSpoolTime);
                    if (currentSpoolRate > 0.995f)
                    {
                        currentSpoolRate = 1.0f;
                    }
                }

                //If we are throttle controlled, then spool up if current throttle >= previous throttle, and spool down if current throttle < previous throttle.
                else
                {
                    if (previousThrottle > 0 && !this.part.vessel.isActiveVessel)
                    {
                        currentSpoolRate = Mathf.Lerp(currentSpoolRate, 1.0f, TimeWarp.fixedDeltaTime / rotorSpoolTime);
                        if (currentSpoolRate > 0.995f)
                        {
                            currentSpoolRate = 1.0f;
                        }
                    }
                    else if (FlightInputHandler.state.mainThrottle >= previousThrottle && FlightInputHandler.state.mainThrottle > 0.0001f && this.part.vessel.isActiveVessel)
                    {
                        currentSpoolRate = Mathf.Lerp(currentSpoolRate, 1.0f, TimeWarp.fixedDeltaTime / rotorSpoolTime);
                        if (currentSpoolRate > 0.995f)
                        {
                            currentSpoolRate = 1.0f;
                        }
                    }
                    else
                    {
                        currentSpoolRate = Mathf.Lerp(currentSpoolRate, 0f, TimeWarp.fixedDeltaTime / rotorSpoolTime);
                        if (currentSpoolRate <= 0.002f)
                        {
                            currentSpoolRate = 0f;
                        }
                    }
                }

                float rotationPerFrame = ((rotorRPM * 60.0f) * TimeWarp.fixedDeltaTime) * currentSpoolRate;

                currentRotationAngle += rotationPerFrame;
                currentRotationAngle  = currentRotationAngle % 360.0f;
                if (mirrorRotation)
                {
                    rotationPerFrame *= -1.0f;
                }

                if (reverseThrust)
                {
                    rotationPerFrame *= -1.0f;
                }
                rotorTransform.Rotate(rotationAxis * rotationPerFrame);
            }

            //Record throttle setting
            if (this.part.vessel.isActiveVessel)
            {
                previousThrottle = FlightInputHandler.state.mainThrottle;
            }
        }
Пример #16
0
 public void RotateToMin()
 {
     rotationState = ERotationStates.RotatingToMin;
     currentRotationState = rotationState.ToString();
 }
Пример #17
0
        protected void rotatePropellersShutdown()
        {
            //If our spool rate is 0 then spin them back to the neutral position.
            //Useful for making sure our rotors are in the right position for folding.
            if (currentSpoolRate <= 0.001f)
            {
                switch (rotationState)
                {
                //Calcualte direction
                case ERotationStates.SlowingDown:
                    degPerUpdate = neutralSpinRate * TimeWarp.fixedDeltaTime;
                    if ((0f - currentRotationAngle + 360f) % 360f <= 180f)
                    {
                        rotationState = ERotationStates.RotatingUp;
                    }
                    else
                    {
                        rotationState = ERotationStates.RotatingDown;
                    }
                    break;

                //Update angle
                case ERotationStates.RotatingUp:
                    currentRotationAngle += degPerUpdate;
                    currentRotationAngle  = currentRotationAngle % 360.0f;
                    break;

                case ERotationStates.RotatingDown:
                    currentRotationAngle -= degPerUpdate;
                    if (currentRotationAngle < 0f)
                    {
                        currentRotationAngle = 360f - currentRotationAngle;
                    }
                    break;

                case ERotationStates.Locked:
                default:
                    return;
                }

                //If we're rotating, position the mesh and see if we've met our target.
                if (rotationState == ERotationStates.RotatingUp || rotationState == ERotationStates.RotatingDown)
                {
                    if (currentRotationAngle > 0f - degPerUpdate && currentRotationAngle < 0f + degPerUpdate)
                    {
                        currentRotationAngle = 0;
                        rotationState        = ERotationStates.Locked;
                    }

                    //Rotate the mesh
                    if (!mirrorRotation)
                    {
                        rotorTransform.transform.localEulerAngles = (rotationAxis * currentRotationAngle);
                    }
                    else
                    {
                        rotorTransform.transform.localEulerAngles = (rotationAxis * -currentRotationAngle);
                    }
                }

                return;
            }

            //If needed, show the non-blurred rotors
            if (isBlurred)
            {
                isBlurred = false;
                setupRotorTransforms();
            }

            //Slow down the rotors
            rotationState    = ERotationStates.SlowingDown;
            currentSpoolRate = Mathf.Lerp(currentSpoolRate, 0f, TimeWarp.fixedDeltaTime / rotorSpoolTime);
            if (currentSpoolRate <= 0.002f)
            {
                currentSpoolRate = 0f;
            }

            float rotationPerFrame = ((rotorRPM * 60.0f) * TimeWarp.fixedDeltaTime) * currentSpoolRate;

            currentRotationAngle += rotationPerFrame;
            currentRotationAngle  = currentRotationAngle % 360.0f;
            if (mirrorRotation)
            {
                rotationPerFrame *= -1.0f;
            }

            if (reverseThrust)
            {
                rotationPerFrame *= -1.0f;
            }


            rotorTransform.Rotate(rotationAxis * rotationPerFrame);
        }
Пример #18
0
        protected void rotatePropellersRunning()
        {
            rotationState = ERotationStates.Spinning;
            float minThrustRatio = minThrustRotorBlur / 100.0f;

            //If the engine thrust is >= 25% then show the blurred rotors
            float thrustRatio = engine.finalThrust / engine.maxThrust;

            if (thrustRatio >= minThrustRatio || (isBlurred && isHovering))
            {
                if (!isBlurred)
                {
                    isBlurred = true;
                    setupRotorTransforms();
                }

                //Spin the rotor (blades should be hidden at this point)
                float rotationPerFrame = ((rotorRPM * 60.0f) * TimeWarp.fixedDeltaTime) * blurredRotorFactor;
                currentRotationAngle += rotationPerFrame;
                currentRotationAngle  = currentRotationAngle % 360.0f;
                if (mirrorRotation)
                {
                    rotationPerFrame *= -1.0f;
                }

                rotorTransform.Rotate(rotationAxis * rotationPerFrame);

                //Now spin the blurred rotor
                rotationPerFrame = ((blurredRotorRPM * 60.0f) * TimeWarp.fixedDeltaTime);
                if (mirrorRotation)
                {
                    rotationPerFrame *= -1.0f;
                }

                if (reverseThrust)
                {
                    rotationPerFrame *= -1.0f;
                }
                blurredRotorTransform.Rotate(rotationAxis * rotationPerFrame);
            }

            //Rotate the non-blurred rotor until thrust % >= minThrustRotorBlur
            else
            {
                if (isBlurred)
                {
                    isBlurred = false;
                    setupRotorTransforms();
                }

                currentSpoolRate = Mathf.Lerp(currentSpoolRate, 1.0f, TimeWarp.fixedDeltaTime / rotorSpoolTime);
                if (currentSpoolRate > 0.995f)
                {
                    currentSpoolRate = 1.0f;
                }

                float rotationPerFrame = ((rotorRPM * 60.0f) * TimeWarp.fixedDeltaTime) * currentSpoolRate;
                currentRotationAngle += rotationPerFrame;
                currentRotationAngle  = currentRotationAngle % 360.0f;
                if (mirrorRotation)
                {
                    rotationPerFrame *= -1.0f;
                }


                if (reverseThrust)
                {
                    rotationPerFrame *= -1.0f;
                }
                rotorTransform.Rotate(rotationAxis * rotationPerFrame);
            }
        }
Пример #19
0
        public void FixedUpdate()
        {
            if (rotationState == ERotationStates.Neutral)
                return;

            switch (rotationState)
            {
                case ERotationStates.RotatingToNeutral:
                    if (currentRotationAngle < 0)
                        currentRotationAngle += degPerUpdate;
                    else if (currentRotationAngle > 0)
                        currentRotationAngle -= degPerUpdate;

                    if (Math.Abs(currentRotationAngle) <= degPerUpdate)
                    {
                        currentRotationAngle = 0.0f;
                        rotationState = ERotationStates.Neutral;
                        currentRotationState = rotationState.ToString();
                    }
                    break;

                case ERotationStates.RotatingToMin:
                    currentRotationAngle -= degPerUpdate;
                    if (currentRotationAngle < minRotateAngle)
                    {
                        currentRotationAngle = minRotateAngle;
                        rotationState = ERotationStates.MinRotation;
                        currentRotationState = rotationState.ToString();
                    }
                    break;

                case ERotationStates.RotatingToMax:
                    currentRotationAngle += degPerUpdate;
                    if (currentRotationAngle > maxRotateAngle)
                    {
                        currentRotationAngle = maxRotateAngle;
                        rotationState = ERotationStates.MaxRotation;
                        currentRotationState = rotationState.ToString();
                    }
                    break;
            }

            rotationTarget.transform.localEulerAngles = new Vector3(currentRotationAngle, 0, 0);
        }