Jump to content

[Maths / Physics] Equation of an orbit


grawl

Recommended Posts

Hello everybody !

I'm developping a small smartphone 2D game for my end of year project, and I'm blocked because of my little maths knowledge.

I knew I should have been more present in physics class... :blush:

So, here are my problems :

I've got a star with mass and a "circle" of influence (we're talking 2D here :) ), defined as the center (x=0, y=0).

I've got several objects with mass, coordinates (Xo,Yo) and a speed vector defined by its coordinates (Xv,Yv).

1. Is it possible for the objects to achieve stable orbit around the star ? Or will I need to tweak reality a bit ?

2. What is the way to calculate the new coordinates of the object's vector once it is inside the star circle of influence ?

I figured here was the best place to ask :D

Thank you for your answers, I'm looking forward to them.

PS: Also, if anyone got any idea about a possible gameplay and a way to use the phone captors (accelerometer, inclinometer, screen touch, etc), I'm all ears.

I've already planned to implement some kind of solar storm shot by the player to "eat" the objects it crosses and make your star bigger :P

Edited by grawl
Post Scriptum
Link to comment
Share on other sites

This is fairly easy to achieve with a physics engine such as Box2d. Are you supposed to code your own physics for the project?

I'm playing around with the idea of an "realistic" 2d space game, and this helped me a lot to get started: http://appcodingeasy.com/Gideros-Mobile/Gideros--Box2d-magnets

You have to calculate the vector between two points, which is basically x2-x1, y2-y1. It's also a good idea to calculate the distance between the objects, so that the strenght of gravity varies depending on it.

Link to comment
Share on other sites

Hello,

The thing is I don't use a physics engine. I want to code everything by myself :) I know it might not be a good idea, but I don't got the time to learn how to work with an engine.

What I need is more something like a list of formulaes defining speed and trajectory (I think that's all I need) of an object in orbit.

Like a speeded-up physics course :P

If this isn't successful, I already have a backup plan, that is making a 2D game with sprites where you have to avoid moving objects in the sky with your rocket, then go to a space station (I havn't defined yet how you could "go to the space station").

Either way I'll call it "Cnam Space Project" (my school is called Cnam). I think you'll catch the reference :cool: .

Link to comment
Share on other sites

The simplest way is to calculate the forces acting on an object(look up the proper equations for gravity and whatever else is involved) every frame, update it's velocity(velocity=velocity+acceleration*time), and then update it's position(position=position+velocity*time).

There are more complex methods that achieve better accuracy and stability, but they aren't always necessary. To find relatively stable orbits to work with, look up the math for kepler orbits. From that you can get the starting velocities you need for orbits of given shapes.

Though from the game you describe, I'm not sure if realistic orbital mechanics would necessarily serve your gameplay best.

Link to comment
Share on other sites

I've been following an accelerated course in Kepler's Law programming applications :confused:

I'm studying the Verlet loop right now.

[edit]

I think I might have got it.

Going back to the working bench now...

Keep posting your ideas and thoughts, I'll be back with results ! (hopefuly :sealed: )

And more questions (surely :P)

Cheers!

[/edit]

Edited by grawl
Link to comment
Share on other sites

Success !

I've successfuly implemented the so called "Verlet Algorythm" in my little app.

I am still trying to play with the parameters to get a enjoyable result. I've only tried an higher Star mass for now, but I still get a lot of planets going out of screen

Do you have any ideas?

Here is a piece of the code I wrote.

static void nextOrbit(Planet planet){

double xPlayer=Player.getX(); //Star position X

double yPlayer=Player.getY(); //Star position Y

double dt = 1; //Time factor

double dt_2 = dt / 2; // Used for the Verlet algorythm

double GM = 160; // g times mass of something.

double vx = o.vectX; // x-component of the velocity

double vy = o.vectY; // y-component of the velocity

double x = o.posX- xPlayer; //x planet position relative to star

double y = o.posY - yPlayer; // y panet position relative to star

//Verlet loop

x += vx * dt_2;

y += vy * dt_2;

double r = Math.sqrt(x*x + y*y);

double r3 = r*r*r;

double ax = -GM * x / r3;

double ay = -GM * y / r3;

vx += ax * dt;

vy += ay * dt;

x += vx * dt_2;

y += vy * dt_2;

planet.posX=x+ xPlayer;

planet.posY=y+ yPlayer;

planet.vectX=vx;

planet.vectY=vy;

}

Link to comment
Share on other sites

