Jump to content
  • Advertisement
Sign in to follow this  
NicholasLopez

Help with jumping

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Hidden

*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)


Here is a simple way to understand it:

A .h (Header) file is the definition of a class e.g.

[source]
//Player.h
#ifndef PLAYER_H
#define PLAYER_H

class Player
{
public:
Player();
~Player();

void Update(float deltaT);
private:
Vector3 mPosition;
};

#endif // PLAYER_H
[/source]

A .cpp (Source) file is the implementation of it. e.g.

[source]
#include "Player.h"

Player::Player()
{
}


Player::~Player()
{
}

void Player::Update(float deltaT)
{
}
[/source]


Then you can use this class by:

[source]
//main.cpp
#include "Player.h"

int main()
{
Player thePlayer;
return 0;
}
[/source]

<div><br></div><div>Don't use Dev-C++ instead use Code::Blocks or Visual Studio.</div><div><br></div><div><br></div><div><br></div><div><br></div>

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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;
}
}



Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.[/quote]

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.

Think for yourself about this stuff before you start spouting off about it not working.

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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!