HTML and CSS Reference
In-Depth Information
Hold on to this file, because the rest of the examples in this chapter assume this basic setup and will add
code to it. But before we get to that, you'll first need some background theory.
Gravity
The first kind of particle attraction we discuss is gravity. Gravity was covered in Chapter 5, but that was
gravity as seen from close up.
Standing on earth, gravity has a simple description: It pulls things down. In fact, it pulls things down at a
specific rate. The acceleration it applies is equal to about 32 feet per second. One way of expressing
acceleration is by how much velocity it adds over a specific time period. Gravity makes things go 32 feet
per second faster, every second it pulls on them. You can go to the tallest mountain or the lowest valley,
and that number 32 isn't going to change enough for you to notice (without a specialized measuring tool).
Gravitational force
When you step back, the farther away you are from a planet or large body, the smaller the force of gravity
becomes. This has the pleasant side effect of keeping the earth, as well as other planets, from being
sucked into the sun and smashed together. From this far-away, “top-down” view of the solar system,
where you can refer to planets as particles, the distance between them affects the gravitational pull.
How much the distance affects the force is easy to describe: It's inversely proportional to the square of the
distance. Well, maybe that needs some explanation. Gravity is closely tied to mass—the more mass
something has, the more force it will pull on other things and the more it will be pulled by them. And there
is also something called the gravitational constant (abbreviated as G ) that fits in there. Here's the full
equation for the force of gravity:
force = G × m 1 × m 2 / distance 2
This means the force of gravity on an object by another object is equal to this gravitational constant, times
both masses, divided by the square of the distance between them. Sounds simple enough—you just need
to know what the gravitational constant is, and you'll be all set. Well, here's the official definition of it:
G = 6.674 × 10 -11 × m 3 × kg -1 × s -2
Maybe it is not so simple after all. But thankfully, we can just remove G from the formula and our
animations will look fine:
force = m 1 × m 2 / distance 2
This might seem like we're cheating a bit—because we are—but it's something you're not likely to notice
and will save us a bunch of calculations. Think of the gravitational constant, G , as a way to normalize the
equation to get the force in the proper measurement for your particular implementation. If modeling the
universe, you'd want this value in the unit of Newtons, so you set G to the previous long equation. But in
our animation implementation, we can work in our own little universe, and just set G to 1 and remove it. In
fact, this is also what particle physicists do in their equations. Now, if you are making a canvas-based
 
Search WWH ::




Custom Search