Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This question already has an answer here:

I'm having some trouble implementing a homing missile that satisfies what I'm looking for. I have already tried the Seeking and Arrival behaviours described in this link, but none of them were what I wanted. With Seeking, it winds up orbiting the object indefinitely, and Arrival is simply unavoidable.

I'm aiming for something similar to the missiles of this video and the ones of Super Meat Boy - avoidable missiles that nevertheless do the job. It actually looks like a mix of Seek and Arrival, or maybe even the Path Following behaviour described in the first link.

This is my idealized behaviour, with a user-controlled target:

enter image description here

Can I have some insight on the matter? I know that questions regarding homing missiles have popped up quite often around here, but I feel that I'm missing something basic. The first video has an explanation from its creator in the second answer of this question, I didn't quite grasp it, though.

share|improve this question

marked as duplicate by bobobobo, bummzack, Anko, msell, John McDonald Apr 9 '13 at 16:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
*sees nice drawings* "Is it Mutoh?" Yes, it's Mutoh. :) –  Anko Mar 29 '13 at 18:17
add comment

2 Answers 2

up vote 7 down vote accepted

What you have to do is estimate the future position of the moving target and steer towards that.

Here is bobobobo's super 5 step simplified process

enter image description here

1. Initial picture of the system.


enter image description here

2. Really what we are doing here is subtracting the rocket's velocity from both bodies in the system, so the rocket is effectively "stationary", and the target is moving


enter image description here

3. vc is really the scalar speed at which the gap between the 2 bodies is closing.


enter image description here

4. Must not have a=0! If a=0, then eta=inf, which makes sense

Derivation of eta: Sorry, I lied in the image above. The eta= formula comes from:

enter image description here


enter image description here

Some C code:

Vector3f exactTrackHeading( Vector3f mePos, // your pos
      Vector3f meVel, // your speed
      Vector3f target,  // position of what you're following
      Vector3f velTarget, // velocity of your target
      float a, // your capability to accelerate.
      float &eta //, // impact time
      //Vector3f& impactPos // exactly where you'll impact (if needed)
    )
{
  if( !a ){
    error( "predictPos: accel cannot be 0" ) ;
    a=EPS_MIN;
  }
  Vector3f toFollow = target - mePos ;
  float d = toFollow.safeNormalize() ;

  // his speed relative to me
  Vector3f vTarget = velTarget - meVel ;

  // Find the component of -vTarget along toFollow
  float v = (-vTarget).dot( toFollow ) ;

  // TIME ESTIMATE TO REACH MY TARGET
  eta = -v/a + sqrtf( v*v/(a*a) + 2*d/a ) ;
  // Use ETA to estimate WHERE we should aim.

  Vector3f impactPos = target + vTarget * eta ;

  return impactPos - mePos ; // ME TO IMPACTPT is suggested heading.
  // len of this is dist to that target
}
share|improve this answer
    
Alright here's what I understood: Let the Vrt vector be the target's speed - the rocket's speed; I create an acceleration towards the target; I estimate the time it'd take to arrive at the target with the rocket's speed being -Vrt and the aforementioned acceleration; I estimate the location of the target after the elapsed time with target's speed being Vrt; I aim my acceleration towards this location. I hope I got your writing right! c: –  Mutoh Apr 9 '13 at 1:27
    
I think OP is aware of this solution; it's mentioned as the "Arrival" behaviour. –  congusbongus Apr 9 '13 at 1:28
    
Pretty much! If what you're chasing has an acceleration of it's own, you'll get this neat "miss" behavior if the target is "evasive" enough. –  bobobobo Apr 9 '13 at 2:28
add comment

Try increasing the turn speed or slowing the missile based on flight time or distance to the target. Those would help prevent orbiting. (some of the comments in the last link hinted at this and is kind of depicted in your illustration)

EDIT: could also increase turn speed based on the velocity of the missile (probably more realistic but could still end up orbiting, haven't tried to code any of this yet though)

share|improve this answer
1  
I tried all matching combinations of increasing turn speed and slowing the missile based on flight time and distance, but it still doesn't work. "[...] but I've found that if I slow the missile, if it's going to miss, as it approaches the target, it effectively tracks and turns quicker and can hit the target" I wish I actually knew how he finds out whether it's 'going to miss', I'm almost certain this is the missing puzzle piece. –  Mutoh Mar 29 '13 at 21:04
    
I usually use degrees of rotation, if you are on target, the desired direction atan2(targety-selfy,targetx-selfx)*(180/PI) is going to be the same as your velocity vector atan2(yVelocity,xVelocity)*(180/PI) I am fairly sure XNA has atan2. and usually if the difference in the angles is greater than 180 I'll add or subtract 360 from the desired direction so it doesn't try to turn the wrong way. –  UnderscoreZero Mar 29 '13 at 21:26
    
What you may need to do here is more than just seek and arrival. I tried googling it, but there is one called hunt. You would need to take your current velocity, position, and heading, and determine the desired velocity to hit the target based on its movement. I've been messing with some test code and it looks like I'm getting the problem you're having. –  UnderscoreZero Mar 29 '13 at 22:15
    
By Hunt, would you rather mean Pursuing? red3d.com/cwr/steer/PursueEvade.html –  Mutoh Mar 29 '13 at 22:21
    
Right... that would be why I wasn't finding anything on google... –  UnderscoreZero Mar 29 '13 at 22:57
show 5 more comments

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