Keep in mind that Verlet integration is very bad at gravity. Comparatively so, of course. But you should definitely check yourself. Put a single object orbiting a star/planet and run your simulation. But also, draw the ellipse that shows what the trajectory should be like. See if after a few turns around the star/planet your object stays on the correct trajectory. If not, you'll need to decrease dt.

P.S. Your Verlet loop has a slightly different structure than I'm used to, but this might be just another way of writing the same thing. I'll have to sit down and check it with a piece of paper.

Edit: Actually, I'm almost certain that you are doing Forward Euler instead of Verlet. Did you copy the algorithm from somewhere?

Edited by K^2
Link to comment
Share on other sites

I did copy the algorithm from a python based program simulating the orbit of a planet around the sun.

Here it is.

What are the differencies between forward Euler and Verlet loop methods ? Any method more stable than the other?

Link to comment
Share on other sites

Verlet is second order and Forward Euler is first order. They both solve the same problem and both can be equally precise, but the size of the step, usually, has to be smaller with Forward Euler to achieve the same precision.

There is one more advantage of Verlet integration which is responsible for its popularity. It's really good at conserving energy in harmonic interactions. Basically, if you simulate collision between two objects by pretending that there is a spring between them that starts pushing objects apart as they get closer, Verlet integration is much better as it almost guarantees that energy is conserved. With Forward Euler you tend to end up with energy building up in the system. So if you ever see a pool simulation where the billiards seem to bounce off faster than they should, and things getting really chaotic when multiple billiards collide, that's probably because the wrong integration method was used. Verlet cleans that stuff right up.

With gravity, both methods tend to cause energy accumulation over time. And there is no general solution to that problem. It's extremely challenging to build a true simulation of gravitational interaction. The algorithms used by NASA can get rather complex because they need to be able to predict motion of asteroids with very high precision.

For a game, that's not as big of a deal until you start having a lot of objects interacting with each other. If you'll have some spare time, I suggest you try writing a galaxy simulation. You'll see what I'm talking about. But when you just a few satellites around a star/planet, it's not so bad. Either method can be made to work. What you have up there will definitely give you the right answer. It's just worth testing it against an exact solution to see how small you need to make your dt. And if you can improve a bit on that by using a second order method, why not?

I'll take a look at the code you linked to when I have a bit more time, and I'll see if there is an error there or whatnot. But like I said, what you have right now isn't actually wrong. It should work, and you seem to indicate that it does.

Link to comment
Share on other sites

I definitely get the behavior you're describing (energy accumulation). I'll check my dt when I have the time (I'm at work right now, away from my beloved eclipse :D lol)

One more question though, how can I define the semi-major axis and the eccentricity based on the position / speed vector of a point of this ellipse ?

Basically, I got the tangent, how can I find the ellipse itself using it ?

Link to comment
Share on other sites

Simplest way is to compute specific energy and specific angular momentum. (Specific just means per unit of mass. So I'll drop m from all of the subsequent equations.) These quantities are conserved throughout the trajectory and together they fully define an orbit. Here is a bit more detail. The coordinates, x and y, are relative to star. So is distance r = sqrt(x² + y²).

Energy = (1/2)(vx² + vy²) - GM/r [This is kinetic + potential energy per unit mass.]

Angular Momentum = x * vy - y * vx [A.M. is given by cross product.]

It's also very easy to express these two quantities in terms of periapsis and apoapsis. Let me call these rp and ra respectively. In that case:

Energy = - GM/(ra + rp)

Angular Momentum = sqrt(2GM * ra * rb / (ra + rb) )

Finally, once you solve for the apsides, semi-major axis is just (ra + rp)/2, and eccentricity is (ra - rp)/(ra + rp).

Link to comment
Share on other sites

So, here I am, back with results :)

K^2, I did not use your equations, as the model I have is quite stable now. I tweaked the GM value, the initial speeds and positions of my objects. I've put GM at 300 (!) I also tweaked a bit the dt value as you suggested. I'm still playing with it between 0.8 and 1.5, and get satisfying reuslts.

I've got objects that can, in a simplified way, aggregate together (the area of the smaller one is added to the bigger one. The smaller one is disappearing and the bigger one is keeping on its course), or disappear into flames down into the Star.

It's giving me some quite stable systems after a while (less than 5 mins). My phone is struggling to manage more than 200 objects at the start. It's getting easier as the time goes, because some objects go out of the calculation zone, aggregate together or disappear into the star.

I've also seen some beautiful galaxy-shaped systems when I put a lot of planets. :D I was amazed by the power of randomness :P, and trial-error :rolleyes: .

The next thing I'd like to do is, if it is simple enough, implement the result of the collision of an object with another on the trajectory.

How do the mass and speed of the 2 objects influence the resulting movement?

My dream would be to implement n-body physics, but I'm not sure my phone would cope with it for more than a dozen objects. I want to keep my initial objects number as high as possible (the power of randomness isn't acting if there is not enough objects :cool:).

