🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Gravitational acceleration calculation

Started by
143 comments, last by taby 1 year, 10 months ago

@felix.speagel The mass doesn't drop to zere but the net acceleration caused by the mass of the earth all around you drops to zero. edit. Also thank you so much for that video. I'll check it out.

None

Advertisement

felix.speagel said:
You are right, but don't forget one thing: in the formula for the force of gravity there is also a mass. As a ball approaches the middle point the mass that's attracting the ball also drops to zero. In the middle point there is simply nothing that makes a gravity field.

Then the mass cancels itself out, because it ‘comes form all directions’?

taby said:
Actually, gravitation inside of planet is measured by multiplying by r, not dividing by r squared. https://profoundphysics.com/gravity-underground/​ It goes to zero because there is a balance in the forces.

So yes.
But then my hack is not needed. I can prevent explosions using correct physics:

		static bool testSimplePlanets = 0; ImGui::Checkbox("testSimplePlanets", &testSimplePlanets);
		if (testSimplePlanets)
		{
			constexpr int count = 10;
			struct Planet
			{
				sVec3 com = sVec3(0);
				sVec3 vel = sVec3(0);
				sVec3 force = sVec3(0);
				float mass = 1;
				float radius = 1;
			};
			
			static Planet planets[count];

			auto CalcPlanetRadius = [](const float mass, const float density = 1.f)
			{
				float vol = mass * density;
				float radius = pow (vol * 3.f / (4.f * float(PI)), 1.f/3.f);
				return radius;
			};

			// generate random planets (this code runs only once)
			static bool init = true;
			if (init || ImGui::Button("Reset Simulation"))
			{
				int k = 0;
				auto rnd = [&](float vMin, float vMax)
				{
					float f = PRN::randF(k++);
					return (f * vMax-vMin) + vMin;
				};

				for (int i=0; i<count; i++)
				{
					planets[i].com = sVec3(rnd(-1,1), rnd(-1,1), rnd(-1,1));
					planets[i].vel = sVec3(0);//sVec3(rnd(-1,1), rnd(-1,1), rnd(-1,1)) * 0.001f;//
					planets[i].force = sVec3(0);
					planets[i].mass = rnd(0.001f, 0.001f);
					planets[i].radius = CalcPlanetRadius(planets[i].mass);
				}

				init = false;
			}


			// function to update forces of a pair of planets
			auto GravityPair = [&](Planet &bodyI, Planet &bodyJ)
			{
				const float G = 1.f; // some number to relate your mass metric to gravity. i just assume we use a mass unit so G becoems 1.

				sVec3 diff = bodyJ.com - bodyI.com;

				float dist = diff.Length();
				
				if (dist > 0.f)
				{
					// simple model - too lazy to read up details from https://profoundphysics.com/gravity-underground/
					float massI = bodyI.mass;
					if (dist < bodyI.radius) massI *= dist / bodyI.radius;
					float massJ = bodyJ.mass;
					if (dist < bodyJ.radius) massJ *= dist / bodyJ.radius;


					float force = G * (massI * massJ) / (dist * dist);

					sVec3 direction = diff / dist;
					bodyI.force += direction * force;
					bodyJ.force -= direction * force;
				}
			};

			static float timestep = 0.001f; 
			ImGui::SliderFloat("timestep", &timestep, 0.0002f, 0.005f, "%.5f");


			// update (this code runs once every frame)

			// accumulate forces
			for (int i=0; i<count-1; i++)
			for (int j=i+1; j<count; j++)
				GravityPair(planets[i], planets[j]); 

			// integrate 
			for (int i=0; i<count; i++)
			{
				Planet &p = planets[i];

				sVec3 acc = p.force / p.mass;
				p.vel += acc * timestep;
				p.com += p.vel * timestep;

				p.force = sVec3(0); // reset accumulation
			}

			// render
			for (int i=0; i<count; i++)
			{
				const Planet &p = planets[i];
				RenderCircle (p.radius, p.com, sVec3(0), 1,1,1);
			}
		}

It works. Simulation is stable.

Thanks guys. My understanding of the universe has increased. : )

I think that it's important to reiterate that the shell theorem states that the net acceleration is zero inside of a shell.

The key word is net.

There is still gravitational time dilation!

Can someone go on to Physics Stack exchange and ask what the time dilation is inside of a shell. I am banned lol

I did read about that, and discovered the Schwarzschild radius, which also defines the event horizon of black holes.

It follows, you have to calculate the square root of a negative number to calculate time dilation below that.
Which means, there is an imaginary white space of increasing equilibrium, peace and harmony towards the center, where Jimi still rocks forever. <: )

@joej , I feel less stupid now, lol. I guess this is common problem, lol. However, as we found his explanation of why isn't totally correct. https://youtu.be/OAcXnzRNiCY?t=1101

None

Ha - he even just clamps the distance. What an amateur… knowing nothing about the universe. \:D/

Hot Dog!!! https://codepen.io/jonny-b/pen/QWQqVXe

None

I see you have borrowed his hacks.

That's fine, but now we need more particles and dimensions… ; )

@taby && @joej - Well, I've made a bunch of progress. I decided to use the p5.js library which took some of the burden from me to calculate force and acceleration as vectors, as well as some other syntactic sugar and functions!! Anywhoo, here's the progress. https://codepen.io/jonny-b/pen/NWyaJNd?editors=1010​​ . Something still seems off though. There is a black hole star that has more mass than the others combined and it doesn't interact in the way I thought.

None

This topic is closed to new replies.

Advertisement