creating simple homing missile?

we’ll i’m trying to make a simple homing missle, but am having trouble, not quiet sure where to start, basically at the moment when the enemy ship fires a missile it just goes straight, ie, just +1 from its x co-ordinates every frame.

i have the goody ship at (x, y) coordinates, and when the enemy fires an missiles, i want it to head towards the goody ship and off the screen not continuasly head towards the goody ship.

any ideas on how i would implement this? ??? ??? ???

Are we talking about a top-down horizontal scrolling shooter here? Like Xenon 2 or 1942?

If so, each movement calculate:

int verticalUpdate = goodyship.y - missle.y;

then update your missile by:

missle.move(1,verticalUpdate)

You might want to change it to support speed of movement and such.

Kev

thx for the reply its 1942 style, i’ll try that.

Erg, having re-read what I posted I note that would make your missle jump to the same location as the goody ship each frame.

You’ll need to add a scaling factor of some sort.

Kev

scaling factor?, err ok i’ll try!


// find center of player ship
            shpX = Ship.shipR.x + (Ship.shipR.width/2);
            shpY = Ship.shipR.y;
            // find x and y distance from enemy "laser" or missle to player ship
            xD = shpX - laserR.x;
            yD = shpY - laserR.y;
            distance = Math.sqrt((yD*yD) + (xD+xD));
            ratioToDistance = 4/distance;
            // calculate the speed for x and speed for y so it will be shot at the player.
            ySpeed = (int) (ratioToDistance * yD);
            xSpeed = (int) (ratioToDistance * xD);


heres some code i made for a homing missle… maybe not the best, but it works. check it out at http://xaptronic.ionichost.com and goto space feud.
ask for more source if ud like…

This is best done with a simple feedback control system. I’m not suggesting you start doing Laplace transforms etc… but you could actually solve it that way.

Really it comes down to calculating how much the missile should turn to head straight at the target and adjusting the heading accordingly.

In Psuedocode:


final double f = 0.1;  // tracking factor adjust to get the feel you want
double oldHeading = heading;
heading = (1-f) * oldHeading + f * towardsTarget;

x += Math.cos(heading) * stepSize;
y += Math.sin(heading) * stepSize;

For smooth motion you have to keep something in mind. When the angle between a missle and a target changes, the missle’s trajectory is not immediately set to the new angle. Rather it bends gradually from the old trajectory to the new one. So instead of setting a new angle variable you should increment or decrement the old one in relation to the new one.

That’s exactly what my code does… not the increment/decrement part… the gradual part.

You have to scale the feedback so that you get a realistic response.

That’s the ‘f’ in:

heading = (1-f) * oldHeading + f * towardsTarget;

only allow it to turn a small percentage of the way to the new heading in any one step.

Add in a small chance that it doesn’t correct its path - otherwise the player won’t have a decent oppertunity to dodge. If this seems cheesy, keep in mind how many real military munitions don’t function quite right …

That shouldn’t be a problem… just scale it so the missile can’t turn nearly as fast as the player… Then the player just has to quickly duck behind something to make the missile crash into whatever he is hiding behind.

A friend of mine wrote a Quake mode for homing rockets that worked like this and it was great.