Sunday, August 16, 2015

xna - Implementing a homing missile



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.



Answer



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

}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...