-3

I want to make a Solar System kind of Simulator an therefor I want to move a Planet around the Sun. I got the drawing Part done, but I don't know how to do the circular moving Part around the sun.

Thank you.

1 Answers1

2

Suppose you have the coordinates of the sun and the desired radius, like

double sunX = ...
double sunY = ...

double radius = ...

and some kind of game-time which we use to determine current position together with some kind of speed:

int time = ...

Let's say the time increases once per millisecond. And the planet should start movement from and reach 360° (from 0 to 2 * PI) again after 2 seconds (2000 milliseconds), then the current angle may be determined by

double orbitalPeriod = 2000.0;
double portion = (time % orbitalPeriod) / orbitalPeriod; // [0, 1)
double angle = portion * 2 * Math.PI;                    // [0, 2 * PI)

We can now easily compute the coordinates for the planet by using the desired angle and radius.

double planetX = sunX + radius * Math.cos(angle);
double planetY = sunY + radius * Math.sin(angle);

You can read more about the formula on Wikipedia. The following image might probably help in understanding the equation:

enter image description here

Zabuzard
  • 25,064
  • 8
  • 58
  • 82