I think I have also found a way to use this in a "playable" way.

At the start the objects would have something like "matter type", or "resources", if you prefer. (Dirt:3units, Water:2units, or Uranium:1unit, Stone crystal:2units, etc)

These would pile up when objects aggregate together.

When clicking on an object, this would allow you to review the composition of your objects. If I can spend enough time on it, I think I even can do something that will tell you the possibility of life on the planet (random number multiplied by somenthing in relation to the size of the planets, the materials forming it - water or not water - , the last impact and the distance from the star).

Where can I find documentation on the guy that calculated the possibility of life in the universe?

Give me your ideas !

I will need a lot of resource names ideas also. The ones I thought about are a bit crappy (see for yourself up there)

Thanks !

PS: is there any way to take screenshots on an android phone?

Link to comment
Share on other sites

Hello , it's me again !

I'm trying to gather some useful informations about my planets, like periapsis and apoapsis.

I've got how it works, but I always seem to miss one parameter, the eccentricity.

K^2, I cannot find it based on your equations, as there is no way for me to know Ra and Rp individually.

If only I could find a way to calculate eccentricity...

All the examples I've found on the 'net are with horizontal ellipses centered in the middle of a repair. Mine are not centered and not horizontal.

As you can see I'm still struggling with orbital maths. I should do an internship at squad to learn more about it :D

Link to comment
Share on other sites

grawl, equations I have shown you can be solved for rp and ra. So again, you can easily compute E (energy) and L (angular momentum) of the body. From that follows:

L2 = -2E ra rp and E = -GM/(ra + rp)

or, slightly rearranged

ra + rp = -GM/E and ra * rp = -L2/(2E)

So you know a sum of the two quantities and you know their product. Given that, you can easily find quantities themselves. Let me define:

S = -GM/E and P = -L2/(2E) so that now:

ra + rp = S and ra * rp = P

From product equation, rp = P/ra, which I can now substitute into the sum equation.

ra + P/ra = S

And, of course, I can multiply the whole thing by ra and carry all the terms to one side.

ra2 - S ra + P = 0

That's a straight forward quadratic equation which has a solution:

ra = (S +/- Sqrt(S2 - 4P))/2

Why the +/-? Because this particular set of equations can't distinguish between which of the two apsides is the periapsis and which is apoapsis. So you really get two for one.

ra = (S + Sqrt(S2 - 4P))/2

rp = (S - Sqrt(S2 - 4P))/2

Once you substitute the expressions for S and P back into these, you'll have equations you seek.

Edit: Note that this does not tell you the angle of the line of apsides. That's a separate equation that I can run you through if you are interested.

Link to comment
Share on other sites

Haaaaa... :confused:

Now I see....Easy....

It's really a long time from my last maths class :D I don't know how I could not see this possibility :blush:

I need to rework my algorithms now, with this new insights in mind.

Holo, I think I'll use a simplified version of the drake Equation, or just make one myself :P As their accretion is randomly happening, I can't define "models".

I will definitely use the chemical elements to "build" my planets, it is the most straightforward way to do it..

Thank you !

If you want to check some screenshots, go there

I'll create a proper final thread with good screenies and a review of the concepts I used (if I can remember them all) when all features have been implemented and the app is near completion.

Do NOT expect it to blow your mind though :sticktongue:

Link to comment
Share on other sites

I am trying it now.

Where does "Energy = - GM/(ra + rp)" comes from ?

I've been scratching my head in all sorts of ways and I don't understand :(

[re-edit]

[edit]

But it still doesn't work, I've got S equals -Delta (Delta=sqrt(S²-4P)) at all times...

So the results are 0 and 2S everytime :/

Do you know what symptom is that?

I seem to miss a constant of some sort...

[/edit]

Forget it, it was all my fault !

It was due to my (very) stupid way to write equations in java code :0.0: Fixed it now.

It is working now, and really well !

Thank you very much !

[/re-edit]

Edited by grawl
digressions in edit
Link to comment
Share on other sites

Ok, cool !

Now I need to start working on the UI part... boring... lol :D but indispensable.

Thank you again for the time you spent explaining me this stuff, I wouldn't have made it without it.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...