You are viewing a single comment's thread from:

RE: Planets | A Multiplayer Planet Gravity Game

By the way: The following construct can be simplified to not use the slow trigonometric functions because:
cos(atan(Δy/Δx)) = cos(α), in the right triangle with the legs of Δx and Δy.
In the same triangle cos(α) = Δy/r
→cos(atan(Δy/Δx)) = Δy/r
So the following can be simplified to:

// slow form:
double accelX = -(Math.cos(Math.atan2(yDist, xDist)) * gravityConstant)/ (squaredDistanceFromPlanet);
// Simplified form:
double accelX = -(yDist * gravityConstant)/ (squaredDistanceFromPlanet*Math.sqrt(squaredDistanceFromPlanet));
Sort:  

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!

Thanks! I changed it to not use trig and it has fixed a lot of issues I was having!

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 64476.22
ETH 3153.91
USDT 1.00
SBD 2.54