Computer Science Canada

[Tutorial] How to Jump

Author:  Mazer [ Sat Dec 22, 2007 10:46 am ]
Post subject:  [Tutorial] How to Jump

If your game involves any kind of movement you should already be keeping track of the following variables:
  • players x and y position
  • players x and y velocity

It seems as though alot of you forget about the second one. Go ahead and add it now.
Now then... [copied from another post where I explained jumping]

When you are running right your x velocity should be 10 (or whatever) and when running left, -10.

Your y velocity is somewhat different. You should always be subtracting some value (9.8m/s^2 is acceleration due to gravity, but the units you are using may not correspond so use a value that looks about right) from the y velocity. When the player presses the jump button, first make sure that they are on the ground (y position is 0 or something like that) and then set the y velocity to something relatively high. Don't worry about it looking like the value is too high, you're still decreasing it every time you go through the loop. In this way the y velocity will eventually reach zero and then go below zero, so your character should move through the air in a smooth arc. Don't forget to stop the character when it hits the ground.

Now for some source code.
code:
% some constants, tweak this to your liking
const GROUND_HEIGHT := 120
const RUN_SPEED := 10
const JUMP_SPEED := 30
const GRAVITY := 2

var win := Window.Open ("graphics:640;480,offscreenonly")
var chars : array char of boolean

% player position and velocity
var posx, posy : int
var velx, vely : real

posx := 20
velx := 0
posy := 400
vely := 0

loop
    Input.KeyDown (chars)
    if chars ('q') then
        exit
    end if
   
    % to make the player move
    if chars (KEY_LEFT_ARROW) then
        velx := -RUN_SPEED
    elsif chars (KEY_RIGHT_ARROW) then
        velx := RUN_SPEED
    else
        velx := 0
    end if
   
    % remember, in order to jump you MUST be on the ground when pressing UP
    if chars (KEY_UP_ARROW) and posy = GROUND_HEIGHT then
        vely := JUMP_SPEED
    end if

    % subtract your "gravity" constant from the velocity EACH frame
    vely -= GRAVITY
    posx += round (velx)
    posy += round (vely)

    % simple "collision" check. just makes sure the player stays above ground
    if posy < GROUND_HEIGHT then
        posy := GROUND_HEIGHT
        vely := 0
    end if

    % different colours just to illustrate whether or not you may jump
    if posy = GROUND_HEIGHT then
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, green)
    else
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, red)
    end if

    drawline (0, GROUND_HEIGHT, maxx, GROUND_HEIGHT, blue)
    View.Update
    delay (25)
    cls
end loop

Window.Close (win)


That's a really simple example, but it should be enough to get going. If you can't adapt that to account for things like floating platforms or ceilings or pits (ie, real collision detection) then you've probably picked a project that is too complicated for you right now.

Author:  Zampano [ Sat Dec 22, 2007 10:37 pm ]
Post subject:  Re: [Tutorial] How to Jump

A tutorial like this is is very helpful, as are all that teach one how to perform tasks without processes.
I do have three questions, though.
First, is there anyway to do the same things you did but without declaring a costly 'real' variable? If the user decides not to jump, vely will continue to decrease and decrease even though it will not be used (unless one jumps). Surely there is a way to avoid something as redundant as that.
Second, what is the purpose of of velx if you could simply increment posx by the value of ground speed?
Third, what is the purpose of declaring those four constants if there values are already constants by themselves?

I think that though a tutorial such as this does not fit into the Turing Walkthrough, tutorials like these should be listed elsewhere; perhaps in some sort of index to solutions to common programming goals.

Author:  Tony [ Sun Dec 23, 2007 12:31 am ]
Post subject:  Re: [Tutorial] How to Jump

Zampano @ Sat Dec 22, 2007 10:37 pm wrote:

is there anyway to do the same things you did but without declaring a costly 'real' variable? If the user decides not to jump, vely will continue to decrease and decrease

no, if the character stays on the ground, the y-velocity will be 0. If it was to decrease, the character would obviously fall down through the ground.
Zampano @ Sat Dec 22, 2007 10:37 pm wrote:

what is the purpose of of velx if you could simply increment posx by the value of ground speed?

the value of ground speed is velx.
Zampano @ Sat Dec 22, 2007 10:37 pm wrote:

what is the purpose of declaring those four constants if there values are already constants by themselves?

the purpose of declaring those four constants is that their values are already constant.

remember how you can't change the values of constants? Yeah, it's just like that.

Author:  Mazer [ Sun Dec 23, 2007 12:36 am ]
Post subject:  Re: [Tutorial] How to Jump

Zampano @ Sat Dec 22, 2007 10:37 pm wrote:
First, is there anyway to do the same things you did but without declaring a costly 'real' variable?

Go with integers if you really want. It's actually pretty pointless since I'm rounding anyways.
Zampano @ Sat Dec 22, 2007 10:37 pm wrote:
If the user decides not to jump, vely will continue to decrease and decrease even though it will not be used (unless one jumps). Surely there is a way to avoid something as redundant as that.

You're doing loads of processing in a game regardless of whether the user is doing anything. In the big scheme of things, decreasing vely probably won't count for much of a performance hit. If you prefer, you can put it inside that if statement so that it only decreases when the player is above the ground.
Zampano @ Sat Dec 22, 2007 10:37 pm wrote:
Second, what is the purpose of of velx if you could simply increment posx by the value of ground speed?

I just wanted to be consistent; the y position isn't directly controlled by keyboard input so I wanted to do the same with the x position. And maybe you'll want to add friction to your game. Or prevent the player from moving horizontally whilst airborne.
Zampano @ Sat Dec 22, 2007 10:37 pm wrote:
Third, what is the purpose of declaring those four constants if there values are already constants by themselves?

That's the whole point. I could easily get rid of those constants and replace every occurrence of GROUND_HEIGHT with 120, but what happens next week when I want to change the ground height in my suddenly larger game? Besides tweaking variables, this makes for easier reading. 120 is the height in pixels of the "surface" in this game, but it's also the minimum height in centimeters for an officer of the Royal Canadian Mounted Police and I don't want to waste one second thinking about which I'm referring to. In fact, the minimum height was 180 centimeters and that rule hasn't actually applied in a really really long time, but I felt like using that in my example.

Zampano @ Sat Dec 22, 2007 10:37 pm wrote:
I think that though a tutorial such as this does not fit into the Turing Walkthrough, tutorials like these should be listed elsewhere; perhaps in some sort of index to solutions to common programming goals.

I don't give a damn about the Turing Walkthrough. What concerns me is the number of retards who come in here and suggest the use of a for loop for something like jumping. DON'T ****ING DO THAT I WILL SLAP YOU. Not that it matters anyways; the people in question wouldn't look at common problems just like they wouldn't look at this tutorial. They want a solution, not a learning experience.

Author:  rcbhs [ Wed Jan 02, 2008 7:59 am ]
Post subject:  Re: [Tutorial] How to Jump

Ratards cause we are new and are basing our ideas off of what we have seen in other programs? Harsh.

Author:  Clayton [ Wed Jan 02, 2008 12:03 pm ]
Post subject:  Re: [Tutorial] How to Jump

rcbhs @ Wed Jan 02, 2008 7:59 am wrote:
Ratards cause we are new and are basing our ideas off of what we have seen in other programs? Harsh.


Not quite.

Author:  rcbhs [ Thu Jan 03, 2008 5:01 am ]
Post subject:  Re: [Tutorial] How to Jump

Oh and thx for this though. Working nicely but now every time I go in one direction I don't stop, i can turn directions and sich but cant stop. I also cant go up with the up arrows key anymore (jump is TAB btw) which I need for ladder climbing. Still trying to figure it out.

