Tuesday, October 27, 2015

Capstone Post 1: Programming Gravity

Simple forms of Newtonian gravity are very simple to emulate in code. We will take a look at how to achieve the effect of bodies moving about a perfectly circular orbit around a fixed point in space. The essence of this code will work in any environment; I will be using Unity C# to demonstrate the concept.

To start, let's create a new Unity project and set it to 2D mode. Then let's toss some planets and a sun into our game world.



Next, let's attach a script to our planets that will give them this gravity behavior. Let's also attach a rigidbody that, ironically, does not use gravity. In the editor, simply turn off gravity for our rigidbody. We don't need predefined gravity; we are going to write our own!

























Now that we've finished setting up our scene, let's give these planets some Newtonian physics! To perform our calculations, we're going to need some information about our scene. We can start by defining the following variables in our script:
























A good practice with Unity C# is to define your components as member data to avoid littering your code with GetComponent calls. In the editor, we can simply grab our sun GameObject and drop it into the public variable slot for sun, and set the sun's mass and G to be whatever we would like.

Newton's actual gravitational constant (G) is approximately equal to 6.67 x 10^-11, but we can define our own G for this simulation (and I would recommend you not actually use that value unless you are placing your planets millions of miles away in Unity, which will cause you floating point errors). 

Let's step away from our code for a minute and examine the physics we are attempting to emulate. Newton defines the force of gravity between two bodies as follows:


where m1 is the mass of one body, m2 is the mass of the other body, and r is the distance between their centers of mass. 

Often, with this formula, one mass is so large in comparison to the other, that we can remove the smaller mass from the equation altogether. We will be doing this in our code. Let's start our calculation by finding the force of gravity between our two objects, in precisely the same way Newton's formula says to:












Here, we do a simple distance calculation to find r. We then use this distance in our Fg calculation, removing the mass of the planet because it is negligible in our system. This in reality only gives us the magnitude of force, a directionless value.

So, we must apply this force in the direction towards the sun. We do this by subtracting the position of the sun from the position of the planet of the sun, normalizing it so we can get our x and y components to be between 0 and 1, and then applying our force magnitude into our force vector.

 If we were to apply this force to our rigidbody, the planets would plummet into the sun quite quickly (distance squared). So, we must apply a velocity perpendicular to the direction of Fg in order to retain orbit. In essence, we want our planets to fall into the sun due to the force of gravity, but maintain a perpendicular velocity which makes it miss.

To achieve this, we can use the following simple formula to find our magnitude:


And finally, some code:


And that's it! We simply plug in our values, give that velocity magnitude a direction and apply Fg and our velocity to the rigidbody attached to the planet. The -90 degrees is so we apply this velocity perpendicular to the direction of our Fg vector.

I implemented this Newtonian gravity into Serpent Shadows, and frankly it was more because I felt like having fun with it. Spoiler alert, about the same effect could be achieved by creating a new transform at the position of the sun, child-ing the planet to this transform, and rotating the transform. But this only took about an hour, and I had a bunch of residual physics excitement from my physics class that day.

Programming gravity is quite interesting, especially when you attempt to put lots of bodies with varying masses and velocities, however the principles of Newtonian motion can apply in a virtual space. If nothing more, by doing so we can, in a sense, write proofs (in a theoretical sense) of Newton's gravitational formula in a virtual environment which can emulate the impossible perfection that comes from elimination of real-world variables.

No comments:

Post a Comment