I've spent the last 2 days working on slope/hills for my 2D plateformer game. I got the slope working and you can move up and down on it, but when the player starts jumping onto the slope he goes through it. I'm just wondering if this http://jnrdev.72dpiarmy.com/en/jnrdev2/ is the only way to implement slope that doesn't cause the jump bug or is it possible for the method I came up with.

This is my check slope method and it's only called when player is moving left/right or jumping

There are other ways, although this is the easiest. Typically you'd keep him from going through the slope by checking the total Y displacement you would be doing. If it's too large (more than half his height or something like that), then that means that he can't actually go through it and so should be repelled normally. This also makes sense for if you're trying to walk into a wall. Normally this method will place you all the way on top of the wall.

There are other ways, although this is the easiest. Typically you'd keep him from going through the slope by checking the total Y displacement you would be doing. If it's too large (more than half his height or something like that), then that means that he can't actually go through it and so should be repelled normally. This also makes sense for if you're trying to walk into a wall. Normally this method will place you all the way on top of the wall.

Thanks for the reply Demonpants! That gave me some ideas on how to fix up the slope problem, I'll go try it out now

This is my updated checkSlope code using Y displacement check like demonpants suggested, I can walk up and down, jump around the slope just fine now. But when I'm moving from higher tile onto a slope, "sometimes" the character will keep jumping if i don't press any key. I'm suspecting its because I didn't fully check when the player is entering a slope. So I added this if statement before my check slope method, but it only made things worse(player jumps more often now), any help would be appreciated.

New If statement I added to check if there's a slope going down near left/right of player

By "moving from a higher tile onto a slope," do you mean a tile that is at a Y of 15, dropping down to a slope at a Y of 12, or something like that?

By "keep jumping if I don't press any key," what do you mean exactly? Like when you have no buttons pressed they continue to go up and down as if you were tapping the jump button? Or they stay in the air? Or what?

It sounds like it's the bouncing thing, which doesn't seem like it makes sense to have as a problem. Usually you have a bool to check to see if you can currently jump at all, set to true if a block is below the player's feet, otherwise to false. Then you only jump if a jump button is being held down. I don't understand why your guy would continue jumping unless a button was being held down...

By "moving from a higher tile onto a slope," do you mean a tile that is at a Y of 15, dropping down to a slope at a Y of 12, or something like that?

By "keep jumping if I don't press any key," what do you mean exactly? Like when you have no buttons pressed they continue to go up and down as if you were tapping the jump button? Or they stay in the air? Or what?

It sounds like it's the bouncing thing, which doesn't seem like it makes sense to have as a problem. Usually you have a bool to check to see if you can currently jump at all, set to true if a block is below the player's feet, otherwise to false. Then you only jump if a jump button is being held down. I don't understand why your guy would continue jumping unless a button was being held down...

Sorry that I didn't phrase my problems clearly. For the first question, its the way you understand it, moving from a tile that is at Y of 15 to a slope at Y of 12.

For the second question the "keep jumping" is when no buttons are pressed the character continues to go up(shifted up Y by the checkSlope method) and down(falling down).

I also have a boolean to check if the player is onSlope or not, when onSlope is true the character cannot fall( when player is falling, I'm also calling the jump method but instead of going up, he goes down) but can jump up.

//Whether or not the jump button on the keyboard is being pressed.booleanjumpButtonPressed;//How much longer it is until the player is allowed to jump again.intjumpDelay;

publicvoidupdate(){//Some function or whatever that loops through all pressed down keys.//Also you might just be using keyPressed(KeyEvent).doKeyboardInputForAllKeys();

if (player.hasBlockUnderneath()) {jumpDelay--; }else {//JUMP_WAIT_TIME is some constant that determines for how many timesteps you want the player to "rest" after a landing.//Also can be based on an animation rather than a timer.jumpDelay = JUMP_WAIT_TIME; }

if (jumpDelay <= 0 && jumpButtonPressed) {//Normally you would probably put this in a player.jump() function.player.velocity.y += player.jumpSpeed; }}

Hm, well shouldn't the jumping be linked to a button press, so he can't jump if no button is being pressed at all?

Also your code seems to be weirdly coupled, which will give you lots of problems. Collision, jumping, etc. should all be separate, not mashed together like that. Something like the pseudo code below:

Does that maybe enlighten you at all?

Thanks for the reply Demonpants! Thanks for the pseudo code too! Actually I do link jumping to a button, below I'll post my code for the missing part of my code. Also the fallCheck method from previous post is to check whether a player is standing on a walkable tile or not, if not then it moves player down, the actual jumping still requires a player to press the space bar.

I think I've found the problem of my jumping bug, its actually the problem of my slope checking method. I'll explain with a picture so its easier to understand.- The blue square is the player, and the center buttom white square is my check point used to check for slope(highlighted by green circle)

-The part highlighted by red circle is where my problem is.

The Slope method works fine in picture 1 and 2, I can move up and down, jump on the slope and its all working fine.

I think in picture 3 its suppose start shifting player to the correct Y displacement, but for some reason the onSlope boolean is false(only shift player if its true) and player shifting never takes place until step 4. I have tried adding an if statement to check if the re is a slope below the player when he's moving from a high tile onto slope but that didn't work out either. I've spent days on this problem so any help would be greatly appreciated. Thanks in advance!

if statement to check if a player is moving from a high tile onto slope

Run your game in an IDE. Create a situation in your game that causes the problem (ie when the box doesn't follow the slope like it should). Set a breakpoint. Step through your code, checking the value of variables as it executes. You should find your check for "onSlope" is returning false. Stepping through will let you see why it returns false, and that is your problem. Post the check that is wrong, then we can help further.

"y + 1" is intended to check for slope below player, wouldn't y-1, -2 be checking the slope above player? I think my problem happens when entering/exiting the slope, so I'm doing the check suggested by Nate now and I'll post back with the results. Thanks again for the help =)

Here are the test results:

Note:- checkX/checkY is the X/Y position to check slope collision(where the white square is)

- onSlope is a boolean to tell if player is currently on a slope

- playerX/playerY is the current position of player(top left of the blue square)

The problem was at the if statement that checks if player is moving from a higher tile onto a slope. I made some slight modifications and added (int) cast to the check and it works There are still some minor bugs like mini screen shake when moving on slope and the moving from slopt -> higher tile not playing the correct running animation(it plays jumping), but other than that the slope is looking good, now I need to work on shifting player from Map 1 to a different map and I have a half working game Thanks again for the help!

Aaaah, yes I've had rounding errors like that too. I typically would typecast to an int as you did or instead of looking for equality subtracting them and looking for a range less than 0.00001 or something.

Aaaah, yes I've had rounding errors like that too. I typically would typecast to an int as you did or instead of looking for equality subtracting them and looking for a range less than 0.00001 or something.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org