Author:  Mazer [ Thu Jan 03, 2008 12:15 pm ]
Post subject:  RE:[Tutorial] How to Jump

That's kind of confusing. You'll have to explain your problem better or post some code.

Author:  thrivey [ Fri Jan 11, 2008 4:01 pm ]
Post subject:  Re: [Tutorial] How to Jump

I am trying to make my character jump onto a platform. Can some one please tell me what an if statement would look like if there are 2 conditions because if my character passes the Y co-ordinate he will teleport to the platform and if he passes the X co-ordinate he teleports to the platform aswell. I need a code that needs both co-ordinates of the platform before it teleports to the platform.. Which wouldn't look like it teleported there cause the co-ordinates would be the same. This is what i got.


if placex = 70 and placey = 70 then
placex := 70
placey := 70
end if


Edit: Or I just need any code that allows my character to jump on a specific like say 60, 60, 60, 60,red

Author:  Clayton [ Fri Jan 11, 2008 6:54 pm ]
Post subject:  RE:[Tutorial] How to Jump

code:

 if placex = 70 and placey = 70 then
    placex := 70
    placey := 70
end if


Think about that piece of code for a minute. I mean chew it over, think about what's happening. Afterwards, tell yourself what's wrong with it.

Author:  thrivey [ Mon Jan 14, 2008 6:45 am ]
Post subject:  Re: [Tutorial] How to Jump

Honestly, I don't know Razz. I just know what I'm learning in school which isn't very much. That was just me trying get my character to jump onto something.

Author:  [Gandalf] [ Mon Jan 14, 2008 10:08 am ]
Post subject:  RE:[Tutorial] How to Jump

If an apple is worth $5 and an orange is worth $5 then adjust the price of the apple to $5 and the orange to $5. That's essentially what you're doing. Will it have any effect, except wasting time? No.

Using += will probably get your further...

Author:  thrivey [ Mon Jan 14, 2008 12:26 pm ]
Post subject:  Re: [Tutorial] How to Jump

I'm still pretty lost.. Isn't there a command that I can make that when my character collides with my platform it = the base of the platform and therefore stays on?

Can anyone add to the above "How to Jump" code and make it so that box can jump onto something.. I think i'd be able to get it to work from there.

Author:  Tony [ Mon Jan 14, 2008 12:50 pm ]
Post subject:  RE:[Tutorial] How to Jump

go back and read over the code, it's already there
code:

% simple "collision" check. just makes sure the player stays above ground
    if posy < GROUND_HEIGHT then
        posy := GROUND_HEIGHT
        vely := 0
    end if

Author:  thrivey [ Mon Jan 14, 2008 12:53 pm ]
Post subject:  Re: [Tutorial] How to Jump

I tried something like that already.. but it doesn't work.. =/


if placey = 75 and placex > 100 and placex < 200 then
placey := 75
speedy := 0
end if

Author:  Tony [ Mon Jan 14, 2008 1:04 pm ]
Post subject:  RE:[Tutorial] How to Jump

that's because you are skipping over the details

<= is not the same as =, try again.

Author:  callawayguy24 [ Mon Jan 14, 2008 1:08 pm ]
Post subject:  RE:[Tutorial] How to Jump

HE GOT IT, HE REALLY DID, thank you soo much Tony, u saved my carreer as a computer programer

Author:  thrivey [ Mon Jan 14, 2008 1:08 pm ]
Post subject:  Re: [Tutorial] How to Jump

Thanks a lot! It worked.. Really appreciate it.

Author:  thrivey [ Mon Jan 14, 2008 3:43 pm ]
Post subject:  Re: [Tutorial] How to Jump

I just got back from school and I just something, when I try to go under the platform it teleports me onto the platform. I made it so i'd be able to jump while on the platform though.

Author:  thrivey [ Tue Jan 15, 2008 12:01 pm ]
Post subject:  Re: [Tutorial] How to Jump

