title: How do I predict the future position of a moving object?
body: <p>I have asked this question previously and it was closed due to it not being a programming question, this is entirely as I worded it wrong. I would like this to implemented into java. I am creating a little game and I have a photon torpedo which is being fired from a ship towards a target. Now as the speed of the torpedo is slow the ship will never hit any targets if they're moving and I want to fix this. I have drew up multiple theories and mapped out lots of mathematical stuff to find out the best way to accomplish this and in the end I deduced the following:</p>
  
  <ol>
  <li>I find the time it takes for the photon torpedo to get to the target.</li>
  <li>I find how far the target will have traveled in the time it takes for the torpedo to arrive.</li>
  <li>I then find the distance between the new position of the target and the original ship.</li>
  <li>this then gives me the opportunity to use the Cosine rule (SSS) to find out the trajectory at which the bullet needs to be fired to have a much higher chance of hitting.</li>
  </ol>
  
  <p>Here is a digram:</p>
  
  <p><img src="https://i.stack.imgur.com/LxiFC.png" alt="A diagram of my problem"></p>
  
  <p>Now the only problem that I need to rotate line a to the correct orientation as by default it's parallel to line c which messes up the entire equation. Can anyone help with this? And also if you can think of a better way to find the new position suggestions are very welcome. My java game entity mechanic works as follows:</p>
  
  <ol start="2">
  <li>Each entity has two Vectors which control movement. Position and Velocity. However, velocity is not tracked entirely properly as instead of it being a speed and a direction, to make things easier it's an xSpeed and a ySpeed.</li>
  <li>The entities are all updates once per tick and the ship which shoots the torpedo must calculate the future position in this one tick and not over multiple ticks.</li>
  </ol>
  
  <p>I ask this question not to be closed again, because this time I really need the answer to be implemented into Java.</p>
  
  <p>This is the math i've tried so far:</p>
  
  <pre><code>        double dis = level.distanceBetween(photonTargetTop, this);
  
          double speed = 5;
  
          double time = dis / speed;
  
          double d1 = photonTargetTop.velocity.x * time;
          double d2 = photonTargetTop.velocity.y * time;
  
          double dis2 = level.distanceBetween(this, photonTargetTop.pos.x + d1, photonTargetTop.pos.y + d2);
  
          double dis3 = level.distanceBetween(photonTargetTop, photonTargetTop.pos.x + d1, photonTargetTop.pos.y + d2);
  
          double cosAngle = Math.pow(dis2, 2) + Math.pow(dis, 2) - Math.pow(dis3, 2) / 2 * dis2 * dis;
  
          double angle = Math.acos(cosAngle);
  
          EntityPhoton p = new EntityPhoton(this, level);
          p.rotation = angle;
          level.addEntity(p, pos);
  </code></pre>