Tilebased games in Flash 5 - Part 12

"Jump physics"... This is a topic I get a lot of questions about. I havenīt written about it because itīs more related to physics than the tile-concept(which this set of tutorials is about).
I just think there are so many good physics tutorials out there, better than anything I could write...

But anyway... Here we go.

Before, when we made stuff like the Maze example in tutorial 8, we could simply apply the pixels to move directly to the x and y properties of the character, when a key was pressed.

But now that changes, as we need to apply a force(gravity) to the character regardless if a key is pressed or not.

So two new variables are born, I call them xSpeed and ySpeed, some like to call them xVelocity and yVelocity, dosenīt really matter ;)

These variables are then used like this:
xpos += xspeed;
ypos += yspeed;
That means if xspeed is -5, xpos will get whatever it was minus 5, thus making the character move to the left and vice versa.

But before we apply these values to xpos and ypos, we will manipulate xspeed and yspeed a little, adding gravity(to yspeed) and friction on the ground(to xspeed).

Gravity is usually represented by a value between -1 and 1, below 0 means reversed gravity. The higher the value is, the more gravity it is...
The opposite goes for ground friction, the higher the number, the less friction it will be, a negative number will make things go in reverse...

So we set up a couple of constants for this:
gravity = 1;
groundFriction = 0.8;
Then we apply these to xspeed and yspeed like this:
yspeed += gravity;
xspeed *= groundFriction;

xpos += xspeed;
ypos += yspeed;
So now gravity or '1' will be added to yspeed every frame, thus making yspeed a more and more positive value. Which will then be added to ypos and making it fall forever and ever, faster and faster...

Xspeed on the other hand will get closer to 0 every frame unless constantly set to a positive or negative value. Letīs say the "Walk to the right"-key is pressed, and xspeed is constantly set to 3, making the charcater move 3 pixels to the right every frame. When that key is released the ground friction will begin to take effect, first time 3 * 0.8 = 2.4, second time 0.8 * 2.4 = 1.9 and so on, constantly slowing xspeed down.

So now, if wanted the character to jump, we would simply make yspeed a negative value, something like:
if ( Key.isDown(Key.UP) ) { 
	yspeed = -4;
This will make the character move like this(now that gravity is constantly added) over a number of frames:
1.  ypos-4
2.  ypos-3
3.  ypos-2
4.  ypos-1
5.  ypos+-0
6.  ypos+1
7.  ypos+2
8.  ypos+3
9.  ypos+4
10. ypos+5
and so on... Hopefully you get the idea.

The collision checking idea is still the same as before, all though there some more things to have in mind.

When colliding to the left and right we need to set xspeed to 0 or reverse it(making the character bounce back).

We also need to keep track on, if the character has ground under itīs feet, it shouldnīt be able to jump unless itīs standing on the ground. We also need to make yspeed equal to 0 when colliding with something above or below.

I will not go through that in detail, I just feel like I would be repeating myself. I suggest you take a look at this fla(and/or the previous tutorials), and hopefully youīll figure it out ;).

Example (Focus then arrowkeys to control):

If you want to learn more about physics, just search google.com for something like "flash +physics +tutorial" or "director +physics +tutorial"... I know there are some great tutorials for director and the theory is of course the same. And remember Kris Kross will make you JUUMP!

(C) OutsideOfSociety 2002.