3
\$\begingroup\$

Background

I'm building a simple game where two ships are launching missiles at each other in space.

Stuff got complicated when the ships started moving in a frictionless environment and the missiles naturally needed to do the same thing.

Premises

  1. A missile has a constant acceleration and is trying to hit a moving target in a frictionless environment.
  2. Target is also constantly accelerating in the same frictionless environment.
  3. The frictionless environment is only 2 dimensional
  4. There is no gravitation affecting the missile

Question

What math should I use to determine the direction the missile should jolt in?

What i have right now

I've implemented this answer which works quite alright. I am not 100% sure of what a, b, c etc means and not sure which math is actually implemented here.

I modified above linked answer by making the missile mimmic its targets jolts and adding its own on top of those. It is an ugly solution to say the least.

Problem with current solution

The current solution requires to know the speed of the missile.

The missile is constantly accelerating, and it has no top speed.

Unless I know how far the missile will travel, I cannot know the average speed.

Above solution requires that I know the speed of the projectile.

\$\endgroup\$
  • \$\begingroup\$ If they accelerate only, you can use the equations of motion for uniform acceleration. This is right unless either the target or the missile itself are able to change their directions too. In that case, the missile should be able to adjust its direction, kind of self-guidance projectile. \$\endgroup\$ – liggiorgio Jun 3 '16 at 14:42
  • \$\begingroup\$ The formula x = xstart + vstart*t + 0.5*a*(t^2) let you calculate the x position given a starting position xstart, a starting speed vstart, and the acceleration a, while time t is the variable you increment to compute the formula. \$\endgroup\$ – liggiorgio Jun 3 '16 at 14:44
  • \$\begingroup\$ @liggiorgio I don't know t, that is the problem. \$\endgroup\$ – firelynx Jun 3 '16 at 15:06
  • \$\begingroup\$ en.wikipedia.org/wiki/Equations_of_motion#Uniform_acceleration \$\endgroup\$ – liggiorgio Jun 3 '16 at 15:24
1
\$\begingroup\$

A probing solution

I am sure there is a better solution using some clever math, but until you get that answer you can try this.

What you can calculate

You know the position, current speed and acceleration of the enemy and can calculate where it will be in the future.

You also know the position, current speed and acceleration of the missile and can calculate where it will be in the future, if you just knew the direction to go.

Iterating calculation

Now we can start to calculating the direction where to aim the missile like this:

  1. Set the first time in the future to calulate (in the graph 1 sec)
  2. Calculate the position of the enemy
  3. Aiming at the position of the enemy, calculate the position of the missile
  4. Calculate the distance of the missile and the enemy. Use the distance to check if it is near enough (using a threshold distance). Also keep this distance to compare with the next iteration (we want to know if the distance starts to get longer)
  5. If distance was close enough we have a direction and can break the iteration. If the distance gets longer then the last iteration was the best direction and we can break the iteration
  6. We haven't found the solution yet, increase the time value (+1 sec in the graph) and go back to number 2 in the list

missile and enemyship graph

This shows three iterations for the calculation of one, two and three seconds in to the future. The third one was close enough regarding the threshold in this example and the direction was selected.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.