Jump to content

KSP, Unity, and orbital dynamics


RyanBrian

Recommended Posts

Hey all! New here, and I apologize if this is either posted in the wrong place or if there's existing thread about this topic already. I'm curious if the devs have shared anything about what the physics updates look like for bodies in KSP, given the nature of Unity GameObjects, Rigidbodies, and the FixedUpdate and Update methods. I've been developing a simple 2D orbital mechanics game/sandbox for fun, in part to revisit classical mechanics concepts that I'm sadly far out of practice with, and it's raised a number of questions! 

My original assumption was that bodies in KSP simply have one gravitational force applied to the body by means of the Unity-built-in Rigidbody.AddForce(<gravitational_force_applied via other body>) in a body's FixedUpdate() method, but given the 2-body nature of the game, this isn't necessary, considering such a system's deterministic nature and the ability to easily calculate elliptical, parabolic, or hyperbolic trajectories from just the masses, relative position, and relative velocity of the two bodies. In setting up some mechanics where I apply appropriate forces in the FixedUpdate() method, while also calculating a given orbit's semimajor axis, specific angular momentum, eccentricity vector, etc. in the object's Update() method, I've noticed that my parameters that ought to be conserved (e.g. specific angular momentum, specific orbital energy), are not quite as conserved as I'd like them to be. 

I'm certainly not expecting perfection here, but the experience has mainly further amplified my appreciation for the physics of KSP.. The stability of orbits over time is fantastic! It has me wondering if, when a body stops accelerating by anything BUT gravity (i.e. we're not thrusting or interacting with an atmosphere), an equation for an ellipse/parabola/ hyperbola is determined and parameterized, and the body is then just updated along that path until some additional force outside gravity is applied.

Again, I'm new here so I apologize if this is common knowledge or well-documented, but a cursory search didn't lead me to anything, so I'm curious. Thanks for any information!

Link to comment
Share on other sites

23 hours ago, RyanBrian said:

I'm certainly not expecting perfection here, but the experience has mainly further amplified my appreciation for the physics of KSP.. The stability of orbits over time is fantastic! It has me wondering if, when a body stops accelerating by anything BUT gravity (i.e. we're not thrusting or interacting with an atmosphere), an equation for an ellipse/parabola/ hyperbola is determined and parameterized, and the body is then just updated along that path until some additional force outside gravity is applied.

KSP uses a deterministic function like what you described to calculate the orbital position, velocity, etc. for all craft at any given point in time, t. This concept is briefly touched upon in this video where the original developers talk about how KSP was developed. Though they don't go greatly into the specifics of how this works, I've been able to implement this concept myself in Unity with varying degrees of success.

Link to comment
Share on other sites

30 minutes ago, RyanBrian said:

Thanks for the reference, and the info! Yeah I might've rushed to posing this question without thinking much on time warping.. Without the deterministic approach, it'd be real tough to maintain stable orbits at real time and 1000x time, heh.

Don't give up just yet! The deterministic approach isn't that difficult to implement if you can calculate a true anomaly from a mean anomaly. I'd be happy to share my notes if you like.

Link to comment
Share on other sites

  • 1 month later...

Sorry for a month delay in getting back to you, but thanks for the input! There's some awkwardness in what I want, as I want to be able to make use of Unity's rigidbody physics for things like collisions ultimately, but otherwise defer to deterministic updates when not subjected to forces beyond a single gravitational influence. I've got decent agreement b/w the FixedUpdate() gravitational force update and the deterministic Kepler problem update now. 

 

Link to comment
Share on other sites

4 hours ago, RyanBrian said:

Sorry for a month delay in getting back to you, but thanks for the input! There's some awkwardness in what I want, as I want to be able to make use of Unity's rigidbody physics for things like collisions ultimately, but otherwise defer to deterministic updates when not subjected to forces beyond a single gravitational influence. I've got decent agreement b/w the FixedUpdate() gravitational force update and the deterministic Kepler problem update now. 

 

I'm glad to hear it!

Link to comment
Share on other sites

  • 1 month later...
On 3/3/2020 at 11:36 PM, prestja said:

I'm glad to hear it!

Hey! If you're interested, I kept running with the idea :) Web is certainly not my intended target, but I figured I'd slap a pause menu on, rig a rough input model in, and build to web to showcase some of mechanics discussed here. Be wary if you test it out, the Kraken here is far more aggressive than KSP's..
 

Hosted on my github site:
https://rtoole13.github.io/Orbital/

Link to comment
Share on other sites

18 hours ago, RyanBrian said:

Hey! If you're interested, I kept running with the idea :) Web is certainly not my intended target, but I figured I'd slap a pause menu on, rig a rough input model in, and build to web to showcase some of mechanics discussed here. Be wary if you test it out, the Kraken here is far more aggressive than KSP's..
 

Hosted on my github site:
https://rtoole13.github.io/Orbital/

This is absolutely incredible! I've taken a look through your code and I'm very impressed at the work you've done on simulating hyperbolic trajectories. I can easily see this becoming a fully-fledged game for mobile devices.

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...