# How to Simulate Radial Gravity Without Using Physics Engine Part 1/2 When i played angry birds space first time, i really loved the concept. I didn’t think about how Rovio made it because i knew they used physics engine for it and i really didn’t know much things about using physics engines. Today i still don’t know much 🙂 so i challenged myself to simulate radial gravity without using any physics engine. Aaand challenge accepted.

#### Understanding Vectors in 2D

To achieve my goal i used basic vector math. I think, in most cases, using vector math is easier than using trigonometric functions. If you don’t know anything about vectors don’t worry i will tell you enough about basics to understand this tutorial. Ok let’s start.

You can think that a vector in games is very good way of giving information about direction and magnitude at the same time. In 2d games you need 2 points to define a vector. Sometimes you can give only one point to define a vector for example A(2,3) but actually you always have second hidden point which is origin O(0,0).

If you have point A(x1,y1) and point B(x2,y2) you can find the vector from A to B by subctracting x1 from x2 and y1 from y2. This is important, if you want to find the vector from A to B you should subtract coordinates of A from coordinates of B but if you want to find the vector from B  to A you should do the opposite. Although vector A to B and vector B to A have the same magnitude they have different directions and that makes them totally different vectors. You can find magnitude of an vector by using pythagorean theorem. You can multiply a vector with a scalar value. This multiplication does not change the vector’s direction but change it’s magnitude. You can add two or more vectors together. Now anyone who doesn’t know anything about vectors has enough information about them to follow this tutorial.

#### Using Vectors in Our Code

Ok let’s look at the image below. Red circle in the image is our moving object. It’s velocity is defined as the vector V1(x1,y1). That means if we want to move our object with the direction and magnitude of V1, we should add x1 to circle’s x position and y1 to circle’s y position at every frame.

When our red circle is in the gravitational field of gray circle which is shown with the blue dashed circle, then red circle is affected by the gravitational force. The direction of this force is always toward the center of gray circle.

Because of this force there is another velocity for our circle which is V2(x2,y2). Remember the vector addition above. By using addition we can find the final velocity of red circle. So in our code we first should check if red circle is in gravitational field or not. If it is then we should find V2, add it to V1 to find V3 and move our red circle with the direction and magnitude of V3. Value of V1 is up to us. We can give any value to V1 and we can find V3 by adding V1 and V2. So the question is how we can find the V2 ?

Look at the image below. We have two point. Position of red circle and position of gray circle. We know how to find the vector from one point to another. So if we subtract position of red circle from the position of gray circle then we have very close to find our V2. I said very close because we need  a few more calculation. We should divide V by it’s magnitude (Remember we can multiply or divide a vector by a scalar). This calculation gives us the unit vector. Unit vector’s direction is the same with the V and it’s magnitude is always 1. After you have the unit vector of gravitational force then you can multiply it with any value you want to find your V2. The bigger number you multiply the bigger gravitational  force you have.

Now we have enough information about vectors and we know the logic of simulating radial gravity. In the second part of this tutorial we will use what we know so far and write some code.