# How to program gravity field?

I am working on a program to learn about OOP (object oriented programing) and I want to make a program that lets you create planets and fling them around a sun. Here is the current code. Note, I am always actively changing it so the code may be broken at the time you click to open it. Sorry if that happens.

https://www.khanacademy.org/computer-programming/solar-system-creator-oop/4441139047

===========================================

I know the gravity formula is F = (G*M1*M2) / R^2. The problem is that F is a scalar, not a vector. I need to split this equation into X and Y components. But apparently, you cannot just simply define the X components and Y components separately using that formula because it is not a linear/inverse relationship. :( Anyone know what the easiest way to make gravity vector field?

## Discussions

Best Answer 3 years ago

F is a vector. For Newtonian gravity (which is what you're doing), it is always directed along the line between the two masses (which you are modelling as points), and is attractive (it will reduce the distance between masses).

For simplicity, you are probably assuming that your sun is much, much, MUCH more massive than any of your planets, and therefore it doesn't move. So place it at the origin. Then, the direction of F is exactly opposite the vector pointing to the planet's position, and the magnitude of F is given above. Use the (x,y) position of the planet to compute a unit vector, and multiply each component of the unit vector by F (magnitude).

Answer 3 years ago

I figured it out! I was afraid I'd have to do lots of converting between polar coordinates and XY coordinates, defining the magnitude as G*m1*m2/r^2, and the direction as some angle, which would have been a nightmare. However what I did was multiply the magnitude F (calculated by G/sqrt(x^2+y^2)^2 or simplified: G/(x^2+y^2)) and multiply that to the unit vector in the direction between the planets and the sun. (which so happens to be exactly what you said!) It works pretty well now!

Answer 3 years ago

Yes, that's exactly right; congratulations! And the unit vector is just (x/r, y/r). You probably want to precompute r, unless you're using a proper vector math library.

Answer 3 years ago

I think his problem includes the rotation and spins as well.

Depends on how accurate it needs to be, I mean in games they use routines for this that look good but don't have to reflect reality.

For gaming I might try to use F as a value for speed/acceleration to influence the path of the planets.

I mean it is about flinging them with your fingers, so IMHO the thing does not need to be 100% precise as long as the relation of the mass to the flicking is similar for all planets!?

Answer 3 years ago

If you have distributed masses (i.e., not point sources), then you need to take into account the shape of the object, because the tidal force (gradient of F) will induce a torque, which will affect the motion.

But this simple problem is assuming point masses, so none of that is relevant in Newtonian gravity.