How to efficently predict an object orbit?
I've implemented the Newton's Law of gravitation to all the GameObject in the scene. I'm trying to simulate the game "Orbit":Sample Video
As you can see in the video, the orbit is real-time predicted, and it gets updated as other objects influence it.
To reproduce the orbit prediction I thought about running a simulation of the entire system every second, but I don't know how to handle it because I'm using the Rigidbody physichs to create gravitational attraction.
Any idea?
Answer by lgarczyn · Dec 21, 2019 at 07:13 PM
Question already answered on stackoverflow:
Basically you can very easily simulate forces frame-by-frame for a single object, simply by incrementing and applying the velocity:
var currentPos = _rigidBody.position;
var prevPos = currentPos;
var currentVelocity = _rigidBody.velocity;
var planetCords = gameObject.transform.position;
for (int i = 0; i < stepCount; i++)
{
var distance = planetCords - currentPos;
var forceMag = Gravity / distance.sqrMagnitude;
forces = distance.normalized * forceMag;
currentVelocity += forces * Time.fixedDeltaTime;
currentPos += currentVelocity * Time.fixedDeltaTime;
//Replace by a TrailRenderer or LineRenderer you reset each frame
Debug.DrawLine(prevPos, currentPos, Color.Red, Time.deltaTime);
prevPos = currentPos;
}
Answer by ristophonics · Aug 01, 2020 at 07:51 AM
@lucadome On the same problem for a few years ... and still going. For a [game][1] too.
I have dug up the math to do this.
You need to solve Kepler's equations with Newton's iteration. Most of the math is here:
Your answer
Follow this Question
Related Questions
Play animation based on impact position? 1 Answer
How to predict where the soccer ball should be intercepted by an AI player after being kicked? 0 Answers
Predict the hit position 0 Answers
How does hinge joint work? 0 Answers
Two Vectors Beetween Angle 0 Answers