Beispiel #1
0
 /// <summary>
 /// Predict the next time to the closest point of approach within the next maxTime seconds using the same kinematics as PredictPosition (i.e, position, velocity and acceleration).
 /// </summary>
 /// <param name="vessel">The first vessel.</param>
 /// <param name="v">The second vessel.</param>
 /// <param name="maxTime">The maximum time to look ahead.</param>
 /// <returns>float The time to the closest point of approach within the next maxTime seconds.</returns>
 public static float ClosestTimeToCPA(this Vessel vessel, Vessel v, float maxTime)
 { // Find the closest future time to closest point of approach considering accelerations in addition to velocities. This uses the generalisation of Cardano's solution to finding roots of cubics to find where the derivative of the separation is a minimum.
     if (vessel == null)
     {
         return(0f);                // We don't have a vessel.
     }
     if (v == null)
     {
         return(0f);           // We don't have a target.
     }
     return(vessel.ClosestTimeToCPA(v.transform.position, v.Velocity(), v.acceleration, maxTime));
 }
Beispiel #2
0
        public static float PredictClosestApproachSqrSeparation(this Vessel vessel, Vessel otherVessel, float maxTime)
        {
            var timeToCPA = vessel.ClosestTimeToCPA(otherVessel, maxTime);

            if (timeToCPA > 0 && timeToCPA < maxTime)
            {
                return((vessel.PredictPosition(timeToCPA) - otherVessel.PredictPosition(timeToCPA)).sqrMagnitude);
            }
            else
            {
                return(float.MaxValue);
            }
        }
        public static Vector3 GetAirToAirTargetModular(Vector3 targetPosition, Vector3 targetVelocity, Vector3 targetAcceleration, Vessel missileVessel, out float timeToImpact)
        {
            float targetDistance = Vector3.Distance(targetPosition, missileVessel.CoM);

            //Basic lead time calculation
            Vector3 currVel = ((float)missileVessel.srfSpeed * missileVessel.Velocity().normalized);

            timeToImpact = (float)(1 / ((targetVelocity - currVel).magnitude / targetDistance));

            // Calculate time to CPA to determine target position
            float timeToCPA = missileVessel.ClosestTimeToCPA(targetPosition, targetVelocity, targetAcceleration, 16f);

            timeToImpact = (timeToCPA < 16f) ? timeToCPA : timeToImpact;
            // Ease in velocity from 16s to 8s, ease in acceleration from 8s to 2s using the logistic function to give smooth adjustments to target point.
            float easeAccel = Mathf.Clamp01(1.1f / (1f + Mathf.Exp((timeToCPA - 5f))) - 0.05f);
            float easeVel   = Mathf.Clamp01(2f - timeToCPA / 8f);

            return(AIUtils.PredictPosition(targetPosition, targetVelocity * easeVel, targetAcceleration * easeAccel, timeToCPA));
        }