gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Homing Algorithm

#4118 - NovusAmor - Wed Mar 19, 2003 5:36 pm

Does anyone know of a good homing (like a targeting missile) algorithm that runs fast in 2d? I'm currently using a vector/normalized solution but it isn't as smooth as I'd like. Also I'm not looking for:

if(objx > targetx) objx--;
else if(objx < targetx) objx++;

That is for newbies that don't know how to code.

Thanks for any help.
_________________
Robert
Senior Programmer
NeoPong Software, Inc
www.neopong.com

#4123 - col - Wed Mar 19, 2003 9:08 pm

x0, y0 is the missiles last position
x1, y1 is the missiles current position
xt, yt is the targets position
angle is the missiles current direction

z=(yt-y0)*(x1-x0)-(xt-x0)*(y1-y0);

if z<0
turn left
else
turn right

(I might have the turn right/left back to front? i'm too tired to think :) )

The only problem with this method is if the missile is heading exactly away from the target, but it's not too tricky to fix that...

cheers

Colin