Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It all comes back to Newton's equations:</p> <pre><code>F = m * a s = s_o + v * t + a * t^2 / 2 v = v_o + a * t </code></pre> <p>In this case <code>F</code> is the force (thrust), <code>a</code> is the acceleration, and <code>m</code> is the mass of the ship. <code>s</code> is the current location, <code>s_o</code> is the original location, <code>v</code> is the velocity, and <code>t</code> is the current time.</p> <p>Of course this is along a straight line, so if you want to convert to two or three dimensions you'll have to do some math. <code>F</code>, <code>s</code>, <code>v</code>, and <code>a</code> are all vectors, meaning that their direction is equally important. Technically <code>t</code> is also a vector, but since time generally only goes one direction, we don't have to worry about that.</p> <pre><code>2d: F^2 = F_x^2 + F_y^2 (use Pythagorean theorem to split force into components) F_x = m * a_x F_y = m * a_y s_x = s_o_x + v_x * t + a_x * t^2 / 2 s_y = s_o_y + v_y * t + a_y * t^2 / 2 v_x = v_o_x + a_x * t v_y = v_o_y + a_y * t 3d: F^2 = F_x^2 + F_y^2 + F_z^2 (surprisingly, this works) F_x = m * a_x F_y = m * a_y F_z = m * a_z s_x = s_o_x + v_x * t + a_x * t^2 / 2 s_y = s_o_y + v_y * t + a_y * t^2 / 2 s_z = s_o_z + v_z * t + a_z * t^2 / 2 v_x = v_o_x + a_x * t v_y = v_o_y + a_y * t v_z = v_o_z + a_z * t </code></pre> <p>Now to adjust your velocity to the direction of the player, you've got a fixed total force (<code>F</code>) in order to change your current velocity toward the player. In physics things don't happen instantaneously, but your goal should be to minimize the time in which the change happens ('t').</p> <p>This gives you an equation in terms of your current location (<code>(s_o_x,s_o_y)</code> or <code>(s_o_x,s_o_y,s_o_z)</code>) and your opponent's current location or your target location (<code>(s_x,s_y)</code> or <code>(s_x,s_y,s_z)</code>), for your target velocity (ignores acceleration).</p> <pre><code>v_x = (s_x - s_o_x) / t v_y = (s_y - s_o_y) / t v_x = (s_x - s_o_x) / t v_y = (s_y - s_o_y) / t v_z = (s_z - z_o_y) / t </code></pre> <p>We can substitute this for our other equation:</p> <pre><code>(s_x - s_o_x) / t = v_o_x + a_x * t (s_y - s_o_y) / t = v_o_y + a_y * t (s_x - s_o_x) / t = v_o_x + a_x * t (s_y - s_o_y) / t = v_o_y + a_y * t (s_z - z_o_y) / t = v_o_z + a_z * t </code></pre> <p>We then solve for the acceleration (this is related to the force, which is what we are trying to calculate).</p> <pre><code>(s_x - s_o_x) / t^2 - v_o_x / t = a_x (s_y - s_o_y) / t^2 - v_o_y / t = a_y (s_x - s_o_x) / t^2 - v_o_x / t = a_x (s_y - s_o_y) / t^2 - v_o_y / t = a_y (s_z - z_o_y) / t^2 - v_o_z / t = a_z </code></pre> <p>Plug this into the force equation:</p> <pre><code>F_x = m * (s_x - s_o_x) / t^2 - m * v_o_x / t F_y = m * (s_y - s_o_y) / t^2 - m * v_o_y / t F_x = m * (s_x - s_o_x) / t^2 - m * v_o_x / t F_y = m * (s_y - s_o_y) / t^2 - m * v_o_y / t F_z = m * (s_z - z_o_y) / t^2 - m * v_o_z / t </code></pre> <p>Now solve for <code>t</code>:</p> <pre><code>t = (-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x t = (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y t = (-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x t = (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y t = (-m * v_o_z +/- sqrt(m^2 * v_o_z^2 - 4 * F_z * m * (s_z - s_o_z))) / 2 / F_z </code></pre> <p>The times should converge, so the times will be equal! This gives us a system of equations for each coordinate (plane and sphere). Note that there are multiple possible values, but some involve imaginary numbers so you'll have to eliminate those solutions:</p> <pre><code>(-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x = (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y F^2 = F_x^2 + F_y^2 (-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x = (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y = (-m * v_o_z +/- sqrt(m^2 * v_o_z^2 - 4 * F_z * m * (s_z - s_o_z))) / 2 / F_z F^2 = F_x^2 + F_y^2 + F_z^2 </code></pre> <p>Solve for the <code>(F_x,F_y)</code> or <code>(F_x,F_y,F_z)</code> coordinates and you've got the force you need.</p> <p>Let me know if you have any questions or if you find mistakes in my math.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload