Today, my eye caught an article on the Quora website. People are seriously discussing and wondering about this. As you can see in the Article Title, "The world is round. So why don't we fall into the universe?" Today, I will explain and show you the algorithm of Newton's law of universal gravitation in practice.
In 1687, English mathematician Isaac Newton published Principia, in which he hypothesized the inverse square law of universal gravitation. It all started when an apple fell on Newton's head. This is actually the underlying theory "Every particle in the universe attracts another particle with a force directly proportional to the product of their masses and inversely proportional to the square of the distance between them."
This formula and theory remain valid today. The gravitational force between planets is also compatible with Newton's theory. Can you see from here how perfect the universe is and how detailed it is based on mathematical formulas ? You can't even imagine what would happen if the gravitational pull of two planets was slightly stronger than the other.
According to this formula, I will simulate you how the law of gravity works in real life in the Unity game engine. In order to do this , ı will create a 2d project in Unity. First, let's make the arrangements for the earth together. I drag the earth of sprite on my scene and add Rigidbody2d and a Collider.Rigidbody settings of the earth object should be as in the image. If you noticed, I set the gravity to 0 because I will adjust it myself according to Newton's theory with the formula.
After dragging my Newton character onto the scene, I add a Rigidbody and BoxCollider to him. I set the gravitational force to 0 because I will calculate according to the formula. But I leave the directions active so it moves. I will not use G in the formula because it is a very small value.
Let's get into coding then.I am creating a script named Planet. This will be the script of our planets. Such as Earth, Mars and Moon etc. There will be 2 Rigidbodies in this script. The first is the rigidbody of the planet, the other is the rigidbody of our character, Newton. Now, let's create a method in this script that applies the gravitational force that the planet applies to the character. Let's create a method called forceGravity. We keep the vector difference between the character and the planet in a variable.We use the magnitude method to get this vector difference and the distance between two objects.(Planet and Character)
We use Newton's law of gravity to measure the exact magnitude of the force we will apply here. We multiply the weight of the planet by the weight of the character and divide by the square of the distance. Then, since force is a vector quantity, we create a vector variable and determine the direction of the character.We multiply the direction we determined by the force we calculated above so that the planet can apply gravity to the character. We put a - sign so that the Upward side of the character looks at the opposite side of the planet instead of looking at it.
In this way, when the force applied to the character is downward, the character's head will look upward. Let's imagine it this way, you will be able to see it when we simulate it. While the character is walking around the world, if the character is below the world, the character's head must be down because the force pulling him towards himself will be upward. Just like in the real world.
Finally, we call this created function in FixedUpdate. The reason we use FixedUpdate is that physical operations work more smoothly here. We add our planet script to our earth object and drag the character here to get its rigidbody. When we start the game engine, when we hold the character and release it, the earth immediately pulls towards itself. Additionally, if we increase the weight of the world, the character will fall down faster as the force value from the rigidbody section increases according to the Newton formula we calculated.
Let's create a script called PlayerContoller so that our character can move on the planets. I will not explain the code of how we move the character here. If you wish, you can download and examine the code later. You can add the planet script to other planets and see how Newton's law works.If you want to simulate the project yourself, it will be enough to import the package into Unity from the link below. All scripts and assets are included in the package. See you later ..