Converting Local to World Forces in gravity simulation.

I have a semi-realistic solution to orbital gravity but it has several problems:

  1. It uses a system that only allows body-body gravity.

  2. It works in local space.

  3. It makes my planets turn unrealistically.

  4. It only works for large objects(planets, moons, stars)

I’d like to find a way to convert the local forces to world forces and store them in variables, then apply them in world space.

I think that I should describe this more in depth, so here we go.

At the moment my system has two spheres of different sizes childed to there own empty objects. The empty object is rotated (problem 3) to face the other’s sphere, so the planets turn as the empty object turns and we all know this doesn’t happen. Then, a second script uses a function to find the distance between them and does this equation g = (GxM1xM2)/(DxD). This gives output in Newtons, then I divide it by its imaginary mass. Imaginary mass is a mass to large to simulate with the ridgedbody. This gives a force of Newtons equal to Meters per Second Squared if the mass of the ridgedbody is 1kg. Then, I apply this as a constant force in the local Z (problem 2) direction of the empty object (Ridgedbody’s mass is set to 1kg). Now, I have gravity! So as the “planet” is childed to the empty object, it will not work for an object (spaceship) that has other forces applied to it (problem 4). Also, it can only face one object at a time. Therefore the force can only be applied in one local z direction. Making it impossible to create a multi-body gravity simulation where a moon pulls on a planet while it gets pulled on by its planet’s star (problem 1).

I can put up the scripts if it helps but not if you dont need them. If you need more explantion on a aspect of the system I can supply that to.

Thanks for the help.

why can’t you add force to rigidbodies in different directions?

you can calculate force for each pair of obects and apply force every FixedUpdate() in relative directions - unique for each pair. also it’s better to ignore far-placed pairs to make calculations lighter, close object’s gravity is much more powerful then far-placed, so you can ignore it in usual game.