# Calculating trajectory of elliptical orbit?

Hey, yesterday I did a basic script what draws trajectory of an Object in future. Here’s a part what actually calculate all that. (I already asked simillar question but there was no answer at all and now I actually understand what I need).

``````for(i = 0; i < 30; i++)
{

x = rigidbody.velocity.x * i;
z = rigidbody.velocity.z * i;
y = rigidbody.velocity.y * i + (Physics.gravity.y)/2 * Mathf.Pow(i, 2);
var pos = Vector3(x,y,z);
lineRenderer.SetPosition(i,transform.position + pos);

//y = yvo * t * g/2 * i^2;

}
``````

And It look like this(look at attachment with red trajectory)

Now I want to modify it so it will draw orbit of satellite what orbits planet so It will look somewhat like this(look at attachment with planet and satellite)

Here’s a script what I use to simulate Gravity (attached to object with rigidbody what doesn’t use gravity).

``````var other : GameObject;  //planet
var gravity : float = 10;
var dist : float;
var distSqr : float;
var force : float;
var dir : Vector3;

function FixedUpdate()
{
}

{
dir = (other.transform.position - transform.position); //force direction
dist = Vector3.Distance(other.transform.position, transform.position); //calculate distance between object and planet
distSqr = dist * dist; //distance^2
force = gravity / distSqr; //calculate force

}
``````

If you have any idea how can I achieve that please let me know :).

Hi again, sorry for the delay!

So when writing that comment I hadn’t understood the question, my suggestion would be to basically run a fast simulation of what your satellite would do and track the positions it hits. The way I do this is by defining a point and then running a loop which saves the point, the recalculates it’s position as it would appear one physics time step later, saves that position and so on. This appears to work nicely apart unless you have your object going very close to the planet with very high gravity, at which point there are so errors caused by the fact that large amounts of distance are covered in the small time step, but I expect these errors might occur with your actual object too. If you need to fix that, try reducing the dt variable!

``````var other : GameObject;
var gravity : float = 100;
var startingVelocity : Vector3 = Vector3.up;
private var distSqr : float;
private var force : float;
private var dir : Vector3;
private var orbitPoints : Vector3[];
var maxCount : int = 10000;
var simplify : int = 5;
private var privateMaxCount : int;
var lineRenderer : LineRenderer;

function Start () {
privateMaxCount = maxCount;
orbitPoints = new Vector3[privateMaxCount];
lineRenderer.SetWidth(0.2,0.2);
ComputeTrajectory();
}

function ComputeTrajectory () {
dir = (other.transform.position - transform.position);
var angle : float = 0;
var dt : float = Time.fixedDeltaTime;
var s : Vector3 = transform.position - other.transform.position;
var lastS : Vector3 = s;
var v : Vector3 = startingVelocity;
var a : Vector3 = dir.normalized*gravity/dir.sqrMagnitude;
var d : Vector3 = dir;
var tempAngleSum : float = 0;
var step : int = 0;
while(angle < 360 && step < privateMaxCount*simplify){
if(step % simplify == 0){
orbitPoints[step/simplify] = s+other.transform.position;
angle += tempAngleSum;
tempAngleSum = 0;
}
d = -s;
a = d.normalized*gravity/d.sqrMagnitude;
v += a*dt;
s += v*dt;
tempAngleSum += Mathf.Abs(Vector3.Angle(s, lastS));
lastS = s;
step ++;
}
lineRenderer.SetVertexCount(step/simplify);
for(var i : int = 0; i < step/simplify; i++){
lineRenderer.SetPosition(i, orbitPoints*);*
``````
• }*
}
Hopefully your satellites are (mostly) on fixed orbit paths as this code is quite inefficient and should be called as little as possible!
Hope that helps,
Scribe
EDIT:
var gravityObjects : GravityObject[];
var startingVelocity : Vector3 = Vector3.up;
private var dir : Vector3;
private var orbitPoints : Vector3[];
var maxCount : int = 10000;
var simplify : int = 5;
private var privateMaxCount : int;
private var lineRenderer : LineRenderer;

function Start () {

• privateMaxCount = maxCount;*
• orbitPoints = new Vector3[privateMaxCount];*
• lineRenderer.SetWidth(0.2,0.2);*
• ComputeTrajectory();*
}

function Update(){

• if(Input.GetKeyDown(KeyCode.W)){*
• `````` ComputeTrajectory();*
``````
• }*
}

function ComputeTrajectory () {

• var angle : float = 0;*

• var dt : float = Time.fixedDeltaTime;*

• var s : Vector3 = transform.position;*

• var lastS : Vector3 = s;*

• var v : Vector3 = startingVelocity;*

• var a : Vector3 = AccelerationCalc(gravityObjects, s);*

• var tempAngleSum : float = 0;*

• var step : int = 0;*
_ while(angle < 360 && step < privateMaxCount*simplify){_

• `````` if(step % simplify == 0){*
``````
• `````` 	orbitPoints[step/simplify] = s;*
``````
• `````` 	angle += tempAngleSum;*
``````
• `````` 	tempAngleSum = 0;*
``````
• `````` }*
``````
• `````` a = AccelerationCalc(gravityObjects, s);*
``````

_ s += v
dt;_

• `````` if(gravityObjects.Length == 1){*
``````
• `````` 	tempAngleSum += Mathf.Abs(Vector3.Angle(s, lastS));*
``````
• `````` }*
``````
• `````` lastS = s;*
``````
• `````` step ++;*
``````
• }*
• lineRenderer.SetVertexCount(step/simplify);*
• for(var i : int = 0; i < step/simplify; i++){*
_ lineRenderer.SetPosition(i, orbitPoints*);_
_
}_
_
}*_

function AccelerationCalc(goArray : GravityObject[], simPos : Vector3){
* var a : Vector3 = Vector3.zero;*
* for(var i : int = 0; i < goArray.Length; i++){*
_ dir = goArray*.trans.position - simPos;
gravity = goArray.g;
a += dir.normalizedgravity/dir.sqrMagnitude;

* }
return a;
}*_

class GravityObject{
* var trans : Transform;*
* var g : float;*

* function GravityObject(some_Transform : Transform, grav : float){
trans = some_Transform;
_ g = grav;
}
}*

so here I have set up a new class called GravityObject which just saves the transform and gravity strength of an instance, then I have a new variable which is a list of these GravityObjects and then the acceleration of your satellite is calculated based on each element in the list!_

@Scribe Thanks a lot. This post is like 2 years old now. I implemented this trajectory function just like you did, i just kept acceleration constant so that I didn’t have to compute it every frame. But the main problem I faced was too many calculations per frame. It never struck me that its not necessary to calculate it in a single frame. This countperFrame trick works like a charm. You are awesome…!!!

Apologies for an old and relatively unrelated post, but as I’m converting this to personal uses, I’m curious…

Did unity have a VASTLY different programming scheme in 2014 or why are all the variables and functions so strangely formatted?

Also why are 'M’s replaced with \$\$ anonymous \$\$?