by itishappy · 1y ago · 0 comments
a = G * m1 * m2 / |r|^2 * r_unit
r_unit = r / |r|
a = G * m1 * m2 / |r|^3 * r F = G * m1 * m2 / |r|^2 * r_unit
F = G * m1 * m2 / |r|^3 * r
Or as acceleration, by dividing out an `m` using `F=ma`: a = G * m / |r|^2 * r_unit
a = G * m / |r|^3 * rIt's pulled out of the unit vector. Might be more clear if I notated the vector bits a bit:
old : new
r : r_vec
|r| : r_mag
r_unit : r_dir
As you know, a vector is a magnitude and direction: r_dir = r_vec / r_mag
So the formulas from before become (also correctly labeled as `F` per my other comment): F = G * m1 * m2 / r_mag^2 * r_dir
F = G * m1 * m2 / r_mag^2 * r_vec / r_mag
F = G * m1 * m2 / r_mag^3 * r_vecThis makes sense to do in computer code also because if you were going to raise r_mag to a power, you might as well raise it to 3 instead of 2, because it's not extra cost, but you do avoid the three divisions, by never calculating a unit vector. Back when I was doing this work, was decades ago and I had no idea about cost of floating points. Thanks for explaining!
Also fun is that taking the magnitude involves a square root that can sometimes be avoided, but that doesn't really help us here because of the power of three. If the denominator were squared we could just use `r_mag^2 = r_x^2 + r_y^2`, but we still need the root to get the direction. It is kinda interesting though that in 2d it expands to a power of `3/2`:
F_vec = G * m1 * m2 / (r_x^2 + r_y^2) ^ (3/2) * r_vec