HTML and CSS Reference
In-Depth Information
Many similarities exist between velocity and acceleration in terms of how they are described. They are
both vectors. Like velocity, acceleration is described as a value and a direction. However, whereas velocity
changes the position of an object, acceleration changes its velocity.
Think of it this way. You get in your car, start it up, and put it in gear. What is your velocity? It's zero. By
stepping on the gas pedal (also known as the
accelerator
), your velocity begins to change (at least the
speed portion of it; the direction is changed with the steering wheel). After a second or two, you're going 4
or 5 miles per hour (mph). Then your speed increases to 10 mph, 20 mph, 30 mph, and so on. The engine
is applying force to the car to change its velocity.
Thus, you have the layman's definition for acceleration:
a force that changes the velocity of an object.
In purely coding terms, you can say that acceleration is a value that you add to a velocity property.
Here's another example. Say you have a spaceship that needs to go from planet A to planet B. Its
direction is whatever direction planet B happens to be in relation to planet A. It points in that direction and
fires its rockets. As long as those rockets are firing, force is being applied to the ship, and its velocity is
going to change. In other words, the spaceship continues to go faster and faster.
At some point, the captain decides the ship is going fast enough, and he might as well conserve fuel, so he
shuts down the rockets. Assuming there is no friction in space, the ship continues along at the same
velocity. As the rockets are no longer firing, no more force is being applied to the ship. Because there is no
more acceleration, the velocity does not change.
Then the ship approaches its goal. It needs to slow down or it's going to fly past planet B (or if the
navigator was accurate enough, the ship is going to become a
part
of planet B). So what does the captain
do? You don't have brakes in space—there's nothing for the ship to grab a hold of. Instead, the captain
turns the ship around so it faces the opposite direction, and fires up the rockets again. This applies
negative acceleration, or acceleration in the opposite direction. Really, this is the same thing we covered
while discussing vectors. The force is again changing the velocity, but this time it is reducing the velocity.
The velocity will get less and less, and eventually arrive at zero. Ideally, that will coincide with the point
when the ship is a couple of inches above the surface of the planet. At that point, the captain can kill the
rockets and let gravity finish the job. (We talk about gravity in the “Gravity as Acceleration” section, later in
the chapter.)
Acceleration on one axis
Let's put what you just learned into practice in JavaScript. Like the first velocity example, this first
acceleration example stays on one axis. We jump back to using the
Ball
class for the next few examples.
Here's the code for the first acceleration exercise (
06-acceleration-1.html
):
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Acceleration 1</title>
<link rel="stylesheet" href="style.css">
</head>
<body>