Skip to content
Advertisement

Orbit Simulator in Java returning odd values for velocity etc. despite correct math

I am using LibGDX to make an orbit simulator (elliptical as planets possess their own initial velocity) and I have the physics mapped out like so:

JavaScript

The problem is that my planet wobbles around and doesn’t orbit at all. I fear my calculations in the code might be wrong as the physics are definitely double-checked and correct.

Each celestial object is a planet and I have no ‘Sun’ classes of any type so far. Only one Planet class, which only has Getter and Setter methods, a render() method (which seems irrelevant) and the presented move() method.

I find nothing necessitates the following however I will add the parameters’ values I chose for the both planets:

JavaScript

I am also aware LibGDX won’t have the x, y coordinates of my circle in the middle but rather the bottom left. Therefore I have modified that in the constructor:

JavaScript

Advertisement

Answer

I have been messing around and debugging the code and realised it was a very minor mistake, a classic mistake to assume that the math library’s cos() and sin() functions use degrees. They don’t. They use radians and that was the whole problem all along.

Instead of:

JavaScript

One must do:

JavaScript

I will make sure to edit the question to emphasize this problem and solution for future viewers of it.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement