Answers for "Calculating trajectory of elliptical orbit?"
http://answers.unity.com/questions/805799/calculating-trajectory-of-elliptical-orbit.html
The latest answers for the question "Calculating trajectory of elliptical orbit?"Answer 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_supremeAnswer 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 GMTScribe