For aiming, I use a formula I'd like to remember where I got it from (sorry). It's FAR (and I mean FAR) from perfect, but it seems to get the job done, at least in the instances I've used it:
Code:
dx = DELTA (x0, x1);
dy = DELTA (y0, y1);
aux = MIN (dx, dy);
distance = dx + dy - (aux >> 1) - (aux >> 2) + (aux >> 4);
aux = (V * dx / distance); vx = ADD_SIGN (x1, x0, aux);
aux = (V * dy / distance); vy = ADD_SIGN (y1, y0, aux);
Macros are roughly as follows:
Code:
#define MIN(a,b) ((a)<(b)?(a):(b))
#define DELTA(a,b) ((a)<(b)?(b)-(a):(a)-(b))
#define ADD_SIGN(a,b,v) ((a)==(b))?(0):(((a)>(b))?(v):(-(v)))
All are signed fixed point variables.