Skip to main content
added 323 characters in body
Source Link
Thomas
  • 1.2k
  • 8
  • 10

As a matter of fact, I think this factor should be what determines the maximum turn angle: obviously the greatest possible angle between the current direction of flight and the target is 180° -- so you should should pick a factor that even in that extreme case leads to a maximum turn angle that still looks believable.

There's one more thing though that we have to pay attention to: since we're using a maximum turn angle, it means that there is a "smallest circle" that our plane can fly. That is, if we wanted our plane to fly a circle, the diameter of that circle would be directly dependent on how much the plane can rotate maximally in every tick.

There's one more thing though that we have to pay attention to: since we're using a maximum turn angle, it means that there is a "smallest circle" that our plane can fly. That is, if we wanted our plane to fly a circle, the diameter of that circle would be directly dependent on how much the plane can rotate maximally in every tick.

As a matter of fact, I think this factor should be what determines the maximum turn angle: obviously the greatest possible angle between the current direction of flight and the target is 180° -- so you should should pick a factor that even in that extreme case leads to a maximum turn angle that still looks believable.

There's one more thing though that we have to pay attention to: since we're using a maximum turn angle, it means that there is a "smallest circle" that our plane can fly. That is, if we wanted our plane to fly a circle, the diameter of that circle would be directly dependent on how much the plane can rotate maximally in every tick.

deleted 1 characters in body
Source Link
Thomas
  • 1.2k
  • 8
  • 10

One easy way to do this is the following algorithm that is executed each tick:

if (the target is straight ahead) then
    keep flying straight on
else
    rotate a little bit towards the target

It's easy to see that this algorithm works... but we have to talk about what "a little bit" means. Certainly, there's a maximum angle you want your plane to turn in a single tick, or else the flight will look unnatural. Imagine the plane doing a 90° jump from one tick to the next. That would probably not look very convincing.

So you could either define a constant MAX_TURN_ANGLE which you simply find by testing what works for you; or you make the maximum turn angle a function of the plane's current speed, so that it must take wider turns when it's flying very fast. Again, you could test whatever looks best in your opinion; for now I assume that you can somehow determine the maximum angle your plane can turn.

But then we have to ask ourselves: does it always make sense to do the maximum turn possible?

I think the answer to that one is no because it won't always lead to very graceful turns. Imagine the target is 90° to the right with respect to the current flight direction, and quite a bit away. There are two extreme ways how to turn:

  1. Turn as quickly as possible and then fly the rest of the way in a straight line.
  2. Fly a very loooong arc that will make you reach the target before you fly straight again.

(The second variation is what my other answer produces.)

But perhaps the actually best way to do it is somewhere in the middle: first fly a graceful turn and then do the rest in a straight line.

Here's one idea how to implement that:

  1. Compute the angle between the current direction and a straight line to the target
  2. Have a factor between 0 and 1
  3. Multiply the angle from 1. with the factor.
  4. If the result is smaller then the maximum turn angle, use it for the turn; else use the maximum turn angle.

You could play with the factor in step 2. and see what gives good result. Maybe something around 0.755 would be a good starting point, but you could also make it a function of the distance to the target: if it's far away, use a higher value, if it is already quite close, use a smaller value.

There's one more thing though that we have to pay attention to: since we're using a maximum turn angle, it means that there is a "smallest circle" that our plane can fly. That is, if we wanted our plane to fly a circle, the diameter of that circle would be directly dependent on how much the plane can rotate maximally in every tick.

As a consequence, if the target destination lies inside that smallest circle, i.e., is closer than the diameter of the smallest circle, we can never reach it with the above method. In such a case, the best strategy is to fly in the opposite direction, i.e., away from the target. That is to as quickly as possible increase the distance between the plane and its target so that it is no longer inside the "smallest circle". Once it is outside, we will fall back to the standard method above.

One easy way to do this is the following algorithm that is executed each tick:

if (the target is straight ahead) then
    keep flying straight on
else
    rotate a little bit towards the target

It's easy to see that this algorithm works... but we have to talk about what "a little bit" means. Certainly, there's a maximum angle you want your plane to turn in a single tick, or else the flight will look unnatural. Imagine the plane doing a 90° jump from one tick to the next. That would probably not look very convincing.

So you could either define a constant MAX_TURN_ANGLE which you simply find by testing what works for you; or you make the maximum turn angle a function of the plane's current speed, so that it must take wider turns when it's flying very fast. Again, you could test whatever looks best in your opinion; for now I assume that you can somehow determine the maximum angle your plane can turn.

But then we have to ask ourselves: does it always make sense to do the maximum turn possible?

I think the answer to that one is no because it won't always lead to very graceful turns. Imagine the target is 90° to the right with respect to the current flight direction, and quite a bit away. There are two extreme ways how to turn:

  1. Turn as quickly as possible and then fly the rest of the way in a straight line.
  2. Fly a very loooong arc that will make you reach the target before you fly straight again.

(The second variation is what my other answer produces.)

But perhaps the actually best way to do it is somewhere in the middle: first fly a graceful turn and then do the rest in a straight line.

Here's one idea how to implement that:

  1. Compute the angle between the current direction and a straight line to the target
  2. Have a factor between 0 and 1
  3. Multiply the angle from 1. with the factor.
  4. If the result is smaller then the maximum turn angle, use it for the turn; else use the maximum turn angle.

You could play with the factor in step 2. and see what gives good result. Maybe something around 0.75 would be a good starting point, but you could also make it a function of the distance to the target: if it's far away, use a higher value, if it is already quite close, use a smaller value.

