I am trying to make a simple 2D game with Newtonian physics. My first project is to have a ship which orbits a planet and can thrust and change the orbit.
I successfully got my ship to orbit the planet, however the orbit is not elliptical but is in the pattern of a mathematical rose or rhodonea curve.
If anyone can please help me, I would like to know how to get a real elliptical orbit and I would like to know why I am getting the rhodonea curve.
Here is my orbit code (mostly taken from unitywiki):
What you’re referring to is called precession, and it happens (usually to a tiny extent) in all real orbits.
To understand why you’re getting it, just look at the following curve, and imagine a planet following this path:
It’s clear that since the planet moves directly through the center, it cannot possibly avoid precession, because to follow the exact same path as before would require an instantaneous change in direction and momentum at the ‘pointy’ end which isn’t going to happen.
So basically, a non-precessing elliptical orbit is one where the rotating body (planet) must enter the situation with the exact balance of initial conditions (speed, mass and direction) required to achieve a non-precessing orbit - i.e. you must set the initial conditions precisely.
I’m not quite sure right now how to calculate that but it won’t be very easy. This might be of help.
Also be aware that Unity’s physics might not be up to the task of maintaining the precise conservation of momentum required to achieve a stable non-precessing orbit. So you might end up having to ‘reset’ it periodically.
First off you need to multiply the mass of the object you have orbiting by everything as well. Otherwise a more massive object will be affected by the gravity less.
This is the function I use to apply gravity to objects, this function is designed so you can set the gravity for a surface gravity. IE some gravity value at a set distance from the center of the planet.
That code was setup for specific use in a project where I wanted to be setting the gravity of a planet as a direct value, and have that amount of force be the gravity at the surface of the planet.
So say I had a planet that I wanted to have 1G on the surface. The distance from the center of the planet to the surface would be the planetRadius value. And the surfaceGravity value I could set in the inspector to 1, or whatever value I wanted the gravity to be on the surface.
In regards to your other question about semi major axis distances and such I will have to consider that tomorrow, to tired right now.