All pastes #637677 Raw Edit

Something

public text v1 · immutable
#637677 ·published 2007-07-29 03:34 UTC
rendered paste body

//
// Predicts a direction to fire a projectile at-
// \param mp: my position
// \param md: my velocity
// \param hp: his position
// \param hd: his velocity
// \param bs: speed of bullet

vector3df predictDirection( const vector3df &mp, const vector3df &md, const vector3df &hp, const vector3df &hd, const f32 bs) 
{

	// first work out time to intercept

	f32 	A = hp.X - mp.X,
		B = hd.X*hd.X - hd.X*md.X*2 + md.X*md.X,
		C = hd.X*A - md.X*A*2 + hd.X*A,
		D = hp.Y - mp.Y,
		E = hd.Y*hd.Y - hd.Y*md.Y*2 + md.Y*md.Y,
		F = hd.Y*A - md.Y*A*2 + hd.Y*A,
		G = hp.Z - mp.Z,
		H = hd.Z*hd.Z - hd.Z*md.Z*2 + md.Z*md.Z,
		I = hd.Z*A - md.Z*A*2 + hd.Z*A,
		J = bs*bs*bs;

	f32 r = (C+F+I-J)*(C+F+I-J) - 4*(A*A + D*D + G*G)*(B+H+E);

	f32 T = ( -C -F -I +J - sqrt(r) ) / (2*(B+H+E) );


	// now we can work out bullet direction...
	return (hp + hd*t - mp - md*t).normalize();
}