How do I determine the future position of a moving object?

I create a small game, and I have a photon torpedo that is fired from the ship toward the target. Now that the torpedo’s speed is slow, the ship will never hit targets if they move, and I want to fix it. I developed many theories and outlined many mathematical materials to figure out how to do this, and in the end I deduced the following:

  • List item
  • I find the time necessary for the torpedo of photons to hit the target.
  • I find how far the target will travel in the time it takes to get a torpedo.
  • Then I find the distance between the new target position and the original ship.
  • this gives me the opportunity to use the cosine rule (SSS) to find out the trajectory in which the bullet must be fired in order to have a much greater chance of striking.

Here is the digram:

A diagram of my problem

Now the only problem I need is to rotate the line a in the correct orientation, since by default it is parallel to the line c, which messed up the whole equation. Can anyone help with this? And also, if you can think about how best to find new offers on positions, we are very welcome :)

+4
source share
1 answer

It looks like a game in 2D. Now, taking all the world coordinates:

Target Position: {xt, yt} Target Velocity: {vxt, vyt} Ship Position: {xs, ys} 

Now, let's say you hit a bullet in the direction of {bx, by} , this is a direction vector or a unit vector. The bullet has an sb speed. Therefore, in time t' spatial coordinates of the bullet and the target will be:

 Target New Position: {xt + vxt*t', yt + vyt*t'} Bullet New Position: {xs + bx*sb*t', ys + by*sb*t'} ( bullet starts from ship) 

Since at this time both the bullet and the target should be in the same place, then we can say that the bullet hit the target. Therefore, we can replace the new target position with the new location of the bullet (after time t '):

 xt + vxt*t' = xs + bx*sb*t' yt + vyt*t' = ys + by*sb*t' 

As I said earlier, bx and by are directivity vectors, so bx^2 + by^2 = 1

 (xt + vxt*t' - xs)/(sb*t') = bx (yt + vyt*t' - ys)/(sb*t') = by bx^2 + by^2 = 1 

Therefore, after squaring and adding them, you will receive:

 (yt + vyt*t' - ys)^2 + (xt + vxt*t' - xs)^2 = (sb*t')^2 

This is a quadratic equation with one variable: t' solve it, and then you can find bx and by .

+2
source

All Articles