- Home /
Predict orbit based on velocity and basic gravity script
So I have my basic planetary gravity script
var other : GameObject;
var gravity : float;
var dist : float;
var distSqr : float;
var force : float;
function FixedUpdate()
{
AddGravity();
}
function AddGravity()
{
var dir = (other.rigidbody.position - rigidbody.position);
dist = Vector3.Distance(other.transform.position, transform.position);
distSqr = dist * dist;
force = gravity / distSqr;
rigidbody.AddForce(dir.normalized * force);
}
And it works perfectly fine, the thing is I want to draw the orbit before satellite finish one orbit. I was thinking about putting another object and getting velocity and other variables multiped(for example by 10) but that didn't work so well. Any help would be appreciated :)
Answer by fafase · Oct 27, 2013 at 12:02 AM
You can anticipate the path of an object simply getting its equation ahead of time.
Using wikipedia, for projecctile motion:
x = initialVelocity * Mathf.Cos(angle) * time;
y = initialVelocity * Mathf.Sin(angle) - 0.5f * gravity * time * time;
Where time is just how long the object has been flying. time is the value at the moment, time + 1 is the value ahead 1s and then you could plot a point there to show where the guy is going, same with +2 to have even further.
If your object is in ideal condition (no air resistance) then it is fairly simple, with air resistance, it gets trickier since it is based on geometry.
If you need 3D I think the z goes:
z = initialVelocity * Mathf.Sin(angle) * time;
It would be nice if someone with a Phd in physics could confirm that.
Yes the object (satellite) don't have any drag, but can you tell me more about that and can i use it as Update? I mean like if i add force to a satellite it will draw on what orbit will it move (kinda like in ksp map view)?
It gets slightly more complex since for each point your gravity is now depending on the distance to the planet. You could use the inverse square law:
g = g$$anonymous$$ax / distance distance; g = (g < $$anonymous$$Value) ? 0 : g; // if g is too small, makes it 0 x = initialVelocity $$anonymous$$athf.Cos(angle) time; y = initialVelocity $$anonymous$$athf.Sin(angle) - 0.5f gravity time * time;
You would maybe only consider the closest planet or you will end up with astronomical calculation.
Okay I see, but unfortunately I am horrible at understanding new things :S, I know this is not a place for "hey i dont know how to do that, can you write script for me", but I still need some more information about how I use it. I guess i should use it with line renderer? but then I am not sure what does (angle) mean :(.
Better off reading this http://babek.info/libertybasicfiles/lbnews/nl130/proj3d.htm