Real Gravity

Ive been working on a space exploration game. One of the key features is the realisticx gravity and orbits. Ive bee n trying to get this to work but every time ive failed miserably.

I hooked up a for each loop on tick, which compared against all children of my celestial body base class

And then in the “Get Gravitational Force” function I put

But after all of that nothing worked. the planets just sit idle, not moving. Does anyone know what ive done wrong?

I can’t tell for sure, since I can’t see the whole setup.
But a few things to check, even though they might seem obvious: Are your celestial bodies set to movable? Is physics simulation enabled? Is the mass correct, or might the number be so small you can’t see any effect?
Have you tried with what seems like a ridiculously large number, just to see if something happens then?

Also, here is a video about planetary gravity that could give you some pointers (it’s old, but might still help).

Yeah i check all of them. When if fiddle around i can get a rudimenttary simulation (planets orbit eachother) but it isnt accurate to the equation F = G M1M2/r2. Rather, i just do Gravitational constant * mass1 * mass2, which works, but really inacurate. Gravity slingshots dont work, orbhits are really wonky, all planets eventualy come into the sun etc. Perhaps there is an error in the calculation i put?

You may want to double-check the size of your computed units (i.e. try printing them out to the screen). For example, the mass of the earth is 5.972 × 10^24 kg and the Sun is applying an average force of 3.5*10^22 Newtons on it. However, floating point variables only have about [7 digits of accuracy][1]. It’s possible that the units are just way beyond what Unreal’s physics engine is designed for. You might want to re-scale all your units so that planets are only hundreds of meters apart and weigh a few hundred kilograms max for Unreal. Your time-steps may also need to be bigger chunks of time (which will sadly reduce accuracy).

Also, in your test code, did you remember to set linear and angular dampening on your planets to zero?

300964-dampening.png

Alternatively, you can choose not use Unreal’s physics engine, and write your own, much simpler astronomical physics engine in an Unreal C++ class with double precision variables, and then cast the final computed object positions to floats just before rendering. I suspect floating point precision is probably insufficient for truly stable orbits, etc.