0

I'm making a 4x game with solar systems in the Unity game engine. I have planets which I'd like to orbit around their stars in an elliptical fashion. The planets for various reasons are not parented to the stars. The game is in 3D space with a top down view, so the orbits are on the x and z planes with a y of zero.

Following on from various posts e.g. this, this and this I've put together the following code in a co-routine:

    while (true)
    {
        yield return new WaitForFixedUpdate();
        x = orbitStar.position.x + ((dist + xMod) * Mathf.Sin(a));
        z = orbitStar.position.z + (dist * Mathf.Cos(a));
        orbitLines.position = new Vector3(x, 0, z);
        a += aPlus * Time.fixedDeltaTime;
    }

orbitStar is the star the planets are orbiting, dist is the distance to the star, orbitLines is the object to be orbited (actually a trail renderer, later to also be the planet but in turn based time not real time) and xMod is the variable that controls how elliptical the path is. a and aPlus are arbitrary variables that control the angular velocity.

This forms a nice ellipse based on xMod. The problem is the trail renderer does not bisect the planet as xMod is increased, the trail moves further away from the planet, here the trail is the turquoise color curve:

enter image description here

Being somewhat inept at maths I've tried juggling round the variables and throwing 'magic' numbers at the function with inconsistent results e.g:

z = orbitStar.position.z + ((dist - xMod) * Mathf.Cos(a));

and

z = orbitStar.position.z + (dist * Mathf.Cos(a)) + someOtherVariable;

How can I correct for the distance xMod is moving the trail by, so that the trail moves through the planet?

Absinthe
  • 3,258
  • 6
  • 31
  • 70
  • Show the rest of the trail render code. – meowgoesthedog Feb 28 '18 at 14:21
  • 1
    Things to note: 1) the star is at a *focus* of the ellipse, not its center 2) angular velocity (aPlus) is not constant – meowgoesthedog Feb 28 '18 at 15:06
  • @meowgoesthedog the trail renderer is a built-in class in Unity, it just draws a line behind an object which is moving, so has no real relevance to the question at hand. Similarly the aPlus variable is something fed into another Unity class to control the speed of motion and has no effect on the shape of the ellipse, only how fast an object (the trail renderer) travels in the described pattern. – Absinthe Feb 28 '18 at 16:54
  • as meowgoesthedog hint your approach is not related to real world. Use Kepler equation for more realism. What is the size of your system? My bet is your accuracy is lower than you think if you use SI than the orbital radiuses are big ... meaning quadratic ellipse will be even less precise causing this offset between quadratic ellipse (orbital trajectory line) and goniometric position of planet. take a look at this [Is it possible to make realistic n-body solar system simulation](https://stackoverflow.com/a/28020934/2521214) it deals with a lot of the problems you are facing... – Spektre Mar 01 '18 at 07:04

1 Answers1

0

It all depend where the planet is. If I understand properly, you are trying to create an ellipse around a center point, and then try to figure out the parameter of this ellipse so a certain planet is on its path. You need to take this planet's coordinate into account.

The ellipse equation is

x = a * cos(t)
z = b * sin(t)

In your case,

a = dist + modX b = dist

Basically, when z = 0, the planet must be is at a distance 'a' on the x axis to be on the ellipse. In the same way, if the planet is at x = 0, it must be at a distance 'b' to be on the ellipse.

So the question is: where is your planet? if the planet is fixed, there is an infinite number of solutions for a and b, to make different eclipses go through it. You could choose a ratio between a and b that you like and solve the equation above to find the value of 'a':

x = a * cos(t)
z = a*ratio * sin(t)

--> You know x, y, and ratio. t remains variable. solve for a

If, on the other hand, you want to choose the ellipse path and then place the planet on it, just choose a and b and find the planet x and z by computing

x = a * cos(t)
z = b * sin(t)

And choose and arbitrary 't', which represents where the planet is around the eclipse

This link might help you visualize what variables 'a' and 'b' do

Basile Perrenoud
  • 4,039
  • 3
  • 29
  • 52