Esempio n. 1
0
        public void AimToTarget(Vector3 targetPosition, bool pitch = true, bool yaw = true)
        {
            if (!yawTransform)
            {
                return;
            }

            float deltaTime = Time.fixedDeltaTime;

            Vector3 localTargetYaw = yawTransform.parent.InverseTransformPoint(targetPosition - (yawTargetOffset * pitchTransform.right));
            Vector3 targetYaw      = Vector3.ProjectOnPlane(localTargetYaw, Vector3.up);
            float   targetYawAngle = VectorUtils.SignedAngle(Vector3.forward, targetYaw, Vector3.right);

            targetYawAngle = Mathf.Clamp(targetYawAngle, -yawRange / 2, yawRange / 2);

            Quaternion currYawRot = yawTransform.localRotation;

            yawTransform.localRotation = Quaternion.Euler(0, targetYawAngle, 0);
            Vector3 localTargetPitch = pitchTransform.parent.InverseTransformPoint(targetPosition - (pitchTargetOffset * pitchTransform.up));

            yawTransform.localRotation = currYawRot;
            localTargetPitch.z         = Mathf.Abs(localTargetPitch.z);    //prevents from aiming wonky if target is behind
            Vector3 targetPitch      = Vector3.ProjectOnPlane(localTargetPitch, Vector3.right);
            float   targetPitchAngle = VectorUtils.SignedAngle(Vector3.forward, targetPitch, Vector3.up);

            targetPitchAngle = Mathf.Clamp(targetPitchAngle, minPitch, maxPitch);

            float yawOffset   = Vector3.Angle(yawTransform.parent.InverseTransformDirection(yawTransform.forward), targetYaw);
            float pitchOffset = Vector3.Angle(pitchTransform.parent.InverseTransformDirection(pitchTransform.forward), targetPitch);

            float linPitchMult = yawOffset > 0 ? Mathf.Clamp01((pitchOffset / yawOffset) * (yawSpeedDPS / pitchSpeedDPS)) : 1;
            float linYawMult   = pitchOffset > 0 ? Mathf.Clamp01((yawOffset / pitchOffset) * (pitchSpeedDPS / yawSpeedDPS)) : 1;

            float yawSpeed;
            float pitchSpeed;

            if (smoothRotation)
            {
                yawSpeed   = Mathf.Clamp(yawOffset * smoothMultiplier, 1f, yawSpeedDPS) * deltaTime;
                pitchSpeed = Mathf.Clamp(pitchOffset * smoothMultiplier, 1f, pitchSpeedDPS) * deltaTime;
            }
            else
            {
                yawSpeed   = yawSpeedDPS * deltaTime;
                pitchSpeed = pitchSpeedDPS * deltaTime;
            }

            yawSpeed   *= linYawMult;
            pitchSpeed *= linPitchMult;

            if (yaw)
            {
                yawTransform.localRotation = Quaternion.RotateTowards(yawTransform.localRotation, Quaternion.Euler(0, targetYawAngle, 0), yawSpeed);
            }
            if (pitch)
            {
                pitchTransform.localRotation = Quaternion.RotateTowards(pitchTransform.localRotation, Quaternion.Euler(-targetPitchAngle, 0, 0), pitchSpeed);
            }
        }
Esempio n. 2
0
        public bool TryLockTarget(Vector3 position)
        {
            if (!canLock)
            {
                return(false);
            }
            Debug.Log("[BDArmory]: Trying to radar lock target");

            if (currentLocks == maxLocks)
            {
                Debug.Log("[BDArmory]: This radar (" + radarName + ") already has the maximum allowed targets locked.");
                return(false);
            }


            Vector3 targetPlanarDirection = Vector3.ProjectOnPlane(position - referenceTransform.position,
                                                                   referenceTransform.up);
            float angle = Vector3.Angle(targetPlanarDirection, referenceTransform.forward);

            if (referenceTransform.InverseTransformPoint(position).x < 0)
            {
                angle = -angle;
            }
            //TargetSignatureData.ResetTSDArray(ref attemptedLocks);
            RadarUtils.UpdateRadarLock(weaponManager, angle, referenceTransform, lockAttemptFOV,
                                       referenceTransform.position, minLockedSignalThreshold, ref attemptedLocks, signalPersistTime, true,
                                       rwrType, true);

            for (int i = 0; i < attemptedLocks.Length; i++)
            {
                if (attemptedLocks[i].exists && (attemptedLocks[i].predictedPosition - position).sqrMagnitude < 40 * 40)
                {
                    if (!locked && !omnidirectional)
                    {
                        float targetAngle = VectorUtils.SignedAngle(referenceTransform.forward,
                                                                    Vector3.ProjectOnPlane(attemptedLocks[i].position - referenceTransform.position,
                                                                                           referenceTransform.up), referenceTransform.right);
                        currentAngle = targetAngle;
                    }
                    lockedTargets.Add(attemptedLocks[i]);
                    currLocks = lockedTargets.Count;
                    Debug.Log("[BDArmory]: - Acquired lock on target.");
                    vesselRadarData.AddRadarContact(this, lockedTarget, true);
                    vesselRadarData.UpdateLockedTargets();
                    return(true);
                }
            }

            Debug.Log("[BDArmory]: - Failed to lock on target.");
            return(false);
        }
