Today we're going to talk about acceleration. It's quite a simple topic, and it shouldn't take you that long to understand fully. Acceleration is what happens when we send things across the screen and their velocity becomes greater and greater. The best example is that of a car. You simply step on the gas pedal and the car moves faster. Your car is not just moving, it's increasing the rate at which it moves.
Acceleration, like a couple other physics operations we'll be learning over the course of these lessons, is simply the plugging in of secondary variables which are added or multiplied to our values. We've seen this already with velocity (our VX and VY variables from the previous lesson).
Now let's take a closer look at what happens with velocity in order to understand acceleration which is nothing more than compounded velocity in HTML5.
To start with, we're going to return to our example from yesterday:
Now we're going to automate it so that it travels across the screen on its own. How do we do this? First off, we get rid of our keyboard events. Then we plug some initial values into our VX and VY variables. I've gone with 3 and 3, but you can change them around if you want. Just make sure your rectangle isn't flying so fast you can't see it accelerate when the time comes!
These go on top with the other variables....
And that does it! Our ship is now flying on auto-pilot. Here's our full, modified code:
Cool, huh? Now, just to illustrate what's happening here, go ahead and set your setInterval timer to 500:
This slows the animation WAY down to 1/2 a second per change, but this will give us a chance to see what's happening from close-up.
Every time our setInterval goes off, the rectX and rectY values of our rectangle are increased. If we start off at 100 x and 100 y, that means we add three to our rectY and rectY each time around. Therefore we have:
Slowing down our interval to 500 milliseconds helps us visualize that better. But how are going to implement acceleration, then? We need a new variable that we can add to VX and VY, right? If velocity is the constant adding of a value to our x and y values, acceleration is nothing more than the adding of an ACCELERATION value to our... VX and VY values!
So let's define some new values:
Ladies and gentlemen, I bring you: AX and AY. Let's add them right away to our ever-growing list of variables:
The formula is simple: all we need to do is go to our drawRectangle function (you know, the one that is constantly running) and add these two simple lines:
And what does this do? Before running it, let's understand it first. Here's our list of rectX and rectY values once again. This time with acceleration:
Now run it and watch as our rectangle slowly jumps further and further with each leap. It starts out slowly, then adds the value of AX and AY on each tick. Neat, huh?
Here's the full code again with all of our changes. Feel free to change the speed of the setInterval if that helps you visualize it better. I've found that a value of about 50 or so works just fine. By the way, isn't it neat what happens to the rectangle when it start accelerating REALLY fast... it gets skippy, then stands still. In fact the term for this illusion is motion standstill. Don't you F.L. science?
Now this should give you ample space for experimentation. We are now familiar with the basics of screen boundaries and we can control the velocity and acceleration of things we choose to move across the screen. I'd like you to try moving different things and work with these until you can do them flawlessly. But you may have noticed at this point that you can only move things in straight lines. Fun for a while, but let's get rid of some constraints so we can REALLY follow our imaginations and be able to do whatever our minds conjure up. THAT'S the real goal of these tutorials: freeing imagination of all the shackles that limit free expression in game development with HTML5. Once you've got all the tools and have mastered the technical aspects, you can very easily rely on your own noggin for making games. So get ready for tomorrow when we look at other ways to produce movement!
So stay tuned and I hope you enjoyed! As always, thanks for following along! Code didn't work for you? Hate me? Are you my illegitimate child? Send input anytime!
Onward to Lesson Eighteen!
Back to Lesson Sixteen!
Back to Index