You are viewing a single comment's thread from:
RE: Planets | A Multiplayer Planet Gravity Game
Yea, you are right, though I think that equation is wrong, it should be:
double accelX2 = -(xDist * gravityConstant)/ (squaredDistanceFromPlanet*Math.sqrt(squaredDistanceFromPlanet));
(xDist instead of yDist in this case).
I originally planned on using this equation that I got from using a program to simplify it, but yours is so much shorter and makes sense logically.
double accelX = -1/(squaredDistanceFromPlanet * Math.sqrt(1 + Math.pow(yDist, 2)/Math.pow(xDist, 2)));
The only reason I didn't use this originally was because with 0 xDist and 0 yDist, it produces NaN instead of infinity, but this could easily be fixed in code with a simple if statement. The way you describe is also better since it actually gives zero when it should be instead of floating point errors that the trig functions give you.
With yDist = 0 and xDist = 10:
-6.123233995736766E-19
-0.0
I think I'll switch to the non-trig method, thanks!