This site uses strictly necessary cookies. More Information

X- Home /

# How can i predict an Orbit?

Hello, Sorry but my english is a mess. I am trying to Predict and draw the orbit of a rigidbody around another but i cant getit... If somebody can give me a clue, that be great The orbital fuction is the same as the natural laws using the phisics included in the engine and works awesome, but i cant draw the orbit, i know how to use the linerenderer component.

what is the function throught time variable to calculate the points?

MASS = Orbiting Object`s mass RB.mass = Planet Mass. dir = direction of planet from orbiting object Distance = Distance between bodies.

RealGravity = ((Gravity) * (RB.mass * MASS)) / (Distance*Distance); RB.AddForce((dir) * RealGravity, ForceMode.Force);

Thanks!

**Answer** by Erethan
·
Feb 15, 2016 at 05:07 AM

Since your acceleration varies, position in respect to time formula is not that simple, However, you can do a trick to do the job.

You have it's start position and initial velocity. From RealGravity * dir and mass, you can find acceleration in the direction of the planet in the launch. So if you consider acceleration CONSTANT for a small change in time, you can find the next position of your LineRender with the formula below;:

S = So + v*t + (a * a * t)/2

The method below will return the Vector3[] that you need to assign in your Line Renderer. You choose how many points you will need and how close they will be together from each other. Increase timeInterval for a better approximation

```
Vector3[] TrajectoryPrediction(int points, float timeInterval)
{
Vector3[] final = new Vector3[points];
final[0] = launch.position;
Vector3 considVel = initialVelocity;//You have this from the impulse force you would add
for(i = 1; i < points;i++)
{
final[i] = final[i -1] + considVel * timeInterval + (g* g) * dir * timeInterval;
//g should be a method like your RealGravity used only for this prediction. It will use final[i-1] as the object's position for the distance and it will be without rb.mass as it will be canceled out when a=F/mass
considVel = considVel + RealGravity * timeInterval;
}
return final;
}
```

*The script above is not optimized as I am using here to teach a concept

What do you mean? This is exactly what I suggested at the end of the answer I wrote to your question. However you haven't commented back yet. This simply fast-forwards the simulation a certain amount of steps. You just give the method the number of points you want and the time interval between two points. Of course the time interval shouldn't be too large to get a good approximation.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.