There's one more thing though that we have to pay attention to: since we're using a maximum turn angle, it means that there is a "smallest circle" that our plane can fly. That is, if we wanted our plane to fly a circle, the diameter of that circle would be directly dependent on how much the plane can rotate maximally in every tick.

As a consequence, if the target destination lies inside that smallest circle, i.e., is closer than the diameter of the smallest circle, we can never reach it with the above method. In such a case, the best strategy is to fly in the opposite direction, i.e., away from the target. That is to as quickly as possible increase the distance between the plane and its target so that it is no longer inside the "smallest circle". Once it is outside, we will fall back to the standard method above.

One easy way to do this is the following algorithm that is executed each tick:

if (the target is straight ahead) then
    keep flying straight on
else
    rotate a little bit towards the target

It's easy to see that this algorithm works... but we have to talk about what "a little bit" means. Certainly, there's a maximum angle you want your plane to turn in a single tick, or else the flight will look unnatural. Imagine the plane doing a 90° jump from one tick to the next. That would probably not look very convincing.

So you could either define a constant MAX_TURN_ANGLE which you simply find by testing what works for you; or you make the maximum turn angle a function of the plane's current speed, so that it must take wider turns when it's flying very fast. Again, you could test whatever looks best in your opinion; for now I assume that you can somehow determine the maximum angle your plane can turn.

But then we have to ask ourselves: does it always make sense to do the maximum turn possible?

I think the answer to that one is no because it won't always lead to very graceful turns. Imagine the target is 90° to the right with respect to the current flight direction, and quite a bit away. There are two extreme ways how to turn:

  1. Turn as quickly as possible and then fly the rest of the way in a straight line.
  2. Fly a very loooong arc that will make you reach the target before you fly straight again.

(The second variation is what my other answer produces.)

But perhaps the actually best way to do it is somewhere in the middle: first fly a graceful turn and then do the rest in a straight line.

Here's one idea how to implement that:

  1. Compute the angle between the current direction and a straight line to the target
  2. Have a factor between 0 and 1
  3. Multiply the angle from 1. with the factor.
  4. If the result is smaller then the maximum turn angle, use it for the turn; else use the maximum turn angle.

You could play with the factor in step 2. and see what gives good result. Maybe something around 0.5 would be a good starting point, but you could also make it a function of the distance to the target: if it's far away, use a higher value, if it is already quite close, use a smaller value.

There's one more thing though that we have to pay attention to: since we're using a maximum turn angle, it means that there is a "smallest circle" that our plane can fly. That is, if we wanted our plane to fly a circle, the diameter of that circle would be directly dependent on how much the plane can rotate maximally in every tick.

As a consequence, if the target destination lies inside that smallest circle, i.e., is closer than the diameter of the smallest circle, we can never reach it with the above method. In such a case, the best strategy is to fly in the opposite direction, i.e., away from the target. That is to as quickly as possible increase the distance between the plane and its target so that it is no longer inside the "smallest circle". Once it is outside, we will fall back to the standard method above.

Source Link
Thomas
  • 1.2k
  • 8
  • 10

One easy way to do this is the following algorithm that is executed each tick:

if (the target is straight ahead) then
    keep flying straight on
else
    rotate a little bit towards the target

It's easy to see that this algorithm works... but we have to talk about what "a little bit" means. Certainly, there's a maximum angle you want your plane to turn in a single tick, or else the flight will look unnatural. Imagine the plane doing a 90° jump from one tick to the next. That would probably not look very convincing.

So you could either define a constant MAX_TURN_ANGLE which you simply find by testing what works for you; or you make the maximum turn angle a function of the plane's current speed, so that it must take wider turns when it's flying very fast. Again, you could test whatever looks best in your opinion; for now I assume that you can somehow determine the maximum angle your plane can turn.

But then we have to ask ourselves: does it always make sense to do the maximum turn possible?

I think the answer to that one is no because it won't always lead to very graceful turns. Imagine the target is 90° to the right with respect to the current flight direction, and quite a bit away. There are two extreme ways how to turn:

  1. Turn as quickly as possible and then fly the rest of the way in a straight line.
  2. Fly a very loooong arc that will make you reach the target before you fly straight again.

(The second variation is what my other answer produces.)

But perhaps the actually best way to do it is somewhere in the middle: first fly a graceful turn and then do the rest in a straight line.

Here's one idea how to implement that:

  1. Compute the angle between the current direction and a straight line to the target
  2. Have a factor between 0 and 1
  3. Multiply the angle from 1. with the factor.
  4. If the result is smaller then the maximum turn angle, use it for the turn; else use the maximum turn angle.

You could play with the factor in step 2. and see what gives good result. Maybe something around 0.75 would be a good starting point, but you could also make it a function of the distance to the target: if it's far away, use a higher value, if it is already quite close, use a smaller value.

There's one more thing though that we have to pay attention to: since we're using a maximum turn angle, it means that there is a "smallest circle" that our plane can fly. That is, if we wanted our plane to fly a circle, the diameter of that circle would be directly dependent on how much the plane can rotate maximally in every tick.

As a consequence, if the target destination lies inside that smallest circle, i.e., is closer than the diameter of the smallest circle, we can never reach it with the above method. In such a case, the best strategy is to fly in the opposite direction, i.e., away from the target. That is to as quickly as possible increase the distance between the plane and its target so that it is no longer inside the "smallest circle". Once it is outside, we will fall back to the standard method above.