Comments and answers for "Calculating trajectory of elliptical orbit?"
http://answers.unity.com/questions/805799/calculating-trajectory-of-elliptical-orbit.html
The latest comments and answers for the question "Calculating trajectory of elliptical orbit?"Comment by Eno-Khaon on Eno-Khaon's comment
http://answers.unity.com/comments/1915609/view.html
Sun, 14 Aug 2022 02:55:55 GMTEno-KhaonComment by Dehaku on Dehaku's comment
http://answers.unity.com/comments/1915216/view.html
Wed, 10 Aug 2022 20:53:47 GMTDehakuComment by Eno-Khaon on Eno-Khaon's answer
http://answers.unity.com/comments/1915206/view.html
Wed, 10 Aug 2022 19:45:10 GMTEno-KhaonAnswer by Dehaku
http://answers.unity.com/answers/1915200/view.html
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 $$?Wed, 10 Aug 2022 18:51:53 GMTDehakuAnswer by sorcerer_supreme
http://answers.unity.com/answers/1309708/view.html
@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..!!!Tue, 07 Feb 2017 13:42:39 GMTsorcerer_supremeComment by Scribe on Scribe's answer
http://answers.unity.com/comments/878451/view.html
Fri, 16 Jan 2015 23:04:24 GMTScribeComment by BerkayDrsn_Legacy on BerkayDrsn_Legacy's answer
http://answers.unity.com/comments/878315/view.html
Fri, 16 Jan 2015 19:04:50 GMTBerkayDrsn_LegacyComment by Scribe on Scribe's answer
http://answers.unity.com/comments/877491/view.html
Thu, 15 Jan 2015 18:47:34 GMTScribeComment by BerkayDrsn_Legacy on BerkayDrsn_Legacy's answer
http://answers.unity.com/comments/877219/view.html
Thu, 15 Jan 2015 12:48:00 GMTBerkayDrsn_LegacyComment by Scribe on Scribe's answer
http://answers.unity.com/comments/807483/view.html
Sat, 11 Oct 2014 23:05:51 GMTScribeComment by CupOfMayo on CupOfMayo's answer
http://answers.unity.com/comments/807413/view.html
Sat, 11 Oct 2014 20:09:00 GMTCupOfMayoComment by Scribe on Scribe's answer
http://answers.unity.com/comments/807241/view.html
Sat, 11 Oct 2014 14:57:27 GMTScribeComment by CupOfMayo on CupOfMayo's answer
http://answers.unity.com/comments/806871/view.html
Fri, 10 Oct 2014 20:13:24 GMTCupOfMayoComment by Scribe on Scribe's answer
http://answers.unity.com/comments/806775/view.html
Fri, 10 Oct 2014 17:04:17 GMTScribeComment by CupOfMayo on CupOfMayo's answer
http://answers.unity.com/comments/806511/view.html
Fri, 10 Oct 2014 09:15:01 GMTCupOfMayoAnswer by Scribe
http://answers.unity.com/answers/806221/view.html
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 = gameObject.AddComponent(LineRenderer);
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[i]);
}
}
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 = gameObject.AddComponent(LineRenderer);
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);
v += a*dt;
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[i]);
}
}
function AccelerationCalc(goArray : GravityObject[], simPos : Vector3){
var a : Vector3 = Vector3.zero;
for(var i : int = 0; i < goArray.Length; i++){
dir = goArray[i].trans.position - simPos;
gravity = goArray[i].g;
a += dir.normalized*gravity/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!Thu, 09 Oct 2014 21:50:21 GMTScribeComment by CupOfMayo
http://answers.unity.com/comments/805820/view.html
Thu, 09 Oct 2014 09:55:44 GMTCupOfMayoComment by Scribe
http://answers.unity.com/comments/805814/view.html
Thu, 09 Oct 2014 09:45:50 GMTScribe