18 const Vec3<T>& TargetPosition,
19 const Vec3<T>& TargetVelocity,
21 Vec3<T>& InterceptLocation,
25 T a = TargetVelocity.
Dot(TargetVelocity) - BulletSpeed * BulletSpeed;
26 T b = 2.0f * TargetPosition.
Dot(TargetVelocity);
27 T c = TargetPosition.
Dot(TargetPosition);
30 if ( fabs(a) < 1e-6f ) {
32 if ( fabs(b) < 1e-6f )
39 T discriminant = b * b - 4.0f * a * c;
40 if ( discriminant < 0 )
42 T sqrtD = sqrtf(discriminant);
43 T t1 = (-b + sqrtD) / (2.0f * a);
44 T t2 = (-b - sqrtD) / (2.0f * a);
47 ImpactTime = (t1 > 0 && t2 > 0) ? fminf(t1, t2) : (t1 > 0 ? t1 : t2);
51 InterceptLocation = TargetPosition + TargetVelocity * ImpactTime;
61 const T AccurateStepTime,
66 bool calculationValid =
false;
67 const auto HalfStepTime = StepTime / 2;
68 const auto HalfAccurateStepTime = AccurateStepTime / 2;
70 Vec3<T> InitialOffset = Interceptor.Position;
71 Target.Position -= InitialOffset;
73 T Velocity = Interceptor.InitialSpeed;
82 unsigned int StepsMax =
static_cast<unsigned int>(floor(MaxQueryTime / StepTime));
85 unsigned int Steps = 0;
86 auto PrevMissile = Target;
88 T PrevVelocity = Velocity;
90 for ( Steps = 1; Steps < (StepsMax + 1); Steps++ ) {
92 Target.DiscreteStep(StepTime);
95 const auto OldVel = Velocity;
96 Velocity += Interceptor.Acceleration * StepTime;
99 if ( Interceptor.AirResistance > 0 ) {
101 const auto TrapezVelocity = (OldVel + Velocity) / 2;
102 const auto drag = TrapezVelocity * TrapezVelocity * Interceptor.AirResistance * StepTime;
104 if ( Velocity <= 0 ) {
109 if ( Velocity > Interceptor.MaxSpeed && Interceptor.MaxSpeed > 0 ) {
110 Velocity = Interceptor.MaxSpeed;
114 Range += (Velocity + OldVel) * HalfStepTime;
116 if ( Target.Position.Length() <= Range ) {
117 calculationValid =
true;
122 PrevMissile = Target;
124 PrevVelocity = Velocity;
128 if ( AccurateStepTime > 0 && AccurateStepTime < StepTime ) {
130 Target = PrevMissile;
132 Velocity = PrevVelocity;
135 unsigned int AccSteps = 1;
136 for ( AccSteps = 1; AccSteps < 100000; AccSteps++ ) {
138 Target.DiscreteStep(AccurateStepTime);
141 const auto OldVel = Velocity;
142 Velocity += Interceptor.Acceleration * AccurateStepTime;
145 if ( Interceptor.AirResistance > 0 ) {
147 const auto TrapezVelocity = (OldVel + Velocity) / 2;
148 const auto drag = TrapezVelocity * TrapezVelocity * Interceptor.AirResistance * AccurateStepTime;
150 if ( Velocity <= 0 ) {
155 if ( Velocity > Interceptor.MaxSpeed && Interceptor.MaxSpeed > 0 ) {
156 Velocity = Interceptor.MaxSpeed;
160 Range += (Velocity + OldVel) * HalfAccurateStepTime;
162 if ( Target.Position.Length() <= Range ) {
163 calculationValid =
true;
168 HitTime = Steps * StepTime + AccSteps * AccurateStepTime;
171 HitTime = Steps * StepTime;
174 HitLocation = Target.Position + InitialOffset;
175 return calculationValid;
Linear and dynamic objects simulation / prediction.
Definition YasiuMathLib.h:578
bool InterceptMissile_Dynamic(ProjectileDynamicState< T > Target, const InterceptorParams< T > &Interceptor, const T MaxQueryTime, const T StepTime, Types::Vec3< T > &PredictedLocation)
Iterative state prediction. Returns first possible intercept location and time.
Definition YasiuMathLib.h:885
bool InterceptMissile_Linear(const Types::Vec3< T > &MissilePosition, const Types::Vec3< T > &MissileVelocity, const T BulletSpeed, Types::Vec3< T > &InterceptLocation, T &ImpactTime)
Linear movement with linear velocity, Fast: O(1).
constexpr double EPSILON
Value used for number stability check.
Definition Constants.h:17
Generic Types used in library.
Definition YasiuMathDataTypes.h:24
Categorized name spaces.
Definition Constants.h:10
Params used for ballistics predictions.
Definition YasiuMathLib.h:812
Ballistic state. Can move in time with DiscreteStep function.
Definition YasiuMathLib.h:587
T Dot(const Vec3 &other) const
Dot product of 2 vectors.
Definition YasiuMathDataTypes.h:274