YeenDeer softness blog (programming and electronics)

Ellie the Yeen is a soft YeenDeer that mweeoops and does programming

View on GitHub About Bots Projects Tags

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:

  1. The speed of the projectile
  2. The speed of the target
  3. The position of the target relative to you
  4. 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:

  1. Gravity
  2. Wind speed
  3. Wind direction
  4. Weight of the projectile
  5. Aerodynamics of the projectile
  6. 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


A square image where the lower background is dark blue and upper background is dark green, There are 2 big squares where one is in the upper half and one in the lower, There are several small white squares too and between the 2 big squares there is 2 pink lines that leads to a third point

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


Instance: