/u/7434365
/joeiddon

A Simulating of Gravity

run the simulation here

I'd just like to point out briefly that the simulation requires a certain degree of skill to actually place planets into orbits (right velocities etc.). For that reason, I hope to add URL encoding (as I did for the balls simulation) so that I can pre-setup the positions of the planets to make it simpler to see working. Anyway for now, good luck and enjoy!

This second simulation shares some aspects to my first balls one but instead of the focus on being the collision between balls, this simulation focuses on how balls with different masses interact through gravity.

The equation for gravity (SVG from wikipedia) is:

Where F is the force, m1 and m2 are the masses of the objects interactng, r is the distance between the centers of the masses and G is the gravitational constant.

In my script I applied this formula exactly, but changed the gravitational constant so that it is more suited to a simulation. Also, this is the equation to get the attractional force between the two masses, but in my simulation, what is needed is the acceleration...

I already know the balls' masses so I can just rearrange the equation f = ma to a = f /m and use that as the calculation.

The main section of interest in the code is where this calculation happens for the gravity. These two function can be seen below:

function gravitationalAccel(m1, m2, r){
	force = gConstant * ((m1 * m2) / Math.pow(r,2))
	return {a1: force/m1, a2: force/m2}
}

function applyGravity(){
	for (b1 = 0 ; b1 < noBalls ; b1 ++ ){
		for (b2 = b1 + 1 ; b2 < noBalls ; b2 ++){
			var distanceBetween = Math.sqrt((balls[b2].x - balls[b1].x) * (balls[b2].x - balls[b1].x) + (balls[b2].y - balls[b1].y) * (balls[b2].y - balls[b1].y))
			var xDistance = Math.abs(balls[b2].x - balls[b1].x)
			var yDistance = Math.abs(balls[b2].y - balls[b1].y)
			var secondsElapsed = refreshRate / 1000
			var diagAcc = gravitationalAccel(balls[b1].r, balls[b2].r, distanceBetween)
			
			if (balls[b1].x < balls[b2].x){	//b1 left of b2
				balls[b1].vx += diagAcc.v1 * (xDistance / distanceBetween) * secondsElapsed
				balls[b2].vx += diagAcc.v2 * (xDistance / distanceBetween) * secondsElapsed * -1
			} else {						//b1 right of b2
				balls[b1].vx += diagAcc.v1 * (xDistance / distanceBetween) * secondsElapsed * -1
				balls[b2].vx += diagAcc.v2 * (xDistance / distanceBetween) * secondsElapsed
			}
			
			if (balls[b1].y < balls[b2].y){	//b1 above b2
				balls[b1].vy += diagAcc.v1 * (yDistance / distanceBetween) * secondsElapsed
				balls[b2].vy += diagAcc.v2 * (yDistance / distanceBetween) * secondsElapsed * -1
			} else {						//b1 below b2
				balls[b1].vy += diagAcc.v1 * (yDistance / distanceBetween) * secondsElapsed * -1
				balls[b2].vy += diagAcc.v2 * (yDistance / distanceBetween) * secondsElapsed
			}
		}
	}
}

The full code can be found here on GitHub.