# Help Scaling Gravity Simulation

• B
• SacCno
SacCno
TL;DR Summary
I'm trying to scale the solar system down to around 1:1,000,000,000 for a game I'm making, need help with scaling formulae, forces, velocities etc.
I'm trying to make a 2D game on Unity similar to Universe Sandbox 2. I currently am working with a star and planet, both with roughly the masses of the sun and earth respectively for comparison and at a distance of 31,000,000km.

My current model uses Newton's formula for gravitational force (F = GM1M2 / R2) and then dividing it by the planet's mass to get acceleration towards the star due to gravity. I then use the formula (v = √GMsun/R) to get the orbital velocity. My full process is as follows.
1. Calculate force.
2. Divide by planet's mass to get acceleration towards the star.
3. Calculate orbital velocity.
4. Divide both by 1,000,000,000 to get scaled down values.
5. Multiply both by 525,600 (minutes in a year) to speed up simulation to one year per minute.
I then apply both initial orbital velocity and add acceleration towards the star every frame. These are my input values:
• Msun = 2e30kg
• Mearth = 6e24kg
• R = 31units * 1,000,000,000 = 31,000,000,000m
• G = 0.0000000000667
The values I get from the above steps are:
• Unscaled Orbital Velocity = 65,598.97718m/s
• Scaled Orbital Velocity = 34.4788224m/s = 34.4788224units/s
• Unscaled Force = 8.32882414e23N
• Scaled Force = 4.37762997e20N
• Unscaled Acceleration = 0.1388137357m/s/s
• Scaled Acceleration = 7.29604995e-5m/s/s = 7.29604995e-5units/s/s
The important values there are the scaled orbital velocity (34.48m/s) and scaled acceleration towards the star (7.296e-5m/s/s). Given that the earth has a velocity of roughly 30km/s and the acceleration towards the sun is around 0.006m/s/s the two values I got and the difference between them didn't alarm me too much.

My issue came when I started the simulation and the ≈30m/s of initial velocity sent the planet shooting off into the cosmos. I've obviously made a big mistake somewhere in my calculations but after a few days of trial and error I still can't figure it out. I asked on the Unity forums and got some really handy tips for my simulation as a whole but nothing seemed to address my issue of how to scale my physics. I suspect that my scaling needs to be done inside the formulae to some degree but I've no idea where to start if that is the case.

That website's really cool! I did initially use some of Kepler's laws as a stopgap in the beginning with the planet moving in a simple orbit around the sun, this was very linear and not dynamic at all and I'm not too sure how I could make a dynamic system using Kepler, especially if I want the player to be able to throw other planets/stars/black holes into the mix wherever they want, if you have an idea I'd be more than happy to hear!

I'm kinda laying the track as I go, but unless you think a force model is a really bad approximation I think I'll stick to it.

I'm not too concerned about the accurate scaled size of the planet's right now, down the line maybe, but once they're smaller than the sun I'll be happy for now, currently the earth is technically 1,000,000km in diameter I'm just trying to wrap my head around why the orbital velocity and gravitational acceleration are so out of whack. Again I'm pretty sure I'm being too simplistic with my scaling, but I don't know in what way.

"Debug my code for me" is a pretty big ask.
Doing so without the code is an even bigger ask.

I don't understand why you are scaling at all. If it's for a sensible display, do the calculation and then plot the code at some smaller scale, e.g. plot(x/1000000000, y/1000000000).

Motore
I don't understand why you are scaling at all. If it's for a sensible display, do the calculation and then plot the code at some smaller scale, e.g. plot(x/1000000000, y/1000000000).
this^^

SacCno said:
My issue came when I started the simulation and the ≈30m/s of initial velocity sent the planet shooting off into the cosmos. I've obviously made a big mistake somewhere in my calculations but after a few days of trial and error I still can't figure it out.
This may or may not be an issue with scaling. You might need a different integrator (the code that calculates the new velocities and positions of the objects). A 'basic' integrator won't cut it. You need a symplectic integrator, which is one that approximately conserves energy. Examples include the leap-frog and modified Euler integrators.

See here: https://carma.astro.umd.edu/nemo/pitp/papers/gd2_s3.4.pdf
Leapfrog example with code: https://www.johndcook.com/blog/2020/07/13/leapfrog-integrator/
Another paper with examples of many different integrators: https://www.researchgate.net/public..._Techniques_in_Orbital_Mechanics_Applications (look into the Verlet integrator)

If you need help understanding the algorithms and converting them to code, let me know. I had to do the same thing earlier this year in a simulation I created.

vanhees71
Scaling down distances by a factor of 1 billion would give a scaled down distance from the earth to the sun about 500 feet. Is that what you really want?

## What is a gravity simulation?

A gravity simulation is a computational model used to replicate the effects of gravitational forces on objects. This can range from simple two-body problems to complex systems involving many bodies, such as planetary systems, galaxies, or even clusters of galaxies.

## Why is scaling important in gravity simulations?

Scaling is crucial in gravity simulations to ensure that the computations remain efficient and manageable. As the number of objects increases, the computational complexity can grow exponentially. Effective scaling techniques help to optimize performance and resource usage, making it possible to simulate larger and more complex systems.

## How can I improve the performance of my gravity simulation?

Improving performance can be achieved through several methods: using more efficient algorithms like the Barnes-Hut algorithm, parallelizing computations across multiple processors or GPUs, and optimizing data structures to reduce computational overhead. Additionally, simplifying the physical model where possible can also lead to performance gains.

## What are common challenges in scaling gravity simulations?

Common challenges include handling the increased computational load, managing numerical stability, ensuring accuracy over long simulation times, and dealing with the limitations of hardware resources. Balancing these factors requires careful consideration of algorithm design and implementation.

## Are there any tools or libraries that can help with scaling gravity simulations?

Yes, there are several tools and libraries designed to aid in scaling gravity simulations. Examples include GADGET, a widely-used code for cosmological simulations, and AMUSE (Astrophysical Multipurpose Software Environment), which provides a framework for multi-physics simulations. These tools often come with built-in scaling techniques and optimizations to help manage large-scale simulations.

Replies
86
Views
5K
Replies
15
Views
2K
Replies
4
Views
2K
Replies
67
Views
4K
Replies
1
Views
653
Replies
42
Views
1K
Replies
18
Views
1K
Replies
33
Views
2K
Replies
7
Views
4K
Replies
2
Views
1K