There is a faster way...
> // calculate distance from the sun
> xLen = sunPos.x - objPos.x;
> yLen = sunPos.y - objPos.y;
> lengthsqr = (xLen * xLen) + (yLen * yLen);
> length = sqrt(lengthsqr);
> // calculate unit vector
> unitX = xLen / length;
> unitY = yLen / length;
> // gravity is inverse r squared
> unitX = unitX/lengthsqr;
> unitY = unitY/lengthsqr;
> // update object speed
> speed.x += unitX;
> speed.y += unitY;
Compute: D = ((xLen * xLen) + (yLen * yLen)) ^ -1.5
dx = xLen * D
dy = yLen * D
speed.x += dx
speed.y += dy
The -1.5 exponent seems strange, but you'll note that there is no need
to compute the unit vector this way. Whatever iterative method you use
for square root may apply to this as well. I presented this to the Xpilot
team several years back, and they got a good speed boost from it - that
game can have a LOT of gravity sources.
BTW, the "obvious" way to extend to 3D works, as that is where it came
from (take the gradient of the gravitational potential field (-1/r) in
cartesian coordinates). Hope it helps.
-- ___ __ _ _ _ | \ / \ | | | || | phkahler@oakland.edu Engineer/Programmer | _/| || || |_| || |__ " What makes someone care so much? |_| |_||_| \___/ |____) for things another man can just ignore. " -S.H.Received on Mon Nov 1 09:34:13 1999
This archive was generated by hypermail 2.1.8 : Fri Aug 01 2003 - 00:32:26 EDT