# Jumping in a 2d game

This topic is 5059 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, ive been working on a 2d game for a while now. Ive already gone alot farther then I ever planned and have big plans for its future. I unfortunatly never took physics in school and dont understand at all how to add jumping into my game. What I have now is when I press the jump button (UP) I move 10 pixels up. Im going to add something that lets it load slower then an instant jump, possiable 1 at a time, spaces out by a timer im guessing. Anyways, I tried putting into the game loop to lower the character down by one but yet it wont go, I understand the concepts of jumping, I just dont know how to do it. Any advice? Also the jumn n run tutorials absoultly suck in my opinion. Well there just not for me, they are helpful for others.

##### Share on other sites
I haven't yet made a game where there has been jumping but I guess you should have a velocity variable. When jumping up increase up_velocity by some value (gravity). Then every frame add the velocity to the sprite's y co-ordinate and every frame decrease it a bit.. I guess that's how it could be done.

(Not sure though, since I haven't done this ever.)

##### Share on other sites
My understanding of jumping (though I also haven't implemented this yet, I've thought about it a fair amount) is as follows:

a) you can only jump when you're standing on something
b) variable-height jumps should be allowed
c) as a general rule, flight should not be allowed

So what I'm planning on for my response to the "jump" key being pressed looks like this:
If (I am standing on the ground) // Begin the jump  valid_jump = true;  velocity += JUMP_VELOCITY;  jump_duration = 0;If (valid_jump AND jump_duration < MAX_JUMP_LENGTH)  velocity -= gravity; // Ignoring gravity allows variable-length jumps    jump_duration++;Else  valid_jump = false;

Realize that this is entirely untested pseudocode. You'll have to figure out when the player is standing on the ground on your own, too. The line where I subtract gravity from the player's velocity assumes that the player regularly has gravity added to his velocity; hence, subtracting it here means that the player ignores gravity for as long as the jump button is pressed and MAX_JUMP_LENGTH is not exceeded. This allows the player to "float" upwards. Probably a better way to handle this would be, in the player's position update code (where gravity would be added), to check if the player's in a valid jump and the jump button is pressed, and to actually ignore gravity there.

##### Share on other sites
Ok, that works, but I have no clue how to add velocity for my player. Right now he just has his xpos and ypos. Also I dont know where to put gravity in, if my player jumps up, he stays up.

##### Share on other sites
Quote:
 Original post by ThrustOk, that works, but I have no clue how to add velocity for my player. Right now he just has his xpos and ypos. Also I dont know where to put gravity in, if my player jumps up, he stays up.

Just add a new int variable to your player struct/class/what ever. And gravity can be a global constant.

struct Player {  int x_pos;  int y_pos;  int x_velocity;  int y_velocity;};

##### Share on other sites
ok but I dont know what velocity does, or how to calculate it or gravity.

Also now that I made a struct for the player, it doesent work. I get parse errors everywhere.

//struct Playerstruct player{int x_pos;int y_pos;int x_vel;int y_vel;};int spriteh = 40;int spritew = 29;player.x_pos =32;player.y_pos = 566;

small example...

##### Share on other sites
//struct Playerstruct player{int x_pos;int y_pos;int x_vel;int y_vel;};int spriteh = 40;int spritew = 29;player.x_pos =32;player.y_pos = 566;

what you mean is [smile] :
typedef struct player {  int x_pos;  int y_pos;  int x_vel;  int y_vel;} Player;

Player.x_pos = 32;
//blah blah blah

##### Share on other sites
Quote:
 Original post by Alpha_ProgDestypedef struct player { int x_pos; int y_pos; int x_vel; int y_vel;} Player;Player pl; pl.x_pos = 32;

Anyway, the OP should learn programming first.. Or what ever language he's using.

##### Share on other sites
#include <iostream>#include <stdlib.h>#include <winsock2.h>#include <SDL/SDL.h> //Globalsint map [20][25]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//Functionsvoid drawMap();void drawScene();//draws bitmaps, x = o is top, y = 0 is left, sprite is the imagevoid draw(int x, int y, SDL_Surface *sprite, SDL_Surface *screen);//SurfacesSDL_Surface *tilemap;//SpriteSDL_Surface *sprite1 = SDL_LoadBMP("gfx/sprites/sprite1.bmp");SDL_Surface *cloud = SDL_LoadBMP("gfx/tiles/cloud.bmp");SDL_Surface *sun = SDL_LoadBMP("gfx/tiles/sun.bmp");//TilesSDL_Surface *tile1 = SDL_LoadBMP("gfx/tiles/tile1.bmp");SDL_Surface *tile2 = SDL_LoadBMP("gfx/tiles/tile2.bmp");//struct Playertypedef struct player {int x_pos;int y_pos;int x_vel;int y_vel;}player;int spriteh = 40;int spritew = 29;player.x_pos = 32;player.y_pos = 566;//------------------------------------------------------------------------------using namespace std;int main(int argc, char *argv[]){    Uint8* keys;       //Sets transparency    SDL_SetColorKey(sprite1, SDL_SRCCOLORKEY,     SDL_MapRGB(sprite1->format, 255, 100, 255));    SDL_SetColorKey(cloud, SDL_SRCCOLORKEY,     SDL_MapRGB(cloud->format, 255, 100, 255));    SDL_SetColorKey(sun, SDL_SRCCOLORKEY,     SDL_MapRGB(sun->format, 255, 100, 255));    //boolean used to determine if the game is running    bool isRunning;    //A SDL event, used to keep gamerunning.    SDL_Event event;    //unload function    atexit(SDL_Quit);        //sets the video mode that SDL uses    // Peremiters are window width, window height,color depth, then flags    tilemap = SDL_SetVideoMode(800, 640, 0, SDL_HWSURFACE|SDL_DOUBLEBUF);    //gives the boolean a value    isRunning = true;    drawMap();        //Main game loop    while(isRunning == true)    {       //Poll event         SDL_PollEvent(&event);        //vent that ends SDL and main game loop       if(event.type == SDL_KEYDOWN)        {            if(event.key.keysym.sym == SDLK_ESCAPE)                isRunning = false;        }        keys = SDL_GetKeyState(NULL);       if ( keys[SDLK_UP] )       {              if (map[((player.y_pos-2)/32)][(player.x_pos/32)] == 1 && map[((player.y_pos-2)/32)][((player.x_pos+spritew)/32)] == 1)              {                   player.y_pos-=2;              }       }       if ( keys[SDLK_DOWN] )        {              if (map[(((player.y_pos+2)+spriteh)/32)][(player.x_pos/32)] == 1 && map[(((player.y_pos+2)+40)/32)][((player.x_pos+spritew)/32)] == 1  )              {              player.y_pos+=2;              }       }       if ( keys[SDLK_LEFT] )        {              if (map[(player.y_pos/32)][(((player.x_pos-2))/32)] == 1 && map[((player.y_pos+spriteh)/32)][(((player.x_pos-2))/32)] == 1 && map[((player.y_pos+(spriteh/2))/32)][(((player.x_pos-2))/32)] == 1)              {              player.x_pos-=2;              }       }       if ( keys[SDLK_RIGHT] )              {              if (map[(player.y_pos/32)][(((player.x_pos+2))/32)] == 1 && map[((player.y_pos+spriteh)/32)][(((player.x_pos+2)+spritew)/32)] == 1 && map[((player.y_pos+(spriteh/2))/32)][(((player.x_pos+2)+spritew)/32)] == 1)              {              player.x_pos+=2;              }       }       drawScene();    }    cout << "Testing a tile map..." <<endl;  cout << "Lets hope this works." << endl;  cout << "Geebers, it did (Knew that was going to happen)"<<endl;  cout << "Now for colision..." << endl;  cout << "Haha I actuially did it, woohoo!" << endl;    if (event.type == SDL_QUIT)       {              SDL_FreeSurface(tile1);              SDL_FreeSurface(tile2);              SDL_FreeSurface(sprite1);              SDL_FreeSurface(cloud);              SDL_FreeSurface(sun);              isRunning = false;       }      return 0;}//------------------------------------------------------------------------------------//Draws stuff to the screenvoid draw(int x, int y, SDL_Surface *sprite, SDL_Surface *screen){    SDL_Rect source;    SDL_Rect dest;    source.x = 0;    source.y = 0;    source.w = sprite->w;    source.h = sprite->h;    dest.x = x;    dest.y = y;    dest.w = source.w;    dest.h = source.h;    SDL_BlitSurface(sprite, &source, screen, &dest);    return;}//DRAWS MAP!!!void drawMap(){   int map [20][25]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,                    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};for(int y = 0;y < 20; y++){     for(int x = 0;x < 25; x++)    {        if(map[y][x] == 1)         {           draw(x*32,y*32,tile1,tilemap);         }        else if (map[y][x] == 0)         {           draw(x*32,y*32,tile2,tilemap);         }    }}}//draws backgroundvoid drawScene(){  drawMap();  draw(40,40,cloud,tilemap);  draw(330,60,cloud,tilemap);  draw(670,40,sun,tilemap);  draw(40,40,cloud,tilemap);  draw(player.x_pos, player.y_pos, sprite1, tilemap);  SDL_Flip(tilemap);}

Still doesent work. This is so frustrating. I think I might just go back to my other way and get rid of the struct part of it. But I still have no idea about how to make my character sucked towards the ground, jump, etc...

##### Share on other sites
Quote:
Original post by tentoid
Quote:
 Original post by Alpha_ProgDestypedef struct player { int x_pos; int y_pos; int x_vel; int y_vel;} Player;Player pl; pl.x_pos = 32;

Anyway, the OP should learn programming first.. Or what ever language he's using.

good correction... oops [grin]

well, you burn, you learn [smile]

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 15
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633352
• Total Posts
3011483
• ### Who's Online (See full list)

There are no registered users currently online

×