Sign in to follow this  
MrChrisnis

Jump Feature Breaking My Game?

Recommended Posts

Hey guys,

In one of my classes we are doing Scrolling backgrounds and general sprite movement stuff. Up until now everything has worked properly, however when I put in a jump feature, it seems to cause problems when the player jumps. Before jumping, the player will be confined within the sides of the screen when moving left/right, however after the player jumps, the confines of the screen seem to break and the player moves at a higher pace than they are supposed to when moving side to side.

The code is attached to this post.

Any help would be appreciated,
Thanks
Chris

Share this post


Link to post
Share on other sites
I'm not wanting to download a questionable file, nor am I wanting to crawl through >12MB of code, so would you be willing to post the relevant portions between [ code ] and [ /code ] tags?
Don't post the entire project, just the area where the bug is most likely to be (post the jump code, screen collision code, and the movement code - the entire function for each). Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
[size=4][code]
//The Jump Function

void Player::jump()
{

set_velocities(0, -30);
set_auto_move(40);

set_y_acceleration(9.8);
set_auto_accelerate(40);

}

//Which is stopped by this to stop the

if(player->get_y() > 300)
{
player->set_y_velocity(0);
player->set_y_acceleration(0);
player->set_world_position_y(300);
isJumping = false;
}

//The screen collision code is just put in with the SDK_KEYDOWN stuff

case SDLK_LEFT:
if(player->get_x() >= 10)
player->moveHorizontally(-10); break;
case SDLK_RIGHT:
if(player->get_x() + player->get_width() <= 630)
player->moveHorizontally(10); break;

[size=4]//And the moveHorizontally function

void Player::moveHorizontally(float speed)
{

set_velocities(speed, 0);
move();

}
[/code][/size] Edited by MrChrisnis

Share this post


Link to post
Share on other sites
Hi Mr Chrisnis,
What does the move() function look like, and is there a movement related function that's called on every main game-loop iteration? (If you have a such...)
Also, the units you use are unknown to us. Are they pixels or tiles?

How about rather than player->moveHorizontally, you create a set_velocity_x() or something like that?
Make it so that the movement of the player is defined by a vector, limited by a maximum on both axes, and maybe added to on each iteration by an acceleration vector.
This is also far from ideal, but I can imagine the confusion when you call the movement bit more than one place.

I just found that moveHorizontally seems to set the vertical movement to 0:
[code]
void Player::moveHorizontally(float speed)
{
set_velocities(speed, 0);
move();
}
[/code]
Is this intentional? Again, make it possible to set the velocity on each axis, independent on oneanother, or call set_velocities(speed, velo.y).

Share this post


Link to post
Share on other sites
MrChrisnis, don't send your ipch, debug folder and the .sdf file. We don't need those files [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]
As soon as we open your project and rebuild those files are created again. Your rar file takes 12MB+ ... without the files from the sentence above your rar file should be around 170Kb.

By the way, I can't run your project. I think you have separated your your SDL files from your game files. Unless you have linked them in the properties? And also, I opened your project and looking at your code, honestly. I see allot of bad coding there.[img]http://public.gamedev.net//public/style_emoticons/default/mellow.png[/img]

I'm sorry but I can't find the problem because I can't get out of your code.

When you make a player class you need to think a game-logic. How do you want your player to act in-game. You want to say: "I give you all info, you take care about it' A player has an AI, 'also in coding..'. the class needs to collect information around them and use them.

[CODE]class Player
{
public:
Player();
~Player();

void Update(EnemyList* pEnemyList, LevelObjects* pLevelObjects;); // something like this, NOTE: this isn't done this way for big games.
void Draw();
};[/CODE]

Your player can receive the information about everything, there are design patterns for this, not showing them now. It's called the Observer if I'm correct.

So your Player can check if he has hit a wall, hits another player, has hit a player with a bullet, and so on. You just need to Update and Draw him in the main function.
To handle key/mouse input I would suggest you for now to use a global(or singleton) Input class. Which stores all the keyboard and mouse info.
I know singletons are bad.. But everyone, really every programmer has made a singleton is his life because it's 'cool' and 'easy' [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]
Though I don't know how SDL works, maybe you need to send all the input through the Update method also.

Have a look at this: [url="http://www.angelfire.com/art2/ebooks/teachyourselfcplusplusin21days.pdf"]http://www.angelfire...lusin21days.pdf[/url]
Look at the day 6: basic classes. Look at how they structure their classes.


~EngineProgrammer

Share this post


Link to post
Share on other sites
Thanks for posting the code! It makes it easier for lazy or paranoid people (like me) to help you.
Thank you for also using the [ code ] tags, it makes it easier to read (and again easier to help you).

One of the reasons why your screen collision might not be working is this:
[code]case SDLK_LEFT:
if(player->get_x() >= 10)
player->moveHorizontally(-10); break;[/code]

You're only checking it when the player clicks the key... but the player can move (from velocity from previous clicks) without clicking the key. You ought to check for screen collision after any position update from velocity, not after any keypress.

Also, when colliding, don't just undo the horizontal movement, but set the player back to a save place.

Imagine this:[list=1]
[*]Player (from velocity) moves [i]more[/i] than 10 units to the left. Let's say he moves 25 units.
[*]He goes past the edge of the screen by those 25 units.
[*]You undo 10 units of movement.
[*]He's still past the screen by 15!
[/list]
Instead, [i]set[/i] his position to the correct position upon collision.

This is example code that you'll need to adapt:
[code]//Check if the player went past the boundry he wasn't supposed to cross.
if(player->get_x() > LeftScreenBoundry)
{
//Set the player's position to the right position.
player->set_x(LeftScreenBoundry);

//If the player still has velocity in the wrong direction...
if(player->get_x_velocity() > 0)
{
//...cancel further movement in that direction.
player->set_x_velocity(0);
}
}[/code] Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
Thanks guys for the help.

I am looking through to see if I can change it to get it working with the help you guys have posted.

As for the poor programming, sadly that is the way I have been taught to program up until now. My lecturer said that first year was just a "get it work no matter what the code is like" year, and that during this year, my second year, we learn why certain programming methods are better than others (Currently doing Data Structures and Software Engineering modules so they should help).

I shall also look through that angelfire link that was posted and see what I can learn [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Up until now with this work, I have just been following through the worksheets we are given and doing what they say each week. I wish I could do more but baby steps eh?

Anyways, thank you again for your help with this [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

EDIT: Forgot to add @EngineProgrammer, yes. I have the SDL Files separate from my project and I link to them in the properties Edited by MrChrisnis

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this