# Noob need code on 2d homing missile

This topic is 4905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know this is quite noobish..... Did you guys saw the npc homing missiles in popcap games "Heavy Weapon"? I want to make those.... I flip through gamemaker forum and found move_toward_point(x,y) in step event. But this doesn't produce a physics realistic missile with trajectories and nice curve. I flip through some physics tutorial in gamedev.net but there's a lot of square root and math to implement probably will slow the game down. I search gamedev forum and homing missile help is hard to find. Yahoo! homing missile and I got some Blitz platform code..but I need more.... Where can i find a good tutorial on homing missiles physic with practical application to make a 2d game like Heavy Weapon? Can you give me some c++ or pseudo codes?

##### Share on other sites
Its simple. Each object has a x and y (and z?) acceleration. Rather than code like this:
if(my_missile_x < my_target_x){
my_missile_x--;
}

Do this:
if(my_missile_x < my_target_x){
my_missile_x_acceleration--;
}

Every update:
Update(float milliseconds){
my_missile_x+=my_missile_x_acceleration*milliseconds*someconstant;
}

##### Share on other sites
Another approach is given by

http://mathworld.wolfram.com/PursuitCurve.html

There is more detail on this page that you are likely to want, but equation 3 sounds like the sort of thing you could use. It also shows where those square roots are probably coming from.

The gist of that page is actually pretty simple: rotate the missile to face the target. That's it.

Scott's method will probably get the missles where you want them, but unless you rotate them, they could look a little funny.

-Josh

##### Share on other sites
You can do this by getting the angle from the start and the target (Using ATAN2) and if your current angle is greater than the retuned angle, then subtract else add. Another way is to use a modified dot product(which is faster).

then it's just:

vx = cos(angle) * speed
vy = sin(angle) * speed

If you wan't to see this in action:

http://rel.betterwebber.com/index.php?action=contents&item=Space+Impakto

##### Share on other sites
Oops! I was just thinking about the realistic acceleration of the missile and not the image of it. Yes, you can use my method of adding acceleration and increasing/decreasing the angle towards the target, or you could increase/decrease the angle towards the target and add acceleration based on that angle(more complex). I'm not sure how the two methods would look differently without seeing it.

##### Share on other sites
i think a pretty common one is this. I hope you know what a vector is.

Let P0 be the missles pos and P1 be the entities pos
Let V0 be the missles vel and V1 be the entities vel

The relative position between the 2 objects is
Prel = P1-P0;
The relative velocity between the 2 objects is
Vrel = V1-V0;

therefore the time of impact is currently

toi = ||Vrel||/||Prel||

so you want to aim the missle at the player after he has moved toi time

and the find the direction and aim the missle there.

the players new pos is

Pnew = P1 + toi * VI;

Find the direction of the missle to the players new pos

Dir = (Pnew - P0)/||Pnew-P0||;

then just update the missle

P0 += Dir * Speed;

i hope i didnt screw it up i didnt have the code or anything nearby and this is off the top of my head. but it should have the missle somewhat predict the path of the player and try to go there

i am OP

##### Share on other sites
By the way, don't forget to add noise, if your program necessitates it.

You could add a percentage chance, let's say a 1% chance, that the missile will go off course. This adds a bit of realism... If you somehow want the player to outrun the missile, or you want the missile to struggle to correct itself, in the smallest fashion, then you may want to add noise.

As well, you may want it to overcorrect. As described above (I think) there's also a velocity on the horizontal axis as well as the vertical. This gives it a weight that looks more realistic. Be careful, though: give it too much juice and it will go in circles around your target, if the target has been moving but is now stationary.

Patrick

##### Share on other sites
wouldn't
toi = ||Vrel||/||Prel||
give you the inverse of the time?

I mean, should it be:
toi = ||Prel||/||Vrel||

##### Share on other sites
Quote:
 wouldn'ttoi = ||Vrel||/||Prel||give you the inverse of the time?I mean, should it be:toi = ||Prel||/||Vrel||

Correct.

Quote:
 then just update the missleP0 += Dir * Speed;

This expression also has a problem with the units. "Dir" is dimensionless and "Speed" will, I assume, have units of length per unit time, whereas P0 should have units of length. So you need to multiply this expression by a variable with units of time.

-Josh

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 11
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×