Noone? Razz

Author:  Tony [ Tue Jan 15, 2008 12:13 pm ]
Post subject:  RE:[Tutorial] How to Jump

That's because being under the platform is, well... being under the platform. Look over the code and see what if-conditions are met.

Hint: consider using <= POSITION_OF_PLATFORM with WIDTH_OF_PLATFORM for a more limited "snap back to the ground" check.

Author:  thrivey [ Tue Jan 15, 2008 12:40 pm ]
Post subject:  Re: [Tutorial] How to Jump

if placey <= 250 and placex > 100 and placex < 200 and not placey > 250 then
placey := 250
speedy := 0
end if


I tried this but still the same. =/

To my understanding this means if placex is equal to both x 100-200 AND placey at 250 so basically it shouldn't do anything unless the co-ord of my character are (100,250), (101,250), (102,250) etc

Author:  Tony [ Tue Jan 15, 2008 12:55 pm ]
Post subject:  RE:[Tutorial] How to Jump

no, I already wrote in this thread
Tony @ Mon Jan 14, 2008 1:04 pm wrote:
that's because you are skipping over the details

<= is not the same as =, try again.


The very first thing in your code -- if placey <= 250

it's less than or equals to 250. Not "exactly 250".

btw, if you try to have it be exactly 250, there's a chance you'll fall in by a bit first (lets say get to 249) and then your character will fall through the ground).

Basically re-read the tutorial, all the necessary information is here.

Author:  thrivey [ Tue Jan 15, 2008 1:04 pm ]
Post subject:  Re: [Tutorial] How to Jump

Put If i jump over it and falling down onto it it should eventually equal 250.

Author:  Tony [ Tue Jan 15, 2008 1:29 pm ]
Post subject:  RE:[Tutorial] How to Jump

only if your jump/fall speed is a constant 1 pixel per frame.

Author:  thrivey [ Tue Jan 15, 2008 4:26 pm ]
Post subject:  Re: [Tutorial] How to Jump

My jump and fall speed are constants.

Edit: Want me to paste my whole code, and you can tell me wants wrong with it?

Author:  Gooie [ Tue Jan 15, 2008 10:34 pm ]
Post subject:  Re: RE:[Tutorial] How to Jump

Tony @ January 15th, 1:29 pm wrote:
only if your jump/fall speed is a constant 1 pixel per frame.


Actually, If you step through the application of the Y Velocity to the Y Position with a for loop, you can check every pixel, even if it isn't drawn there. That's what I used. It's super smooth, fast, and flexible.

Author:  Mazer [ Tue Jan 15, 2008 10:52 pm ]
Post subject:  RE:[Tutorial] How to Jump

