| | 1 | | using UnityEngine; |
| | 2 | |
|
| | 3 | | // The iterative launch planner class is a launch planner that performs an iterative process to |
| | 4 | | // determine the intercept point. The algorithm continuously performs the following 2-step iterative |
| | 5 | | // process: |
| | 6 | | // 1. Time-to-intercept estimation: The algorithm determines the time it takes the interceptor to |
| | 7 | | // reach the target at the predicted intercept position, which is initialized to the target's |
| | 8 | | // current position. |
| | 9 | | // 2. Intercept position estimation: The algorithm predicts the target position at the estimated |
| | 10 | | // time-to-intercept. |
| | 11 | | public class IterativeLaunchPlanner : ILaunchPlanner { |
| | 12 | | // Maximum number of iterations before declaring failure. In certain cases, the predictor and |
| | 13 | | // planner do not converge, so we need to limit the number of iterations. |
| | 14 | | private const int MaxNumIterations = 10; |
| | 15 | |
|
| | 16 | | // Convergence threshold in meters for the difference vector magnitude. Convergence is declared |
| | 17 | | // when the intercept position has not changed by more than this threshold between iterations. |
| | 18 | | private const float ConvergenceThreshold = 10f; |
| | 19 | |
|
| | 20 | | // Maximum intercept position threshold in meters to declare convergence. This threshold is used |
| | 21 | | // as a final sanity check to ensure that the predicted target position and intercept position do |
| | 22 | | // not differ by more than this threshold. This threshold should be set depending on the |
| | 23 | | // granularity of the possible intercept positions. |
| | 24 | | private const float InterceptPositionThreshold = 1000f; |
| | 25 | |
|
| | 26 | | public IterativeLaunchPlanner(ILaunchAnglePlanner launchAnglePlanner, IPredictor predictor) |
| 0 | 27 | | : base(launchAnglePlanner, predictor) {} |
| | 28 | |
|
| | 29 | | // Plan the launch. |
| 0 | 30 | | public override LaunchPlan Plan() { |
| 0 | 31 | | return PlanFromZeroOrigin(); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | // Plan the launch from a specific interceptor origin. |
| | 35 | | // This implementation accounts for the interceptor's starting position and the origin's |
| | 36 | | // current location (including movement for naval assets). |
| | 37 | | // origin: Interceptor origin object |
| | 38 | | // Returns: Launch plan with timing and angle information |
| 0 | 39 | | public override LaunchPlan Plan(InterceptorOriginObject origin) { |
| | 40 | | // Get the current origin position (accounts for moving origins) |
| 0 | 41 | | Vector3 originPosition = origin.GetPosition(); |
| 0 | 42 | | return PlanFromOrigin(originPosition); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | // Original implementation for zero origin (0,0,0). |
| | 46 | | // Preserved for backward compatibility with existing tests. |
| | 47 | | // Returns: Launch plan with timing and angle information |
| 0 | 48 | | private LaunchPlan PlanFromZeroOrigin() { |
| 0 | 49 | | PredictorState initialState = _predictor.Predict(time: 0); |
| 0 | 50 | | Vector3 targetPosition = initialState.Position; |
| | 51 | |
|
| 0 | 52 | | LaunchAngleOutput launchAngleOutput = new LaunchAngleOutput(); |
| 0 | 53 | | Vector3 interceptPosition = new Vector3(); |
| 0 | 54 | | for (int i = 0; i < MaxNumIterations; ++i) { |
| | 55 | | // Estimate the time-to-intercept from the current origin position |
| 0 | 56 | | launchAngleOutput = _launchAnglePlanner.Plan(targetPosition); |
| 0 | 57 | | float timeToIntercept = launchAngleOutput.TimeToPosition; |
| | 58 | |
|
| | 59 | | // Estimate the target position at intercept time |
| 0 | 60 | | PredictorState predictedState = _predictor.Predict(timeToIntercept); |
| 0 | 61 | | targetPosition = predictedState.Position; |
| | 62 | |
|
| | 63 | | // Check whether the intercept position has converged |
| 0 | 64 | | Vector3 newInterceptPosition = _launchAnglePlanner.GetInterceptPosition(targetPosition); |
| | 65 | |
|
| 0 | 66 | | if ((interceptPosition - newInterceptPosition).magnitude < ConvergenceThreshold) { |
| 0 | 67 | | interceptPosition = newInterceptPosition; |
| 0 | 68 | | break; |
| | 69 | | } |
| 0 | 70 | | interceptPosition = newInterceptPosition; |
| | 71 | |
|
| | 72 | | // Check that the target is moving towards the intercept position relative to the threat's |
| | 73 | | // initial position. This prevents launching when the threat is moving away from the predicted |
| | 74 | | // intercept. |
| 0 | 75 | | Vector3 targetToInterceptPosition = interceptPosition - initialState.Position; |
| 0 | 76 | | Vector3 targetToPredictedPosition = targetPosition - initialState.Position; |
| 0 | 77 | | if (Vector3.Dot(targetToInterceptPosition, targetToPredictedPosition) < 0) { |
| 0 | 78 | | return LaunchPlan.NoLaunch; |
| | 79 | | } |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | // Check for backwards/sideways launch scenarios using proper geometric analysis. |
| 0 | 83 | | if (IsInvalidLaunchGeometry(Vector3.zero, interceptPosition, initialState)) { |
| 0 | 84 | | return LaunchPlan.NoLaunch; |
| | 85 | | } |
| | 86 | |
|
| | 87 | | // Final validation: ensure intercept and predicted positions are reasonably close |
| 0 | 88 | | if (Vector3.Distance(interceptPosition, targetPosition) < InterceptPositionThreshold) { |
| 0 | 89 | | return new LaunchPlan(launchAngleOutput.LaunchAngle, interceptPosition); |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | return LaunchPlan.NoLaunch; |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | // Origin-aware implementation for non-zero origins. |
| | 96 | | // This implementation properly accounts for interceptor starting position. |
| | 97 | | // originPosition: The position from which the interceptor will be launched |
| | 98 | | // Returns: Launch plan with timing and angle information |
| 0 | 99 | | private LaunchPlan PlanFromOrigin(Vector3 originPosition) { |
| 0 | 100 | | PredictorState initialState = _predictor.Predict(time: 0); |
| 0 | 101 | | Vector3 targetPosition = initialState.Position; |
| | 102 | |
|
| 0 | 103 | | LaunchAngleOutput launchAngleOutput = new LaunchAngleOutput(); |
| 0 | 104 | | Vector3 interceptPosition = new Vector3(); |
| | 105 | |
|
| 0 | 106 | | for (int i = 0; i < MaxNumIterations; ++i) { |
| | 107 | | // Estimate the time-to-intercept from the current origin position |
| 0 | 108 | | launchAngleOutput = _launchAnglePlanner.Plan(targetPosition, originPosition); |
| 0 | 109 | | float timeToIntercept = launchAngleOutput.TimeToPosition; |
| | 110 | |
|
| | 111 | | // Estimate the target position at intercept time |
| 0 | 112 | | PredictorState predictedState = _predictor.Predict(timeToIntercept); |
| 0 | 113 | | targetPosition = predictedState.Position; |
| | 114 | |
|
| | 115 | | // Check whether the intercept position has converged |
| 0 | 116 | | Vector3 newInterceptPosition = |
| | 117 | | _launchAnglePlanner.GetInterceptPosition(targetPosition, originPosition); |
| 0 | 118 | | if ((interceptPosition - newInterceptPosition).magnitude < ConvergenceThreshold) { |
| 0 | 119 | | interceptPosition = newInterceptPosition; |
| 0 | 120 | | break; |
| | 121 | | } |
| 0 | 122 | | interceptPosition = newInterceptPosition; |
| | 123 | |
|
| | 124 | | // Check that the target is moving towards the intercept position relative to the threat's |
| | 125 | | // initial position. This prevents launching when the threat is moving away from the predicted |
| | 126 | | // intercept. |
| 0 | 127 | | Vector3 targetToInterceptPosition = interceptPosition - initialState.Position; |
| 0 | 128 | | Vector3 targetToPredictedPosition = targetPosition - initialState.Position; |
| 0 | 129 | | if (Vector3.Dot(targetToInterceptPosition, targetToPredictedPosition) < 0) { |
| 0 | 130 | | return LaunchPlan.NoLaunch; |
| | 131 | | } |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | // Check for backwards/sideways launch scenarios using proper geometric analysis. |
| 0 | 135 | | if (IsInvalidLaunchGeometry(originPosition, interceptPosition, initialState)) { |
| 0 | 136 | | return LaunchPlan.NoLaunch; |
| | 137 | | } |
| | 138 | |
|
| | 139 | | // Final validation: ensure intercept and predicted positions are reasonably close |
| 0 | 140 | | if (Vector3.Distance(interceptPosition, targetPosition) < InterceptPositionThreshold) { |
| 0 | 141 | | return new LaunchPlan(launchAngleOutput.LaunchAngle, interceptPosition); |
| | 142 | | } |
| | 143 | |
|
| 0 | 144 | | return LaunchPlan.NoLaunch; |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | // Determines if a launch scenario is geometrically invalid (e.g., backwards or sideways). |
| | 148 | | // originPosition: Position from which the interceptor will be launched. |
| | 149 | | // interceptPosition: Calculated intercept position. |
| | 150 | | // threatState: Initial state of the threat (position and velocity). |
| | 151 | | // Returns: True if the launch geometry is invalid and should be prevented. |
| | 152 | | private bool IsInvalidLaunchGeometry(Vector3 originPosition, Vector3 interceptPosition, |
| 0 | 153 | | PredictorState threatState) { |
| 0 | 154 | | Vector3 originToThreat = threatState.Position - originPosition; |
| 0 | 155 | | Vector3 threatVelocity = threatState.Velocity; |
| | 156 | |
|
| | 157 | | // A launch is invalid if the threat is moving away from the origin. |
| | 158 | | // A dot product > 0 means the angle between the vector from the origin to the threat |
| | 159 | | // and its velocity is < 90 degrees, indicating it's moving away. |
| 0 | 160 | | if (Vector3.Dot(originToThreat, threatVelocity) > 0.0f) { |
| 0 | 161 | | return true; |
| | 162 | | } |
| | 163 | |
|
| | 164 | | // A launch is also invalid if the intercept point is "behind" the origin |
| | 165 | | // relative to the threat's direction of approach. "Behind" means the angle |
| | 166 | | // between the vector to the threat and the vector to the intercept point is > 90 degrees. |
| 0 | 167 | | Vector3 originToIntercept = interceptPosition - originPosition; |
| 0 | 168 | | if (Vector3.Dot(originToIntercept.normalized, originToThreat.normalized) < 0.0f) { |
| 0 | 169 | | return true; |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | return false; |
| 0 | 173 | | } |
| | 174 | | } |