E
E
elleremo2018-10-07 23:05:44
JavaScript
elleremo, 2018-10-07 23:05:44

How to do exact calculation of gravity physics on javascript in canvas by Verlet method?

Made a simple "simulation" of gravity https://elleremo.github.io/ . As I understood later, I used a not very accurate Euler algorithm - at close distances, the planets accelerate like hell and fly away into the sunset. Here's what I don't understand:
1. Do I need to use and consider dt in RequestAnimationFrame?
2. Is it necessary and when to use dt in the Verlet algorithm ?
3. I tried to replace physics with the Verlet algorithm (from guides where they simply count without speed and without dt ), but the result is the same
4. In short, I don’t understand how to make accurate physics without errors

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Roman, 2018-10-08
@lastuniverse

elleremo , the acceleration that planets get when flying near other planets is not a mistake!!!
This effect is called " gravity maneuver to accelerate an object " or " gravity sling "
Didn't do "accurate simulation" of gravity. Followed the path:
1. each object has a mass equal to the volume of the object multiplied by the density of the object
2. each object affects each dynamic object
3. the influence is calculated step by step according to the formula (almost):
V = sum(F); // total force vector
F = k*M/D; // strength of influence (calculated for each object that has an impact on the current one)
M - mass of the object
D - squared distance between the centers of mass of objects
k - coefficient for fitting velocities (acts as a gravitational constant)
Calculation algorithm (step by step):
1. For each dynamic object, new coordinates are calculated (the sum of the current coordinates and the velocity vector calculated for previous iteration of the simulation loop)
2. Each dynamic object is checked against each object for collision. If the distance between the centers of 2 compared objects is less than the sum of their radii, then a merging occurs. If static and dynamic objects are combined, then the dynamic one is removed and its mass is added to the static one with recalculation of density, radius and volume. If 2 dynamic objects are combined, then the mass of the second is added to the first one with recalculation of density, radius and volume, coordinates and velocity vectors are recalculated as a weighted sum of coordinates and velocity vectors of both objects. After the second object is removed.
3. For each dynamic object, the sum of the vectors of the attraction force of all objects is calculated. Then this sum and the velocity vector of the current object are summed
4. Transition to a new iteration of the simulation loop.
The entire implementation of the calculation is here (at the end of the script). All formulas are placed in the Calc class.
video
demo demo (scaled with the mouse wheel, dragged with LMB)
Added planet merging on collision (masses are summed, velocity vectors are summed, position is moved to the center of mass)
Added sources (automatically generate planets)
Added separate buttons to start and stop sources
Added settings:
Improved the mechanism for calculating the influence of gravity, now each object's mass is calculated based on volume and density.
Introduced a change in the process of generating new planets, now they are put on the sheet with the calculated first cosmic velocity relative to the central static object.
I set static objects to a higher density, which significantly increased their mass (can be changed in the settings panel)
I set dynamic objects to a lower density, which significantly reduced their mass and mutual influence (can be changed in the settings panel)
5bbd22bc10be0483291405.png

A
Andrey Fedoseev, 2018-10-08
@itlen

I can be wrong, but in my opinion you are going to solve the three-body problem ) You have static planets that do not exist in nature, so you will not build real physics anymore. And emulation ... as already advised, select the parameters. Restrictions on accelerations during a gravitational maneuver, calculation of gravity not only of the nearest static planet, but also of their ensemble. This is it.

P
profesor08, 2018-10-08
@profesor08

Well, it works fine, doesn't it? Play with the parameters of the planets, arrange them correctly so that they do not fly away, give them the correct speed, the correct vector, the correct mass, the correct radius of the orbit. For example, take our solar system to scale.
And they fly away because the starting direction of the planet's motion is directed towards the attractor, and while it flies towards it, it picks up speed, and at some point its speed will be sufficient to break out of the gravitational field of the attractor and fly away.
In short, you have a physical model, you just need to choose the parameters of the objects.

G
GreatRash, 2018-10-08
@GreatRash

codeflow.org/entries/2010/aug/28/integration-by-ex...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question