As hinted by user concept3d, it is difficult to help you without further details about your implementation approach. I'm going to give it a shot nevertheless, but that means that I have to make some assumptions that may or may not be true for your code. In any event, I hope that the following is general enough that you can adapt it if necessary.
The first assumption I'm making is that you really only need to get the plane to the target point, and it doesn't matter to you at which angle it arrives there. My suggestion is to understand the curve the plane is supposed to take as a segment of a larger circle. The following is thus an idea how to construct that circle and how to use it to calculate a turn rate.
Let's recap the situation. You have a plane in flight, i.e., with a current direction and a current speed and you want it to reach a certain destination. Assuming we're talking about 2D, this situation essentially looks like this:
We can understand this then in terms of points and vectors like so: the plane's current position p is a (x,y)
point, its direction is given by a two-dimensional vector, its speed by a scalar (potentially encoded as the length of the direction vector, but maybe not), and the destination t is another (x, y)
point.
In this image, the current directional vector is drawn in blue, and the vector between p and t is drawn in red:
Let's call the angle between the two alpha (α). You can easily compute it using the dot product between the red and the blue vector.
Now, I want to find a circle so that both p and t lie on the circle's circumreference so that the plane can get from p to t by simply flying along the circle.
There are many different circles that have that both p and t lying on their circumreference, but they all have something in common: the distance from the circle's center point c must be the same for p as for t. This is because the distance between every point on the circumreference to the center is exactly the radius of the circle, and so it must also be for p and t.
So we know that the center point of our circle is equally far away from p and from t. But that means that it has to be somewhere on a line that is perpendicular to the red vector and intersects it halfway, like so:
But what would be a good point to pick? We know that we could theoretically use any point on that center line and draw a circle around it that would go through p and t, so which one should we use?
Well, since we want a smooth transition from the current position to the target point, we want to have a circle that won't make our plane change course abruptly when it enters the circle. At first, new direction given by the circle should at first differ only very slightly from the current direction of flight.
In order to achieve this, we are going to pick the circle for which the blue directional vector that represents the plane's current direction touches the circle tangentially in p.
But how do we construct that circle? How do we know where we have to place c on the horizontal split line? Or, equivalently, how big do we have to make the radius r? (Make sure you understand that the latter question about the radius is actually equivalent to knowing where to place c on the horizontal split line.)
First, we note that the radius r is perpendicular to the blue directional vector, i.e., the angle between the two is 90 degrees. Then we note that there's another 90 degree angle, namely between the red vector and the horizontal split line on which c lies. That means the r is the hypotenuse of a right-angled triangle.
It becomes a bit clearer if we rotate that triangle so that r is horizontal and the right angle is at the top.
So we already know two of the angles in that triangle, the right angle at the top, and the one at the corner p which is 90° - α
. Since the sum of all angles in a triangle is 180°
, we can calculate the angle at c:
right_angle + angle_at_p + angle_at_c = 180°
angle_at_c = 180° - right_angle - angle_at_p
angle_at_c = 180° - 90° - (90° - α)
angle_at_c = 90° - 90° + α
angle_at_c = α
Oh, sweet! The angle at c is actually just α
.
And we also know another thing: we know the length of the red line in the triangle. Well, not yet, but we can compute it. Remember that in the original picture the red line is the vector from current position p to the target position t. Therefore all we have to do is compute the length of that vector and divide it by two: |t - p| / 2.
Of course, this is done with the formula: sqrt((tx - px) ^ 2 + (ty - py) ^ 2) / 2
Now, with that knowledge and a little trigonometry we can easily calculate what we're actually after: the length of r. For that, we remember that sine of an angle is defined as the ratio of the length of the side that is opposite that angle to the length of the hypotenuse.
sin(α) = red_line / r
Therefore we have r = |t - p| / 2sin(α)
.
Okay. That's pretty good.
Now, the next thing we're interested in is how long the arc is that the plane has to fly from p to t if it follows the circle. That's actually not that difficult to compute. We've just seen that the angle at c in the lower triangle is equal to α
. And the upper triangle is pretty much the same as the lower triangle, just mirrored. So the angle at c in the upper triangle is also α
, which gives us a total angle of 2α
for the full arc.
As we know, the length of the full circumreference of a circle is 2πr
. But here, we don't want to fly around the full circle (2π) but only along the length of the arc (2α). Thus we get as the length to fly:
flight_length = 2αr
= 2α|t - p| / 2sin(α)
= α|t - p| / sin(α)
So with this computation, we know two things now:
- The total arc distance our plane has to fly from p to t (
= flight_length
)
- The total angle it has to turn along the way (
= 2α
)
That's pretty much all you need. The only thing that's missing is how to marry that with the plane's current speed.
Let's assume that speed is given as pixels per tick. Since we know the total flight length of the turn, we can calculate how many ticks it is going to take our plane to make this turn. Dividing the total turn angle by that number tells us how much the plane has to turn in each tick.
Thus to go from p to t in a smooth turn can be done by adjusting the plane's direction through a small rotation every tick. And with the above we know everything we have to know to compute the amount of that rotation.
As user Yos233 has already pointed out, the circle approach might give you awkward results in some situations, depending on the relative location of the current and target and position together with the current flight direction. But you can always find a way to reduce such special cases to the above method. For instance, if the target is so far away that you'd end up with a giant circle, you could add some code that uses an intermediate auxiliary target point that is much closer. This should be chosen so that the direction of the plane when it reaches the auxiliary target is so that it can simply fly straight on from there in order to reach the actual destination.
But I would suggest trying out the above method with some easy cases first and see if you like it. If you then need help with the special cases too, I suggest you simply post a new question here.