## Moving targets problem and solution

One day when I was playing Skyrim I noticed that a sorcerer would hit me with their fireballs regardless if I was moving or not and as long as I did not change my speed, trajectory or something blocked the fireball. I tried thinking what the logic and math would be in order to achieve such targeting. To know where the target will be when the projectile arrives requires knowledge of how long time it takes for the projectile to reach the target and to know where the projectile will be in at this point and to know where the target till be at this point requires knowledge of how much the target moves which sounds just like the missile knows where it isnâ€™t but I asked my dad about it and he said it was a differential equation.

Essentially the issue is to bring down the error of the whole thing so the optimal trajectory is requires a few iterations and also a bunch of data like:

- The speed of the projectile
- The speed of the target
- The position of the target relative to you
- Your own position

In a more real situation this would be harder as physics would apply but in a game where we can skip those things we do not need what would be applicable in the real world such as:

- Gravity
- Wind speed
- Wind direction
- Weight of the projectile
- Aerodynamics of the projectile
- Properties of the atmosphere or other medium such as water

There is also assumptions here such as the projectile is something that is not a missile in this example as a missile would be able to do course corrections but in this example we have a static projectile.

Below is an example of how my test looks like but you should really look at The live example and also either press `control + U`

to see the code or look at The repository

If you looked at the code you might have seen the main algorithm for targeting and also a vector type with many methods for convenience. The whole thing is built very much like a game with a tick function for a sort of game loop. I made this a few years ago so I do not know exactly all of the details of it anymore.

Solving problems like this is very interesting as it interesting to solve these kinds of problems and they are also useful to have solved in the case of game development. It can also help a bit with real world problems if you have enough date in order to properly calculate it. I have been learning a lot more math since I started to look into game dev which is something I should do way more in the future.

There are different types of this problem such as doing it in 3D and having a gravity affected projectile that you have to also calculate the angle for and you might also have to chose between a high over over 45 degrees angle which takes a longer time to reach the target from above or a lower angle that will hit the target from the side that I might also investigating some times probably using some game engine.

*Mweeoops*

By ellietheyeen 12 December 2023 Permalink Tags: html javascript math