|
Gravity is used a lot in video games these days. You may have seen gravity in games such as street fighter, super mario brothers, sonic, etc…. But, what is gravity? You ask. Well, to put it in simple words, gravity is the force that pulls two objects together. This is the result on Newton's discovery in the seventeenth century. He discovered that the same interaction that makes an apple fall out of a tree also keeps the planets in their orbits around the sun. This is what gave rise to Newton's law of gravitation, which he published in 1687.
Newton's Law of Gravitation
The best example of gravitation attraction that you are most probably famliarised with is your weight, the force that attracts you toward the earth.
Newton's law of gravitation may be stated as follows:
| Every particle of matter in the universe attracts every other particle with a force that is directly proportional to the product of the masses of the particles and inversely proportional to the square of the distance between them. |
This is how the equation looks like:
Fg = Gm1m2 ⁄ r2 (law of gravitation).
Fg is the magnitude of the gravitational force on either particle, m1 nad m2 are their masses, r is the distance between them, and G is the fundamental physical constant called the gravitational constant. The numerical value of G depends on the system of units used.
Note: Do not confuse the symbols g and G, g is the acceleration due to gravity, which relates weight w of a body with its mass m: w = mg, whereas G is the gravitational constant and relates to gravitational force between two bodies to their masses and the distance between them.
The figure below shows two particles separated by a distance r, and gravitational forces they exert on each other. The magnitude of the forces is the same for both particles even if the masses are different.
When implementing gravity in video games, we don't use the formula above in practice. Well, most of the time you will use another formula that I'll provide later. But the formula may be useful in some situations. Have a look at the queing behaviour found in the steering behaviours link under the artificial intelligence section found on the left−hand menu.
Gravity has an effect on the y−axis only (well, this is true if we are working in 2−D. If working in 3−D, you may find the z−axis to be the up direction.) To model gravity in a computer game, we just add acceleration to the the vertical velocity of our object. This is sufficient to work perfectly.
Let's suppose that an object has an initial velocity, in both x and y directions, which we call vx and vy, respectively. To apply a gravity field to the object, we would add acceleration only to the y−component of velocity (vy) each game cycle. This is how it looks like:
vx = vx + 0 (we do not add gravity on the x−axis)
vy = vy + gravity
where gravity is the gravitational strength. You can choose any value of gravity. That is, you choose a value that will look realistic.
In a game loop, you would use it as follows:
foreach step
vx = vx + 0 ⁄⁄ we do not increment the velocity in x
vy = vy + gravity
px = px + vx ⁄⁄ move the object in x
py = py + vy ⁄⁄ move the object in y
draw_object(px, py) ⁄⁄ draw the object on the screen
Using only the above formula in your game will make your object bounce without stoping. Have a look at the friction tutorial, in which a provide an example of gravity and friction.
That is all there is to gravity. If you have any queries about this tutorial, you can drop me an email.
Best Regards
Fidel
|