• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
NicholasLopez

Help with jumping

44 posts in this topic

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
0

Share this post


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

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.
0

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.
[code]
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;
}
}

[/code]
I hope you understand what gravity means in C++, its just simply easy if you understand it :)
1

Share this post


Link to post
Share on other sites
[quote name='DrNicholas' timestamp='1318485474' post='4872101']
*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)
[/quote]

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>
0

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.
0

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:
[code]

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;
[/code]

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.

[code]
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;
}
}
[/code]


2

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
-3

Share this post


Link to post
Share on other sites
[quote name='DrNicholas' timestamp='1318535853' post='4872284']
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
[/quote]

Dude, What is the 1st thing I said, that you TOTALLY IGNORED? Here, I'll quote it for you:
[quote]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.
1

Share this post


Link to post
Share on other sites
[quote name='DrNicholas' timestamp='1318539029' post='4872304']
I did that didnt I? lol, it still doesnt work. Please just help me get it done. Thanks
[/quote]

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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Like I said in my first post, I knew this was going to happen, I knew how asking for help from here would go?
[img]http://api.ning.com/files/q5wCynzVYsMaybT*syc372DtG7OFlsNZ0PF3e7O0GzseOOEmoJDPjH*NCokg2piMgOOGtW704RRKVY1Xu1hyRr2EfjcWlJ-Z/Carrey_Riddler.jpg[/img] = 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.
-4

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='DrNicholas' timestamp='1318546365' post='4872342']
Debugger doesnt work on my computer :S I have windows 7 x64


[/quote]

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.
0

Share this post


Link to post
Share on other sites
[quote name='DrNicholas' timestamp='1318546365' post='4872342']
Debugger doesnt work on my computer :S I have windows 7 x64


[/quote]

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.
0

Share this post


Link to post
Share on other sites
[quote name='DrNicholas' timestamp='1318543836' post='4872327']
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.
[/quote]

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.
0

Share this post


Link to post
Share on other sites
Your going to have to help us to help you - your not making it easy for the people trying to help you. Rather than posting your code in a zip file, post it directly on the forum, most people are not going to be bothered to download and compile your source - they will just move onto the next post.

Second, you need to tell us why it isn't working. give us a walk through of what you think should be happening, then tell us what is actually happening. give us error messages.

Third, programming isn't about writing code - that is the end product, the job of a programmer is problem solving. Tell me in english how you would make the character jump. If you can't do that, then your not going to be able to tell the computer how to do it in code.
0

Share this post


Link to post
Share on other sites
Theres one thing i have to say you.
In programming you dont learn by asking other, Make mistakes and learn from your own mistakes that's the way to learn programming, i can see a junk of code being copy pasted in your code. I just gave you an idea on how jumping works you gotta learn from it, not just blindly copy paste.
Anyhow for your code.
Lets go to your "player.h" file
[code]
#ifndef PLAYER_H
#define PLAYER_H

#include "timer.h"

//The dimmensions of the player
const int PLAYER_WIDTH = 32;
const int PLAYER_HEIGHT = 32;

//The status of the player
//standing
const int PLAYER_STAND = 0;
//walking right/left
const int PLAYER_RIGHT = 1;
const int PLAYER_LEFT = 2;
//jumping
const int PLAYER_JUMP = 3;
const int PLAYER_FALL = 4;

//Telling whether or not to use animation for the player
int PLAYER_WALKING = false; //true if walking, false if everything else

//The player
class Player
{
private:
//The offset
int x_offSet;
int y_offSet;

//Its current frame
int frame;
Timer playerTimer;

//Its animation status
int status;
int vertical_status;

enum Vertical_States
{
V_JUMPING_UP,
V_APEX_REACHED,
V_FALLING,
V_GROUNDED
};

// jumping-related stuff for now
static const int gravity = 4;
int jump_apex;

int x_pos; // horizontal position
int x_velocity; // horizontal velocity

int y_pos; // vertical position
int y_velocity; // vertical velocity
int y_acceleration;
int Jumping;

int surface_height; // last known good level to stand at

public:
//Initializes the variables
Player();

//Handles input
void handle_events();

//Moves the player
void move();

void updatePosition();

//Shows the player
void show();

//The jumping/falling/and ground with the player
void handleJumping();
void fall();
};

#endif

[/code]
Thats what i call A Junk of unused codes in a class. Lets see,
You have a enum state in your class and you never use it.
[code] static const int gravity = 4 ?? [/code] I dont see you using it anywhere
And what's [code] int jump_apex; [/code]
There are many more errors and void's which you never call from your class and they are useless. So try to create your class in sort of a decent way and just store variables which your going to use.
The main issue is, I dont get it why it doesnt work for you, Even i follow that method to produce a jumping state,
Anyways in Line 224 try taking away that break; statement, I dont know if that might work just try it
we all just gave you an idea what jumping is and its upto you we can't help you more or Just type the code for you, You will never learn if you dont learn from your mistakes.
0

Share this post


Link to post
Share on other sites
[quote name='DaveMS' timestamp='1318577696' post='4872435']
Tell me in english how you would make the character jump. If you can't do that, then your not going to be able to tell the computer how to do it in code.
[/quote]

I just wanted to quote this for emphasis. If you can clearly explain the behavior what you want in detail, then you almost only have to translate that to the programming language of your choice (translation means simply mapping the expressions to the proper syntax) Shit, I'm sure that doesn't mean what I wanted...

To the OP: if you want code, and you basically want someone else to do the work (writing code is jack shit itself, debugging and problem solving is the real work), then you'll have to [b]pay[/b] for it. Either in nature or with hard cash.
People who asked for solutions in my university (the "do it for me" kind of request, like you do) either paid in cash or in beer, and no body liked them. They were the parasites, who eventually (after 7-8 years instead of the 5) got their degree, but went nowhere from there.

Asking for ready solutions can happen from time to time, anything can happen, life is just like that, but doing the same with your own hobby is a mystery for me. Like asking someone else to do the "hard work" on your girlfriend....

EDIT: maybe you are just on the wrong track. Wrong track = learning from others' code as a beginner. I'm sure someone can link to articles that carefully explain why this is not good (for a beginner). Learn by doing on your own. (Well, I'm against learning from tutarials too, but that's a different matter).
0

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0