Background:
I am controlling a simulated (programming) missile in a 2D space (no drag). The missile knows which direction it wants to go (the intended velocity vector direction). It is always accelerating along it's axis (the direction it is pointed) with some acceleration A. The missile cannot turn off its engine. It can only apply a torque to its body to change the direction it is facing, and thus change the direction its accelerating, and through this method only, change it's velocity vector. The goal is to be flying in the intended direction. It starts out in some random orientation with some (usually small) velocity.
Goal
Have the missile traveling in the intended direction for the remainder of the flight as quickly as possible.
Mathy Recap
All in 2D space so all vectors have (x y).
Missile Acceleration A (how much the missile accelerates per second)
Missile Angular Acceleration T (The rate at which we can increase our angular velocity)
Current Direction Vector D (direction missile is facing and accelerating)
Current Velocity Vector C (direction missile is traveling)
Current Angular Velocity Y (how quickly is the missile spinning)
Intended Velocity Vector V (direction missile wants to be traveling)
Note: Application of +T or -T is the only way to control the missile.
Example Bad Solutions
Just rotate the missile so it is pointing in the right direction. This doesn't work because we will have some transverse velocity left over, and will never cancel it out.
My Almost Solution:
Just point the missile so D is equal to C mirrored across V so we cancel out the transverse velocity. Then, when C = V, just rotate back so D = V. This could work, but by the time the missile has rotated back so D = V, C has changed since we were pointing off of V while we rotated our missile back. The same peoblem will happen when we correct this new vector, and we will woble. I need a way to know when to start rotating back so that just when I reach D = V, the missiles acceleration has also made C = V.
This seems like calculus since i need to know the total change (integral) of my vector over the time it takes to rotate back. And my rotational position is an acceleration curve itself since i cant just set velocity.
Edit: A friend just informed me of PID's, which helps solve this problem, I'm not knowledgable enough to apply it. https://en.wikipedia.org/wiki/PID_controller
At any time $t$, the acceleration of the missile perpendicular to its desired direction of travel is proportional to $\sin \theta$, where $\theta$ is the angle between the vector in the desired direction and the vector of the missile's heading at time $t$.
It's best to consider $\theta$ as a signed angle, positive when the missile is pointing to one side of the desired direction and negative when the missile is pointing the other way. We can assume that we measure $\theta$ so that it is initially positive.
We're forced to endure positive $\theta$ for at least a minimum amount of time, during which we accumulate unwanted velocity perpendicular to the desired direction; that unwanted velocity is proportional to the integral of $\sin \theta$ over time. To cancel out that velocity, we need to spend some time at negative values of $\theta$, arranged so that in the end the integral of $\sin \theta$ comes to zero; that is, the "negative" part of the integral must cancel out the "positive" part.
It seems as if we should be applying torque to the missile the entire time until we finally stop at the correct heading and direction of travel. During the time we have negative $\theta$, we do not want to spend any time not applying torque, because by applying torque $T$ for half that time and $-T$ the other half, we could make the average of $\theta$ less positive during that time, and hence have less unwanted velocity that we need to cancel out. During the time we have negative $\theta$, we do not want to spend any time not applying torque, because by applying torque $T$ for half that time and $-T$ the other half (not necessarily in that order), we could make the average of $\theta$ more negative during that time, which makes the total integral more negative than we need; so by further adjusting the schedule of torque we could reduce the amount of time spent at negative $\theta$.
Intuitively, then we should apply torque $T$ for the first $\alpha$ seconds of the maneuver (starting right away) in order to start reducing $\theta$, and we should apply torque $T$ for the last $\beta$ seconds of the maneuver (during which $\theta$ is negative and increasing), because when $\theta$ finally reaches zero we want the missile to have no rotational velocity and it simply flies straight in the desired direction. In between those two intervals, we must spend $\alpha + \beta$ seconds applying torque $-T$ so that the rotational velocity of the missile actually is zero in the end.
So we have these events:
At time $t = 0$, $\theta = \theta_0$.
While $0 < t \leq \alpha$, $\theta$ is changing at the rate $\omega = -T t$ (decreasing), ending at angular velocity $\omega = -T \alpha$ and angle $$\theta_1 = \theta_0 - \frac T2 \alpha^2.$$ During this time, $$\theta = \theta_0 - \frac T2 t^2.$$
While $\alpha < t \leq 2\alpha + \beta$, $\theta$ is changing at the rate $\omega = T (t - 2\alpha)$, ending at angular velocity $\omega = T\beta$ and angle $$\theta_2 = \theta_1 - \frac T2 \alpha^2 + \frac T2 \beta^2 = \theta_0 - T \alpha^2 + \frac T2 \beta^2.$$ During this time, $$\theta = \theta_0 - T \alpha^2 + \frac T2 (t - 2\alpha)^2.$$
While $2\alpha + \beta < t \leq 2\alpha + 2\beta$, $\theta$ is changing at the rate $\omega = -T (2\alpha + 2\beta - t)$, ending at angular velocity zero and angle $$0 = \theta_2 + \frac T2 \beta^2 = \theta_0 - T \alpha^2 + T \beta^2.\tag1$$ During this time, $$\theta = \theta_0 - T \alpha^2 + T \beta^2 - \frac T2 (2\alpha + 2\beta - t)^2.$$
For any given values of $\alpha$ and $\beta$, the integral of $\sin\theta$ over the entire interval $[0,2\alpha+2\beta]$ has the form \begin{gather} \int_0^\alpha \sin\left(\theta_0 - \frac T2 t^2\right)\,dt + \int_\alpha^{2\alpha+\beta} \sin\left(\theta_0 - T \alpha^2 + \frac T2 (t - 2\alpha)^2\right)\,dt \\[.5ex] + \int_{2\alpha+\beta}^{2\alpha+2\beta} \sin\left(\theta_0 - T \alpha^2 + T \beta^2 - \frac T2 (2\alpha + 2\beta - t)^2\right)\,dt. \tag2 \end{gather}
This monster adds up to zero for an optimal maneuver. There are two unknowns, $\alpha$ and $\beta$, but you can eliminate one of them using equation $(1)$, which says that $T \alpha^2 = \theta_0 + T \beta^2.$
By appropriate substitutions and other manipulations, each of those three definite integrals can be shown equal to something of the form $$ k(\alpha) \int_{a(\alpha)}^{b(\alpha)} \sin(u^2) \,du. $$ This integral has no closed-form solution, but there is a useful power series expansion, $$ \int_0^x \sin(u^2) \,du = \sum_{n=0}^\infty \frac{(-1)^n x^{4n+3}}{(2n+1)!(4n+3)}. $$
So in principle you can expand the integral $(2)$ into at most six power series in $\alpha$, combine them, and then use numerical methods to find $\alpha$ such that the sum is zero. But it might be easier to just use Riemann sums to estimate $(2)$.