- Home /
How to predict orbit based on time?
How can I predict a spaceship's orbit, as the player adjusts the velocity. Let's say the spaceship is in a circular orbit. How do I predict the future XYZ coordinates for that and also for elliptical orbits. Say the player fires the engines to boost the speed. How do I continuously update the orbit?
I can find tons of formulas on Kepler's and Newton's laws but none of them are for the future XYZ positions.
I know Kerbal Space Program predicts trajectories using a function of time but they don't say the formulas they use.
People usualy work in spherical coordinates to resolve such a system, you only need to use the newton gravitation equation (it is an acceleration expression so you need to integrate it 3 times)
I could not find the right example on the internet at this point.
yeah, I'm using Newton's universal law of gravitation to add a force towards the planet and adding a horizontal (relative to the planets surface) force to make the satellite orbit.
Here's the script that does that:
public float g = 6.674f * (10^-11);
private float massSatellite;
public float massPlanet;
private float r;
public GameObject Planet;
public Rigidbody rbObject;
public Rigidbody rbPlanet;
public Vector3 dist;
private float gravitationalForce;
public float initialForce;
private Vector3 gravityOfPlanet;
public float gravitationalAcceleration;
void Awake()
{
rbObject = GetComponent<Rigidbody>();
rbPlanet = Planet.GetComponent<Rigidbody>();
}
void Start()
{
massPlanet = rbPlanet.mass;
massSatellite = rbObject.mass;
rbObject.AddForce (new Vector3 (initialForce, 0, 0), Force$$anonymous$$ode.Impulse);
}
void FixedUpdate()
{
dist = Planet.transform.position - transform.position;
r = dist.magnitude;
gravitationalForce = (g * massPlanet * massSatellite) / (r * r);
rbObject.AddForce(-dist * gravitationalForce); //maybe add Forecemode.acceleration
}
What I would like to do is predict the orbit using a linerenderer and a for loop.
I actually just replaced the initial force with an initial velocity vector if that would make things easier.
Can you find an answer to your question?, i have the same problem...
Answer by PeterMu · May 30, 2016 at 04:44 PM
Hi,
I have just completed a blog post on this topic: http://nbodyphysics.com/blog/2016/05/29/planetary-orbits-in-javascript/
It explains how to use the Kepler equations and provides sample Javascript code.
If you want an "out of the box" solution, you can take a look at my new asset "Gravity Engine" in the asset store: https://www.assetstore.unity3d.com/en/#!/content/62432
PM
[1]: https://www.assetstore.unity3d.com/en/#!/content/62432
Your answer
Follow this Question
Related Questions
Simulating 2D planetary gravity and trajectpry using Unity 0 Answers
Ellipticall Orbit like Spaceflight simulator 1 Answer
Can you pls help me fix this rigidbody collision problem 0 Answers
RigidBody movement with AddForce 3 Answers
I have a planetary gravity script; how do I make the object speed up? 1 Answer