Esempio n. 3
0
        void Scan()
        {
            float angleDelta = scanRotationSpeed * Time.fixedDeltaTime;

            //RadarUtils.ScanInDirection(weaponManager, currentAngle, referenceTransform, angleDelta, vessel.transform.position, minSignalThreshold, ref contacts, signalPersistTime, true, rwrType, true);
            RadarUtils.UpdateRadarLock(weaponManager, currentAngle, referenceTransform, angleDelta,
                                       vessel.transform.position, minSignalThreshold, this, true, rwrType, true);

            if (omnidirectional)
            {
                currentAngle = Mathf.Repeat(currentAngle + angleDelta, 360);
            }
            else
            {
                currentAngle += radialScanDirection * angleDelta;

                if (locked)
                {
                    float targetAngle = VectorUtils.SignedAngle(referenceTransform.forward,
                                                                Vector3.ProjectOnPlane(lockedTarget.position - referenceTransform.position,
                                                                                       referenceTransform.up), referenceTransform.right);
                    leftLimit = Mathf.Clamp(targetAngle - (multiLockFOV / 2), -directionalFieldOfView / 2,
                                            directionalFieldOfView / 2);
                    rightLimit = Mathf.Clamp(targetAngle + (multiLockFOV / 2), -directionalFieldOfView / 2,
                                             directionalFieldOfView / 2);

                    if (radialScanDirection < 0 && currentAngle < leftLimit)
                    {
                        currentAngle        = leftLimit;
                        radialScanDirection = 1;
                    }
                    else if (radialScanDirection > 0 && currentAngle > rightLimit)
                    {
                        currentAngle        = rightLimit;
                        radialScanDirection = -1;
                    }
                }
                else
                {
                    if (Mathf.Abs(currentAngle) > directionalFieldOfView / 2)
                    {
                        currentAngle        = Mathf.Sign(currentAngle) * directionalFieldOfView / 2;
                        radialScanDirection = -radialScanDirection;
                    }
                }
            }
        }
        Vector2 TargetAzimuthElevationScreenPos(Rect screenRect, Vector3 targetPosition, float textureSize)
        {
            Vector3 localPos      = vessel.ReferenceTransform.InverseTransformPoint(targetPosition);
            Vector3 aziRef        = Vector3.up;
            Vector3 aziPos        = Vector3.ProjectOnPlane(localPos, Vector3.forward);
            float   elevation     = VectorUtils.SignedAngle(aziPos, localPos, Vector3.forward);
            float   normElevation = elevation / 70;


            float azimuth     = VectorUtils.SignedAngle(aziRef, aziPos, Vector3.right);
            float normAzimuth = Mathf.Clamp(azimuth / 120, -1, 1);

            float x = screenRect.x + (screenRect.width / 2) + (normAzimuth * (screenRect.width / 2)) - (textureSize / 2);
            float y = screenRect.y + (screenRect.height / 4) + (normElevation * (screenRect.height / 4)) - (textureSize / 2);

            x = Mathf.Clamp(x, textureSize / 2, screenRect.width - (textureSize / 2));
            y = Mathf.Clamp(y, textureSize / 2, (screenRect.height) - (textureSize / 2));

            return(new Vector2(x, y));
        }
Esempio n. 5
0
        void FlyToPosition(FlightCtrlState s, Vector3 targetPosition)
        {
            if (!startedLanded)
            {
                targetPosition = FlightPosition(targetPosition, minAltitude);
                targetPosition = vesselTransform.position + ((targetPosition - vesselTransform.position).normalized * 100);
            }

            Vector3d srfVel = vessel.srf_velocity;

            if (srfVel != Vector3d.zero)
            {
                velocityTransform.rotation = Quaternion.LookRotation(srfVel, -vesselTransform.forward);
            }
            velocityTransform.rotation = Quaternion.AngleAxis(90, velocityTransform.right) * velocityTransform.rotation;
            Vector3 localAngVel = vessel.angularVelocity;

            float angleToTarget = Vector3.Angle(targetPosition - vesselTransform.position, vesselTransform.up);

            if (steerMode == SteerModes.NormalFlight)
            {
                //Vector3 dampedDirection = Vector3.RotateTowards(vesselTransform.up, targetPosition - vesselTransform.position, (angleToTarget / 2) * Mathf.Deg2Rad, 0).normalized;
                //targetPosition = vesselTransform.position + (100 * dampedDirection);
            }

            if (BDArmorySettings.DRAW_DEBUG_LINES)
            {
                flyingToPosition = targetPosition;
            }

            //slow down for tighter turns
            float velAngleToTarget     = Vector3.Angle(targetPosition - vesselTransform.position, vessel.srf_velocity);
            float normVelAngleToTarget = Mathf.Clamp(velAngleToTarget, 0, 90) / 90;
            float speedReductionFactor = 1.25f;
            float finalSpeed           = Mathf.Min(speedController.targetSpeed, Mathf.Clamp(maxSpeed - (speedReductionFactor * normVelAngleToTarget), minSpeed, maxSpeed));

            debugString += "\nFinal Target Speed: " + finalSpeed.ToString("0.0");
            AdjustThrottle(finalSpeed, useBrakes, useAB);

            Vector3 targetDirection;
            Vector3 targetDirectionYaw;
            float   yawError;
            float   pitchError;
            float   postYawFactor;
            float   postPitchFactor;

            if (steerMode == SteerModes.NormalFlight)
            {
                targetDirection = velocityTransform.InverseTransformDirection(targetPosition - velocityTransform.position).normalized;
                targetDirection = Vector3.RotateTowards(Vector3.up, targetDirection, 45 * Mathf.Deg2Rad, 0);

                targetDirectionYaw = vesselTransform.InverseTransformDirection(vessel.srf_velocity).normalized;
                targetDirectionYaw = Vector3.RotateTowards(Vector3.up, targetDirectionYaw, 45 * Mathf.Deg2Rad, 0);


                postYawFactor   = 1;
                postPitchFactor = 1;

                if (command == PilotCommands.Orbit)
                {
                    postPitchFactor = 0.85f;
                }
            }
            else            //(steerMode == SteerModes.Aiming)
            {
                targetDirection    = vesselTransform.InverseTransformDirection(targetPosition - vesselTransform.position).normalized;
                targetDirection    = Vector3.RotateTowards(Vector3.up, targetDirection, 45 * Mathf.Deg2Rad, 0);
                targetDirectionYaw = targetDirection;

                if (command == PilotCommands.Follow)
                {
                    postYawFactor   = 1.3f;
                    postPitchFactor = 1f;
                }
                else
                {
                    postYawFactor   = 1.5f;
                    postPitchFactor = 2.4f;
                }
            }

            pitchError = VectorUtils.SignedAngle(Vector3.up, Vector3.ProjectOnPlane(targetDirection, Vector3.right), Vector3.back);
            yawError   = VectorUtils.SignedAngle(Vector3.up, Vector3.ProjectOnPlane(targetDirectionYaw, Vector3.forward), Vector3.right);



            float finalMaxSteer = threatLevel * maxSteer;

            float steerPitch = (postPitchFactor * 0.015f * steerMult * pitchError) - (postPitchFactor * steerDamping * -localAngVel.x);
            float steerYaw   = (postYawFactor * 0.022f * steerMult * yawError) - (postYawFactor * steerDamping * -localAngVel.z);


            s.yaw   = Mathf.Clamp(steerYaw, -finalMaxSteer, finalMaxSteer);
            s.pitch = Mathf.Clamp(steerPitch, Mathf.Min(-finalMaxSteer, -0.2f), finalMaxSteer);


            //roll
            Vector3 currentRoll = -vesselTransform.forward;
            Vector3 rollTarget;

            //if(steerMode == SteerModes.Aiming || angleToTarget > 2)
            //{
            rollTarget = (targetPosition + ((steerMode == SteerModes.Aiming ? 10f : 30f) * upDirection)) - vesselTransform.position;
            //}
            //else
            //{
            //	rollTarget = upDirection;
            //}

            if (command == PilotCommands.Follow && useRollHint)
            {
                rollTarget = -commandLeader.vessel.ReferenceTransform.forward;
            }

            rollTarget = Vector3.ProjectOnPlane(rollTarget, vesselTransform.up);



            float rollError = Misc.SignedAngle(currentRoll, rollTarget, vesselTransform.right);

            debugString += "\nRoll offset: " + rollError;
            float steerRoll = (steerMult * 0.0015f * rollError);

            debugString += "\nSteerRoll: " + steerRoll;
            float rollDamping = (.10f * steerDamping * -localAngVel.y);

            steerRoll   -= rollDamping;
            debugString += "\nRollDamping: " + rollDamping;



            float roll = Mathf.Clamp(steerRoll, -maxSteer, maxSteer);

            s.roll = roll;
            //
        }