I'm making a C program in which I simulate a Patriot missile system. In this simulation my Patriot missile has to catch an incoming enemy target missile. The information about the Patriot missile and the enemy target are stored in a structure like this:
typedef struct _stat {
float32_t x;
float32_t y;
float32_t v; // speed magnitude
float32_t v_theta; // speed angle in radians
float32_t a; // acceleration magnitude
float32_t a_theta; // acceleration angle in radians
} stat;
And I'm storing the informations in two globals variables like those:
stat t_stat; // target stats
stat p_stat; // patriot stats
Now, to simplify the problem the target is moving thanks to an initial speed and is affected only by gravity, so we can consider:
t_stat.x = TARGET_X0;
t_stat.y = TARGET_Y0;
t_stat.v = TARGET_V0;
t_stat.v_theta = TARGET_V_THETA0;
t_stat.a = G; // gravity acceleration
t_stat.a_theta = -(PI / 2);
Again, to simplify I'm also considering to compute the collision point when the Patriot has reached its top speed, so its own acceleration is only used to balance the gravity acceleration. In particular we have:
p_stat.x = PATRIOT_X0;
p_stat.y = PATRIOT_Y0;
p_stat.v = 1701,45; // Mach 5 speed in m/s
p_stat.v_theta = ???? // that's what I need to find
p_stat.a = G; // gravity acceleration
p_stat.a_theta = PI / 2;
In this way we can consider the Patriot as moving at constant speed because the sum of the accelerations by which is affected is equal to 0.
float32_t patr_ax = p_stat.a * cos(p_stat.a_theta); // = 0
float32_t patr_ay = p_stat.a * sin(p_stat.a_theta) - G; // = 0
Now, here comes the problem. I want to write a function which computes the right p_stat.v_theta
in order to hit the target (if a collision is possible).
For example the function that I need could have a prototype like this:
uint8_t computeIntercept(stat t, stat p, float32_t *theta);
And it can be used in this way:
if(computeIntercept(t_stat, p_stat, &p_stat.v_theta)) {
printf("Target Hit with an angle of %.2f\n", p_stat.v_theta);
} else {
printf("Target Missed!\n");
}
For making it even more clear, here is the image which I want