I’m attempted to simulate a planet’s orbit around a star. I want to make this orbit as accurate as possible. Here is the code I have so far in c++ which simulates an object going in a straight line.
void ARotating_sphere::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
time += DeltaTime;
NewLocation = GetActorLocation();
FVector Distance = FVector((NewLocation.X - StarLocation.X), 0, (NewLocation.Z - StarLocation.Z));
Direction.X += ((Force.X) / mass1) * DeltaTime;
NewLocation.X += Direction.X * DeltaTime;
Direction.Z += ((Force.Z) / mass1) * DeltaTime;
NewLocation.Z += Direction.Z * DeltaTime;
Force.X = ((Mass1 * Mass2) / pow(Distance.X,2));
Force.Z = ((Mass1 * Mass2) / pow(Distance.Z,2));
SetActorLocationAndRotation(NewLocation, NewRotation, false, 0, ETeleportType::None);
}
What I’m doing is I’m updating the position and velocity. For the velocity I’m taking the current velocity and adding the acceleration (which is force/mass) and for the position and taking the current position and adding the velocity. As for the force, I’m using an equation which adjusts the force based on how close the planet is to the star. You can also see that for distance that I’m setting y to is 0. This is because for this particular animation, I don’t need to use the y position. Now this works if I just need to move an object in a straight line. What I’m trying to do now is take this code and make it that the planet moves in a circle around the sun.
I tried using sin(time) and cos(time) which worked for moving the planet around the sun but for an orbit, I want the rotation to speed up when it gets close to the star. That’s what those two force equations are. The force is calculated by multiplying the mass of the planet and star and then dividing it by the distance between them. So if the planet is closer to the start then the rotation will go faster.
So how can I take this code and change it into an orbit.