admin管理员组

文章数量:1414621

Clip of wonky orbit
I made a rudimentary (I'm still extremely proud of it) orbital sim. The satellite does not go around the planet on the second pass the same way it went around on the first pass. Its periapsis is constantly moving, not unlike orbital precession. I would think it actually is orbital precession, but there are no other orbital bodies in the simulation. The satellite exerts no force back on the Earth. Is it a problem with my code or is this a real phenomenon? The code below are the only lines that manage the orbital physics.

public static float Gravity(Transform m2Loc, Rigidbody m1Mass, Rigidbody m2Mass, float gravityConstant)
{

    float force = (m1Mass.mass * m2Mass.mass) / MathF.Pow(m2Loc.position.magnitude, 2) * gravityConstant;

    return force;
}

i.mass.AddForce(i.pos.position * -1 * Equations.Gravity(i.pos, planetMass, i.mass, gravity), ForceMode.Force);

The pink is the satellite’s trail. The gravitational equation is used F=(G*M1*M2)/R^2, except G is 1. The Earth has a mass of 1000, the ship with a mass of 10. Force is applied to the ship using Rigidbody.AddForce every frame under FixedUpdate(). The entire script.

EDIT:

I let the simulation run for a bit and I see that the orbits are NOT random, because A: The orbits never exceed or go below a certain altitude and B: The orbits NEVER deviate from their inclination.

Still need an answer, though.

Clip of wonky orbit
I made a rudimentary (I'm still extremely proud of it) orbital sim. The satellite does not go around the planet on the second pass the same way it went around on the first pass. Its periapsis is constantly moving, not unlike orbital precession. I would think it actually is orbital precession, but there are no other orbital bodies in the simulation. The satellite exerts no force back on the Earth. Is it a problem with my code or is this a real phenomenon? The code below are the only lines that manage the orbital physics.

public static float Gravity(Transform m2Loc, Rigidbody m1Mass, Rigidbody m2Mass, float gravityConstant)
{

    float force = (m1Mass.mass * m2Mass.mass) / MathF.Pow(m2Loc.position.magnitude, 2) * gravityConstant;

    return force;
}

i.mass.AddForce(i.pos.position * -1 * Equations.Gravity(i.pos, planetMass, i.mass, gravity), ForceMode.Force);

The pink is the satellite’s trail. The gravitational equation is used F=(G*M1*M2)/R^2, except G is 1. The Earth has a mass of 1000, the ship with a mass of 10. Force is applied to the ship using Rigidbody.AddForce every frame under FixedUpdate(). The entire script.

EDIT:

I let the simulation run for a bit and I see that the orbits are NOT random, because A: The orbits never exceed or go below a certain altitude and B: The orbits NEVER deviate from their inclination.

Still need an answer, though.

Share Improve this question edited Feb 22 at 2:38 Privvet asked Feb 21 at 5:09 PrivvetPrivvet 214 bronze badges 7
  • 7 IMHO Instead of approximating the force, you should define a function that returns the position based on the current time. Otherwise you'll be accumulating errors with your model. – Jeremy Lakeman Commented Feb 21 at 5:26
  • 1 The physical calculations are discrete, the accuracy depends on how small the deltatime setting is. – shingo Commented Feb 21 at 5:40
  • @JeremyLakeman, I opted for using Unity's already used physics system to make my own gravity because I thought that using a system that was already there would be more accurate and more efficient. I even already had a system that you suggest implemented. I used the Transforms of the objects to get and set their positions based on similar equations. I did in both 2D (Pygame) and 3D (Unity) pastebin/Y0NVQwhm. Lines 159-166 told gravity when and how to act. Lines 128-138 managed the forces of gravity. I translated this to 3D for Unity, but I deleted it in favor of the current system. – Privvet Commented Feb 21 at 5:50
  • @JeremyLakeman, I decided against this because it requires my scripts to keep track of and manipulate a bunch of data, which Unity is already doing. And it is a LOT of code compared to the current system, which will make it harder to implement features in the future (I'm making a game that uses realistic orbital physics and other physics). Do you really think using Unity's physics is leading to these orbital errors? Do you think that it won't be that much more demanding on machines? – Privvet Commented Feb 21 at 5:55
  • 1 In general the "normal" physics engines used in Game Engines are optimized for performance .. in exchange of less precision! Unity is all float based so that's already one huge source of inaccuracies (no clue what the physics engine uses internally though I would hope double) ..And then as mentioned physics update only happens on fixed intervals .. which you can try to reduce of course. – derHugo Commented Feb 21 at 8:38
 |  Show 2 more comments

1 Answer 1

Reset to default 0

It seems to be because two variables for the gravity equation that I used (G = 1, M1 = 1000) were not best for the scale of my simulation.

I'm no orbital physicist, so I can't explain WHY this fixed it exactly, but I changed the Gravitational Constant G to 5 * 10^-3 and the mass of the Earth to 1 * 10^9. I guess this setup more closely resembled real life scales, so gravity needed less time to act correctly? This change led to objects' perigees and apogees staying closer to their original position for longer; less ridiculous orbital precession.

There's this value called the Standard Gravitational Parameter (SGP). It's defined as G(M1 + M2). Plugging in real life values, this number comes out to 3.986 * 10^14. Using my original values, it came out to 1000. I then thought that manipulating my G and M1 so that it is closer to the real SGP would result in basically zero orbital precession but deviating from my fixed SGP of 5 * 10^6 in either direction led to more orbital precession.

So, that answers my question, but I do not understand the mathematical reasoning.

本文标签: cWhy does this orbiting object not perfectly repeat its orbitStack Overflow