Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] How to Jump
Index -> Programming, Turing -> Turing Tutorials
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mazer




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Zampano




PostPosted: 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.
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Mazer




PostPosted: 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.
rcbhs




PostPosted: 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.
Clayton




PostPosted: 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.
rcbhs




PostPosted: 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.
Mazer




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
thrivey




PostPosted: 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
Clayton




PostPosted: 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.
thrivey




PostPosted: 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.
[Gandalf]




PostPosted: 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...
thrivey




PostPosted: 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.
Tony




PostPosted: 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
thrivey




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 43 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: