• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Help with jumping

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

44 replies to this topic

### #1DrNicholas  Members

Posted 12 October 2011 - 11:57 PM

I was learning from lazy and I got a simple character moving left and right, I added in my own timer so that the players animation would look better. I am having trouble with jumping. I had a backup code and I replaced main with that, so I lost whatever was in main for jumping. but player.h should still have some relics of what things (y_velocity, y_offset, etc.).

I was also tried splitting my game up into smaller .cpp files, I kept getting errors about the players status and everything. I only had one time the extra .cpp file worked, but nothing was in it (that was in any other thing)

I was also trying to get "platform" to work (a simple test level lol). I could not get any image to show and it kept giving me errors.

Please, I am not in the mood for bickering and flaming, I would like some help. I have looked for tutorials and asked a college kid; but I couldnt find anything to help me, my c++ book says nothing about this, and the college kid is busy with a java project. Can you help me with:
*jumping (it can be simple, I can build up from there)
*falling (same thing)
*splitting my game up into .cpp and .h files (again, saw no tutorial and the book only said to #include .h and then the .h file will look for the .cpp or something like that)
*getting the platform to work (if it isnt in there, thats because I probably reverted back to an older version of my game, but I had nothing in the files to begin with, just a load_image)

if my .zip file doesn't work, then tell me whats wrong, thanks

### #2DaveMS  Members

Posted 13 October 2011 - 07:07 AM

For jumping / falling, try adding a downwards acceleration to your character when it is not on top of a platform. Jumping can be done by adding an upwards force when the jump button is pressed. If you post your errors, we can help you with them.

### #3Serapth  Members

Posted 13 October 2011 - 07:31 AM

First off, don't use DevC++, its outdated, unsupported, buggy and any documentation surrounding it are bound to be outdated and probably bad for you. If you need a free IDE pick up Visual C++ Express or Codeblocks, or failing that even Netbeans or Eclipse, anything but DevC++.

As to your splitting the game up into multiple files, that is relatively easy. You have classes Timer and Player that can easily be split out, simply copy the Player header code to a file called Player.h and the Player code from main.cpp to Player.cpp, then add a #include "Player.h" to the top of your main.cpp. Make sure you start your .h files with the line #pragma once ( or Google C++ header guards for the more accepted but less friendly way ) to prevent it from be included more than once. You can do the exact same thing with your timer class.

### #4JustinDaniel  Members

Posted 13 October 2011 - 09:54 AM

First off as serapth said before don't use Dev shed, its too outdated, I would prefer VC++ over it. Try changing to it, it has alot of Microsoft support too.
Next For jumping,
You need your code properly structured.
The main aim of jumping is, First you launch the object say 50 m/s the object goes at 50 m/s to a certain height and once it reaches the height you slowly have to decrease its speed by a downward direction also known as Gravity, But a computer doesnt know what a gravity is or something, we know gravity's value is 9.8 m/s, So we define gravity also known as Acceleration in downward direction, so we set variables for it to let the computer understand it.
I just added some new variables to your code, just identify them.
offSet is changed to int x, and int y because you need to calculate its x and y positions at a certain height.
velocity is changed to int xVel, int yVel because you need a velocity in upward direction not left right alone.
I also added int xAccel and yAccel to calculate gravity.
And also a "bool" Jumping to verify its jumping state.
Try to understand from this code, I dont know if it will work. But im sure it'll help you.
Player::Player()
{
x = 0;
y = 0;
xVel = 0;
yVel = 0;

yAccel = 5; // For gravity you need a downward accelaration so we set Accel in downward'y' direction
xAccel = 0;

Jumping = false; //Jumping status//

frame = 0;
status = PLAYER_RIGHT;

playerTime.start();
}

void Player::handle_events()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the velocity
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: xVel += 3; break;
case SDLK_LEFT: xVel += 3; break;
case SDLK_SPACE: // suppose the key is space to jump
yVel = 40; // You launch the player to a speed of 40 //
Jumping = true; //Then you set jumping status to true //
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Set the velocity
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: xVel -= 3; break;
case SDLK_LEFT: xVel += 3; break;
case SDLK_SPACE:
if( Jumping )
{
if( yVel > -40 ) // If yVel is greater than its given velocity
{
yVel = yVel - yAccel; //We decrease yVel slowly by downward accel //
}
}
if( y <= 0 ) // Check if player has reached the ground //
{
yVel = 0;
yAccel = 0;
//Remember the value of the ground may change according to where you set its position so try changing values if you cant reach ground //
}
}
}
}

void Player::move()
{
x += xVel;

//keep player in bounds //
if( ( x < 0 ) || ( x + PLAYER_WIDTH > SCREEN_WIDTH ) )
{
x -= xVel;
}
if( Jumping ) //If we are jumping //
{
y -= yVel; // Make sure velocity is upward direction
}
if( yVel > -40 ) //Pretty much same
{
yVel = yVel - yAccel;
}
}


I hope you understand what gravity means in C++, its just simply easy if you understand it

### #5DrNicholas  Members

Posted 13 October 2011 - 10:47 AM

Thanks, dev-c++ is old, mine says it is a stable release from september 25, 2011 lol. I gotta use what the dreamcast likes :S its either codeblocks (which was a nightmare to set up, and would not even compile games) or dev-c++ (which i managed to set up). I also have Xubuntu and I downloaded the SDL library and anjuta, but I could never get the thing set up (all the instructions are for an older version of the IDE which is different). Yeah it doesnt really work, when I push space nothing happens lol. my character just gets flung to the opposite side of the screen.

I included an updated version that has your code, nothing happens.

### #6BeerNutts  Members

Posted 13 October 2011 - 01:31 PM

First, don't check for Space on key down, or else y_velocity will stay at 40 for as long as you hold down space, only check for Space on KEY_UP.

And, only act on space is Jumping is false, like this:

if( event.type == SDL_KEYUP )
{
//Set the velocity
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: x_velocity -= 3; break;
case SDLK_LEFT: x_velocity += 3; break;
case SDLK_SPACE:
if( Jumping == 0 )
{
// we're not jumping set velocity to 40, and set jumping
y_velocity = 40;
Jumping = 1;
}
break;


Now, in your move, just reduce y_velocity by some acceleration until the player hits the ground. Then set Jumping = 0. I don't know what all that other crap is.

void Player::move()

// DO X-movement stuff

//Check if we're Jumping, and act
if (Jumping)
{
// move player by y_velocity
y_offSet += y_velocity;

// Check if y_offSet is at ground
if (y_offset <= 0)
{
y_offSet = 0;
Jumping = 0;
}
else
{
// Change y velocity by y acceleration
y_velocity -= y_acceleration;
}
}


My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #7DrNicholas  Members

Posted 13 October 2011 - 01:57 PM

Thanks for trying to help me but the code does nothing. It may do something, but you don't see it. I have a feeling that you are just editing code, not testing the actual game lol. I understand what the code does, but I have no idea why its not happening... Do you think one of you could just upload my code with the jumping visible? Thanks

### #8BeerNutts  Members

Posted 13 October 2011 - 02:03 PM

Thanks for trying to help me but the code does nothing. It may do something, but you don't see it. I have a feeling that you are just editing code, not testing the actual game lol. I understand what the code does, but I have no idea why its not happening... Do you think one of you could just upload my code with the jumping visible? Thanks

Dude, What is the 1st thing I said, that you TOTALLY IGNORED? Here, I'll quote it for you:

First, don't check for Space on key down, or else y_velocity will stay at 40 for as long as you hold down space, only check for Space on KEY_UP.

So, remove the check for space under SDL_KEYDOWN. That fucks you right there, since you're setting Jumping to true and velocity to 40. You obviously don't understand what it's doing, you just blindly copied my code without reading why. I wasn't re-writing it for you, just giving you an idea.

Besides, I think 40 is going to be too much acceleration, it's going to shoot your guy up 40 pixels the 1st frame, then 35, which is way too much, try setting it to 20, and decrease the acceleration to 3. Then play with it.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #9DrNicholas  Members

Posted 13 October 2011 - 02:50 PM

I did that didnt I? lol, it still doesnt work. Please just help me get it done. Thanks

### #10CodeMunkie  Members

Posted 13 October 2011 - 03:07 PM

I did that didnt I? lol, it still doesnt work. Please just help me get it done. Thanks

Are you asking for help or do you just want someone to write the game for you?

"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.

### #11DrNicholas  Members

Posted 13 October 2011 - 03:20 PM

Im asking for help, but you arent really helping how I would like to be helped. While your code makes sense, ive done what you have said and it does not work (i do not see it working at least). In the past when ive had people help me, if this scenario comes up, theyll usually add the part i need help with, and then i can study what they did (and compare to what i was shown/taught). I knew this was going to happen when I decided to learn c++.

### #12BeerNutts  Members

Posted 13 October 2011 - 03:22 PM

POPULAR

I did that didnt I? lol, it still doesnt work. Please just help me get it done. Thanks

Since this is beginner's, I'll go easy on you. I'm going to ask questions that will make you think, and you'll be able to solve this yourself.

What is y_offSet for? What are you doing with y_offSet?
HINT: Look at our code where you draw your character.

One day you'll realize, you won't learn anything by asking other to do it for you. You're going to have to THINK for yourself. Stop being blind and start THINKING!
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #13phantom  Members

Posted 13 October 2011 - 03:35 PM

Programming is all about problem solving; getting to the point of solving the problem is A LOT more important than seeing the solution.

If every time you hit a problem you just wait for someone to give you the solution then you aren't going to learn anything and you aren't going to get anywhere with programming. If something doesn't work as you expect it to or want it to then you have to sit down, take the code apart, understand what it is doing and figure out WHY it isn't doing it.

Simply saying 'I did it, it didn't work, fix it for me!' is both a waste of YOUR time and the time of those trying to get you to help yourself...

### #14DrNicholas  Members

Posted 13 October 2011 - 04:10 PM

Like I said in my first post, I knew this was going to happen, I knew how asking for help from here would go?
= help

I can work on a game after I get things all set up, I always start with the player, then enemy AI should be easy, and so should everything else (until you get to level editing lol)

And I didnt have fully updated code in the .zip, it should be up to date, where y_offset is placed. All that happens is the character starts in the top left corner, and when you press space its like a tiny pixel shift jump while stuck in the jump animation.

### #15Dancin_Fool  Members

Posted 13 October 2011 - 04:29 PM

You should try stepping through the debugger and see how the values change when you try to jump. This should help give you more indication on why it's not working as you expect it to.

### #16DrNicholas  Members

Posted 13 October 2011 - 04:52 PM

Debugger doesnt work on my computer :S I have windows 7 x64

### #17Dancin_Fool  Members

Posted 13 October 2011 - 05:05 PM

Debugger doesnt work on my computer :S I have windows 7 x64

Switch your IDE then, not having a debugger is going to make programming very very difficult, it can still be done but you need to be really good with your printf's and asserts.

### #18CodeMunkie  Members

Posted 13 October 2011 - 05:21 PM

Debugger doesnt work on my computer :S I have windows 7 x64

If you can't use your debugger, then your dev environment is broken and you need to stop programming until you fix it. Being able to step through code and look at the values of variables while your program is running is one of those basic things you just gotta have. You can either set up a dev environment where your debugger will work, or get a tool that will work properly in your current environment. I would ditch dev-c++ and get something that will work properly. CodeBlocks or Visual Studio Express would be great choices.

"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.

### #19DrNicholas  Members

Posted 13 October 2011 - 05:56 PM

codeblocks doesnt work on my computer.

### #20BeerNutts  Members

Posted 13 October 2011 - 09:06 PM

Like I said in my first post, I knew this was going to happen, I knew how asking for help from here would go?
= help

I can work on a game after I get things all set up, I always start with the player, then enemy AI should be easy, and so should everything else (until you get to level editing lol)

And I didnt have fully updated code in the .zip, it should be up to date, where y_offset is placed. All that happens is the character starts in the top left corner, and when you press space its like a tiny pixel shift jump while stuck in the jump animation.

On Computer screens, where is 0, 0? If a player wants to "jump" (move up on the screen) what should the y value do, grow larger, or smaller?

Quit being so defensive. You could debug it if you knew what you were doing. Dev-C++ has debuging capability (I used it 2005, last time it was released). Codeblocks runs on any Windows XP, Vista, or Win 7.

If you knew how to program this stuff so well, then you wouldn't be on here, struggling with these simple concepts. This isn't a "C++" thing, this would happen in any programming language.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.