Answer:
Step-by-step explanation:
Probably the most famous force of all is gravity. We humans on earth think of gravity as an apple hitting Isaac Newton on the head. Gravity means that stuff falls down. But this is only our experience of gravity. In truth, just as the earth pulls the apple towards it due to a gravitational force, the apple pulls the earth as well. The thing is, the earth is just so massive that it overwhelms all the gravity interactions of every other object on the planet. Every object with mass exerts a gravitational force on every other object. And there is a formula for calculating the strengths of these forces, as depicted in the diagram below:
Diagram of gravitational forces between two spheres
Diagram of gravitational forces between two spheres
Let’s examine this formula a bit more closely.
F refers to the gravitational force, the vector we ultimately want to compute and pass into our applyForce() function.
G is the universal gravitational constant, which in our world equals 6.67428 x 10^-11 meters cubed per kilogram per second squared. This is a pretty important number if your name is Isaac Newton or Albert Einstein. It’s not an important number if you are a ProcessingJS programmer. Again, it’s a constant that we can use to make the forces in our world weaker or stronger. Just making it equal to one and ignoring it isn’t such a terrible choice either.
m_1m
1
m, start subscript, 1, end subscript and m_2m
2
m, start subscript, 2, end subscript are the masses of objects 1 and 2. As we saw with Newton’s second law (\vec{F} = M\vec{A}
F
=M
A
F, with, vector, on top, equals, M, A, with, vector, on top), mass is also something we could choose to ignore. After all, shapes drawn on the screen don’t actually have a physical mass. However, if we keep these values, we can create more interesting simulations in which “bigger” objects exert a stronger gravitational force than smaller ones.
\hat{r}
r
^
r, with, hat, on top refers to the unit vector pointing from object 1 to object 2. As we’ll see in a moment, we can compute this direction vector by subtracting the location of one object from the other.
r^2r
2
r, squared refers to the distance between the two objects squared. Let’s take a moment to think about this a bit more. With everything on the top of the formula—G, m_1m
1
m, start subscript, 1, end subscript, m_2m
2
m, start subscript, 2, end subscript—the bigger its value, the stronger the force. Big mass, big force. Big G, big force. Now, when we divide by something, we have the opposite. The strength of the force is inversely proportional to the distance squared. The farther away an object is, the weaker the force; the closer, the stronger.
Hopefully by now the formula makes some sense to us. We’ve looked at a diagram and dissected the individual components of the formula. Now it’s time to figure out how we translate the math into ProcessingJS code. Let’s make the following assumptions.
We have two objects, and:
Each object has a PVector location: location1 and location2.
Each object has a numeric mass: mass1 and mass2.
There is a numeric variable G for the universal gravitational constant.
Given these assumptions, we want to compute a PVector force, the force of gravity. We’ll do it in two parts. First, we’ll compute the direction of the force \hat{r}
r
^
r, with, hat, on top in the formula above. Second, we’ll calculate the strength of the force according to the masses and distance.
Remember when we figured out how to have an object accelerate towards the mouse? We're going to use the same logic.