Not to be a dick, but all of these platform questions are in the area of collision detection. I only wrote that "simple collision" part so that you could actually see how the jumping works. In anything even remotely interesting you would probably need to work out your own collision detection (or work in someone else's).

jnrdev (jump 'n' run)
Has some great tutorials for side scrolling games that may or may not help you.

Author:  thrivey [ Wed Jan 16, 2008 12:46 pm ]
Post subject:  Re: [Tutorial] How to Jump

I just can't get it... =/ This is what i got, if theres an easier way tell me, or if there is a way to fix it PLEASE tell me.

const ground := 50
const speed := 10
const jumpspeed := 23
const downforce := 2


var win := Window.Open ("graphics:800;600, offscreenonly")
var chars : array char of boolean

var placex, placey : int
var speedx, speedy : real

placex := 20
speedx := 0
placey := 400
speedy := 0

loop
Input.KeyDown (chars)
if chars ('x') then
exit
end if

%character movements.
if chars (KEY_LEFT_ARROW) then
speedx := -speed
if placey = ground then
end if
elsif chars (KEY_RIGHT_ARROW) then
speedx := speed
if placey = ground then
end if
else
speedx := 0
end if

% jumping off of group and platforms
if chars (KEY_UP_ARROW) and placey = ground then
speedy := jumpspeed
end if

if chars (KEY_UP_ARROW) and placey = 90 and placex > 100 and placex < 200 then
speedy := jumpspeed
end if

if chars (KEY_UP_ARROW) and placey = 340 then
speedy := jumpspeed
end if
% down force (gravity)
speedy -= downforce
placex += round (speedx)
placey += round (speedy)

%making sure the character doesn't go through the platforms floor, and walls.

if placey < ground then
placey := ground
speedy := 0
end if

if placey > 95 and placex < 100 and placex < 200 and placey < 85 then
placey := 90
speedy := 90
end if

if placey <= 340 and placex > 230 and placex < 400 then
placey := 340
speedy := 0
end if

if placex < 0 then
placex := 0
speedx := 0
end if

if placex > 770 then
placex := 770
speedx := 770
end if

%picture management(so the picture of the character never leaves)

if not placey = ground then
Pic.ScreenLoad ("mario4.jpg", placex - 2, placey + 2, 0)
end if


if chars (KEY_UP_ARROW) then
Pic.ScreenLoad ("mario4.jpg", placex - 2, placey + 2, 0)
end if

if not placey = ground and chars (KEY_LEFT_ARROW) then
Pic.ScreenLoad ("mario3.jpg", placex - 2, placey + 2, 0)
end if

if not placey = ground and chars (KEY_RIGHT_ARROW) then
Pic.ScreenLoad ("mario4.jpg", placex - 2, placey + 2, 0)
end if

if placey = ground and chars (KEY_LEFT_ARROW) then
Pic.ScreenLoad ("mario3.jpg", placex - 2, placey + 2, 0)
end if

if placey = ground and chars (KEY_RIGHT_ARROW) then
Pic.ScreenLoad ("mario4.jpg", placex - 2, placey + 2, 0)
end if

if placey = ground and not chars (KEY_LEFT_ARROW) then
Pic.ScreenLoad ("mario4.jpg", placex - 2, placey + 2, 0)
end if

%platforms and floor.

drawline (0, ground, maxx, ground, blue)
drawline (100, 90, 200, 90, 6)
drawline (230, 340, 400, 340, 6)
View.Update
delay (25)
cls
end loop

Author:  thrivey [ Thu Jan 17, 2008 11:55 am ]
Post subject:  Re: [Tutorial] How to Jump

Any suggestions?

Author:  TopBypass [ Sat Jan 19, 2008 11:53 am ]
Post subject:  RE:[Tutorial] How to Jump

Is it possible to change the object from a box to an image?

Author:  Clayton [ Sat Jan 19, 2008 12:38 pm ]
Post subject:  RE:[Tutorial] How to Jump

Yes, instead of drawing your box, draw the image in it's place...

Author:  TopBypass [ Sat Jan 19, 2008 12:51 pm ]
Post subject:  RE:[Tutorial] How to Jump

i mean inserting an image.

Author:  Clayton [ Sat Jan 19, 2008 2:24 pm ]
Post subject:  RE:[Tutorial] How to Jump

It's the exact same thing. However, this really deserves it's own topic in [Turing Help], ask your question there.

Author:  Michael516 [ Fri Mar 14, 2008 2:18 pm ]
Post subject:  Re: [Tutorial] How to Jump

thrivey if you still cant figure out how to put platforms in i have edited the how to jump program so that is has a platform in it
here it is...

Author:  A.J [ Sun Mar 16, 2008 4:21 pm ]
Post subject:  Re: [Tutorial] How to Jump

just to add to this tutorial,
the example Michael516 gave is good, but make sure you can't jump up through the line
(it shouldn't be accessible up or down)

and make sure you can't go out of the screen (a couple more if statements)

Author:  Doug101 [ Wed Mar 26, 2008 7:11 pm ]
Post subject:  Re: [Tutorial] How to Jump

Hey Micheal its Doug its a coll game but wat do these stand for "var velx, vely : real" im really confused? Confused

Author:  Mackie [ Wed Mar 26, 2008 7:17 pm ]
Post subject:  RE:[Tutorial] How to Jump

They stand for X Velocity and Y Velocity. Which is the speed the character is moving at for both x, and y axis.

Author:  Doug101 [ Wed Mar 26, 2008 7:22 pm ]
Post subject:  Re: [Tutorial] How to Jump

Thanks Mackie that explains alot Very Happy

Author:  Jujeff [ Mon May 12, 2008 12:08 pm ]
Post subject:  Re: [Tutorial] How to Jump

I've already posted this to collision detection, but I don't know where to really put it, so I put it here too. I'm new to here, and Turing (sort of) so I'm sorry if I appear a noob. I wanted to make a platformer for a school project, so I realized I needed to make my character jump. I found the highly-helpful Mazer Jump Tutorial, copied and pasted (there are some numbers I couldn't figure out the use of), and tweaked it to my needs.

After putting in a platform, I discovered that this messed everything up (the character cannot jump once on a platform), I tried to fix it as best as I could, and added in a boolean variable by recommendation of a friend, however as it is not clear to me the use of this, I will paste the code that I had before the boolean variable.

(The problem of the jumping of the platform, as far as I can see, is that the Yvelocity must be 0 to be able to jump, but the character can only be on the platform if Yvelocity is above the value of "gravity".)

Code:

setscreen ("graphics:1011;699;nobuttonbar;offscreenonly")

% VARIABLES!

var keystroke : array char of boolean

var height : int := 1
var LeftWall : int := 1
var RightWall : int := 1011
var movespeed : int := 10
var jumpspeed : int := 30
var gravity : int := 2

var Xposition, Yposition : int
var Xvelocity, Yvelocity : real

Xposition := 20
Yposition := 400
Xvelocity := 0
Yvelocity := 0

% game loop

loop

Input.KeyDown (keystroke)

% horizontal movement

if keystroke ('a') then
Xvelocity := -movespeed
elsif keystroke ('d') then
Xvelocity := movespeed
else
Xvelocity := 0
end if

% jump movement

if keystroke ('w') and Yposition = height then
Yvelocity := jumpspeed
end if
% world (lines and where you can walk and such)

drawline (0, height, maxx, height, blue)

drawline (245, 146, 384, 146, blue)

% line (if you're on the middle line, stay on it)

if Xposition >= 245 and Xposition <= 384 and Yposition >= 140 then

Yposition := 140
Yvelocity := 10
end if

% reinstate gravity every loop

Yvelocity -= gravity
Xposition += round (Xvelocity)
Yposition += round (Yvelocity)

% don't fall through the ground or go past the edges

if Yposition <= height then
Yposition := height
Yvelocity := 0
end if

if Xposition < LeftWall then
Xposition := 1
end if

if Xposition > RightWall then
Xposition := 1010
end if

% character (square for now)

drawfillbox (Xposition - 10, Yposition, Xposition + 10, Yposition + 20, green)

drawfillbox (Xposition - 10, Yposition, Xposition + 10, Yposition + 20, red)

% animation

View.Update
delay (25)
cls

end loop

Author:  Jujeff [ Mon May 12, 2008 7:47 pm ]
Post subject:  Re: [Tutorial] How to Jump

EDIT::: no need, answer is found

Author:  DaveAngus [ Tue May 20, 2008 12:35 pm ]
Post subject:  RE:[Tutorial] How to Jump

This tutorial is great and has helped me out a lot! Thanks Mazer!

Author:  SAMZ [ Mon May 26, 2008 9:17 am ]
Post subject:  Re: [Tutorial] How to Jump

What about a wall? How would I jump over and back with a wall?


: