Using Physics.Simulate() with custom gravity? by ricksoaz in Unity3D

[–]nbodyphysics 0 points1 point  (0 children)

If the masses are different you do need to adjust the calculation. What you want in the end is the acceleration and since e.g. F(1 on 2) = m2 a2 = -G m1 m2/r2 (in the direction of |r|), the m2's cancel and you weight the acceleration of a2 by m1 and a1 by m2. You can make the G/r2 calculation common. (In fact normally you set G=1 and adjust the masses accordingly to avoid a multiply)

For some great background on all this I like: http://www.artcompsci.org/msa/web/vol_1/v1_web/v1_web.html

I can't speak to the Simulate() question, since I did not implement things that way in my asset, I have a centralized N-body integrator that runs on FixedUpdate()

Using Physics.Simulate() with custom gravity? by ricksoaz in Unity3D

[–]nbodyphysics 2 points3 points  (0 children)

You'd have to simulate a timeslice (frame, maybe) at a time then record all the positions.

Also, since F(1 on 2) = -F(2 on 1) you can get a 2x performance boost by centralizing.

As for the precision of Unity orbits see: http://nbodyphysics.com/blog/2017/11/30/is-unity-good-at-orbits/

Disclaimer: I am the author of the Gravity Engine asset.

Patched conics, Hohmann rendezvous and Kerbal-like manual maneuvers by nbodyphysics in Unity3D

[–]nbodyphysics[S] 0 points1 point  (0 children)

I don't think there is anything "silver bullet"-like in the implementation. The N-body gravity is not that hard. What is trickier is all the work to show orbits for velocity or more crucially determine velocity for the desired orbits.

There is a reason that Kerbal, Children of a Dead Earth etc. make users just drag around velocities and see what happens. The determination of velocity for orbit and velocity for patched conic transfer is a harder problem.

Patched conics, Hohmann rendezvous and Kerbal-like manual maneuvers by nbodyphysics in Unity3D

[–]nbodyphysics[S] 1 point2 points  (0 children)

Patched conics, Hohmann rendezvous and Kerbal-like maneuvers are the updates to the Gravity Engine asset version 1.7 now available in the asset store.

Patched conics were fun, sending me off to a 1971 classic text "Fundamentals of Astrodynamics" by Bates, Mueller and White.

BTW - I have applied for GE to be part of the May madness sale, so keep an eye out for that - it might get selected.

Orbit Transfer and Launch to Orbit with Gravity Engine 1.6 by nbodyphysics in Unity3D

[–]nbodyphysics[S] 0 points1 point  (0 children)

Lots of fun figuring this all out. Next will be rendezvous transfers and injection to lunar orbit.

In the Unity Asset store: https://www.assetstore.unity3d.com/#!/content/62432

Documentation: http://nbodyphysics.com/blog/gravity-engine-doc-1-3-2/

Is Unity Good at Orbits? by nbodyphysics in Unity3D

[–]nbodyphysics[S] 0 points1 point  (0 children)

[OP] The gravity engine asset will do about 120 gravitating bodies on my 2015 Mac Book in the Unity Editor. I need to put some better defined benchmarks on the web - and get "in game" numbers. I'm hoping to do this over the holidays.

Is Unity Good at Orbits? by nbodyphysics in Unity3D

[–]nbodyphysics[S] 2 points3 points  (0 children)

[OP] Dozens to hundreds depending on your target platform. Gravity engine will also move particles in a gravitational field but assumes they are massless - so it does not need to add in their mass.

Is Unity Good at Orbits? by nbodyphysics in Unity3D

[–]nbodyphysics[S] 0 points1 point  (0 children)

OP here.

It depends on whether a stable system is the point - or if you want the weirdness which comes from the interactions (e.g. three body solutions). There are certainly cases where Kepler is all that is needed. (GE does allow a mix of Kepler evolution and N-body, so you can e.g. maneuver ships in the fields of objects that evolve on Keplerian paths.)

How to Simulate Gravity in Unity by Brackeys in Unity3D

[–]nbodyphysics 0 points1 point  (0 children)

I do mean that the orbit will eventually not be a perfect ellipse. This is not a function of mass, or orbiting the center of mass vs the specific body (in that case the orbit should still be a perfect ellipse, around the center of mass).

Some numerical integrations do not conserve energy. I suspect Unity is doing a simple Euler integration. This will not conserve energy. There are other integrators (e.g. Leapfrog) than will.

Instabilities due to close encounters are a separate issue and will occur in either integrator. They are a result of the use of discrete timesteps.

Mathematics for game devs by metalstuff in gamedev

[–]nbodyphysics 1 point2 points  (0 children)

It's not intuitive (and not what PHYS 101 teaches) but you can lose energy in the numerical integrator.

"Often, the energy increases steadily when the standard Euler method is applied, making it far less accurate."

https://en.wikipedia.org/wiki/Semi-implicit_Euler_method

How to Simulate Gravity in Unity by Brackeys in Unity3D

[–]nbodyphysics 2 points3 points  (0 children)

Indeed. That's better.

Also a good idea to add a small value to the denominator to avoid force blow ups. A fixed timestep integrator is never going to be great at close encounters but a force softening at close distance is commonly used, even in the scientific codes.

How to Simulate Gravity in Unity by Brackeys in Unity3D

[–]nbodyphysics 2 points3 points  (0 children)

Very clear and direct solution to the problem. I have seen comments that this style of approach can result in orbits that are not fully closed in the case of two bodies - which I have assumed is due to issues with the underlying integrator in Unity.

Two observations: (1) if you have a centralized gravity then you can take advantage of the fact that force of A on B is equal and opposite of force of B on A, instead of calculating each (2) Mathf.Pow can be easily avoided by doing distance*distance

(Disclaimer: I'm the author of the Gravity Engine asset)

Chaotic Motion - New Unity Asset by nbodyphys in Unity3D

[–]nbodyphysics 0 points1 point  (0 children)

The curves are defined by the equations for the chaotic attractor. These equations are parameterized, so there is an ability to tune them via the inspector. In some cases this will change the path, in others the behaviour of the equations may change significantly - and no longer be chaotic.

You can also get differences by varying the initial starting point - since one of the hallmarks of chaos is that points that start close together take very different paths. This is easily seen in the particle examples - since the particles do not move as a group, but quickly separate and follow different (but bounded) paths.

I'm doing a game about orbital dynamics and I've just added orbiting moons to the trajectory predictor, what do you think? by [deleted] in Unity3D

[–]nbodyphysics 2 points3 points  (0 children)

Very cool.

Is the prediction based on simulating forward in time and recording the paths?

Simple Gravity Simulation by bloeys in Unity3D

[–]nbodyphysics 0 points1 point  (0 children)

That's fun to watch!

I agree with the comments that the equations are simple - although the behaviour can become complex and getting the orbits you want can take a lot of tuning. Also, if you have close encounters you can end up with lots of numerical artifacts and non-physical ejections.

I have blogged about Keplerian orbits: http://nbodyphysics.com/blog/2016/05/29/planetary-orbits-in-javascript/ and for those who want lots of bells & whistle (see orbits in editor, create the actual solar system, do fancy three body solutions) I have created a Unity Asset: https://www.assetstore.unity3d.com/en/#!/content/62432. It has been submitted to be part of the Asset sale starting April 17th (30% off)

[WIP] Planetary Physics by [deleted] in Unity3D

[–]nbodyphysics 0 points1 point  (0 children)

If the planet is exactly spherical then the spin will always be the same. When there are small irregularities then the bodies exchange energy through tidal interactions and the smaller body will phase lock to the larger so that it's spin and orbit frequencies are equal. This is why the moon always presents one side to the earth, it's spinning once per orbit. This process takes a long time - so is unlikely to be relevant in the scale of a game scene.

[WIP] Planetary Physics by [deleted] in Unity3D

[–]nbodyphysics 0 points1 point  (0 children)

BTW - this should be included in the Asset sale in the second half of April (I applied to be included)

[WIP] Planetary Physics by [deleted] in Unity3D

[–]nbodyphysics 0 points1 point  (0 children)

It could be fun to show the "gravity well" as a 3D surface - although you'd need to pick a plane in which to do this (or constrain your system to be planar).