- Home /

# Ellipticall Orbit like Spaceflight simulator

I'm making a game relying on 2D rockets that orbit planets and shoot each other. I would like to be able to predict and draw the rockets trajectory from the positions of the planets, the velocity and mass(within a sphere of influence) like in spaceflight simulator. it doesn't need to be exact, but a closed loop when it comes to the elliptical shape would be nice, so I'm guessing this is more geometry that physics. I have looked everywhere and can't find a simple, strait-forward formula. And I'm sure it's out there because so many simulators predict orbits from current state vectors. I'm not a physics or algebra wizz, but I am always happy to learn(so an explanation would be awesome if possible). Could someone help me out? or just provide me with a formula and ill try my best to figure it out.

Well, if you have multiple planets (bodies) affecting the spacecraft, it will not be a closed loop.

**Answer** by Pangamini
·
Sep 17, 2019 at 08:08 AM

Well, if you have multiple planets (bodies) affecting the spacecraft, it will not be a closed loop. If you want to get something like kerbal space program, then you should know that kerbal is simplifying gravity in a way that the spacecraft is affected by a single body (moon, planet, sun) and its whole simulation frame moves with that body, so you get perfect elliptical orbits. The way Orbiter does it (a really cool spaceflight simulator btw) is that they predict an elliptical orbit, but it gets gradually affected by the other bodies. So even if you see an ellipse drawn, it might be changing over time (even within a single orbital period) in complicated multi-body systems, such as Jovian.

One way is to calculate the elliptical orbital function using the kepler laws.link text. While those may sound simple, the mathematical implementation is quite tricky, and as I remember, the one I used was only iterative approximation anyway (I didn't come up with it, found it online). The other way would be to actually simulate your universe ahead of time and sample the predicted rocket trajectory. For that, you might want to have some kind of copy of your universe (ideally if you can separate it from the monobehaviours / unity physics) so you can create a copy of the physical universe, fast-forward it some time, read the trajectory and ditch it (so your actual game state stays unaffected)

Thanks for your answer! and yes in my simulator/game gravity will only affect a rocket within a certain radius so, making it only affected my one planet at a time. How did you implement the $$anonymous$$epler laws in an iterative approximation way? (I'm just looking at the $$anonymous$$epler laws, and as you say, the implementation seems quite daunting ), The copy of the physical universe method seems good, but then I still need the formulas, and I don't know where to start with those. what I really want is something like this

the statement above is not necessarily true, you might still have overlapping influences Also, your spacecraft may leave one circle of influence and enter the other as it moves. Again it won't give you a closed loop trajectory. Real world gravity has infinite range, so if you are interested in implementing kepler laws, keep that in mindwill only affect a rocket within a certain radius so, making it only affected my one planet at a time

ok, so it does not always have to be close looped, I just wanted to make sure that when it was a normal elliptical orbit, the formula would not create slight imprecisions so that the two endpoints would meet. but yeah, I definitely understand the hole in my logic now(i was thinking of one planet, and thought i could just "make it work" with the others). There is a workaround though, I can stop predicting the outer trajectory once it's in the sphere of influence of another body, as in "rendering trajectory from the earth, and if it hits the moon's sphere of influence, stop rendering" and when you(the rocket) are in the moon's influence, do the same but relative to the moon. But then i need to predict where the moon is, so thinking a little deeper about what you said regarding having a hard copy of the physical solar system, it's definitely the best way because I can also predict the interception of the rocket with planets during its its course. But how would I go about doing that? thanks so much for your help so far!

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.