Sign in to follow this  
ghigared

I need help

Recommended Posts

ghigared    133
How do you develop 2d jumping like the kind found in megaman, or Mario. I know you have to control y velocity I have gotten that far I even have my character jumping but he never caps his jump if I leave the jumping button pressed he will jump forever any help would be greatly appreciated and thank you in advance ps I'm using slick2d with Java

Share this post


Link to post
Share on other sites
Cornstalks    7030

You can either use a timer (i.e. "from now until the next second, give the player an upward y velocity; after this second is over, let gravity take effect."), or you could have a counter (which kinda acts like a timer; i.e. "if counter <= 0, let gravity take effect; if counter > 0, give upward y velocity; when jump button is pressed, set counter to 100 (or whatever) and decrement it every update loop"), or if you're doing a more "physically based" jump you can add some amount to the y velocity to make it positive and then let gravity slowly pull it back down.

 

However, in all of these methods, you're going to probably want to first check if the player is touching the ground when they press jump. If the player is not touching the ground, ignore the press. If the player is touching the ground, go ahead and jump.

Edited by Cornstalks

Share this post


Link to post
Share on other sites
larsbutler    733

How do you develop 2d jumping like the kind found in megaman, or Mario. I know you have to control y velocity I have gotten that far I even have my character jumping but he never caps his jump if I leave the jumping button pressed he will jump forever any help would be greatly appreciated and thank you in advance ps I'm using slick2d with Java

Have you modelled gravity into your game yet? If not, I would do that first. Place the character up in the air somewhere and apply gravitational forces to pull your character down. (By the way, you can apply this downward force all the time--which is physically realistic anyway.) You'll also need to implement basic collision detection and response between the character and a platform.

So with these components you have:
- character is up in the air
- gravity pulls it down
- platforms "catch" the character so it doesn't fall forever

With these in place, implementing jumping is simply a matter of giving the character an initial upward velocity when the key is pressed. The opposition of gravity will gradually decelerate the character, it will stop moving at the peak of the jump, and beginning falling. If implemented correctly, you should get a nice, smooth, parabolic jump. That's it in a nutshell, but there are some cases you need to account for. As Cornstalks said, you need to make sure you're already on the ground before you can jump. Also, you need all around collision response to handle, for example, what happens when the character jumps and hit its head on a platform above.

Hopefully that helps. Edited by thok

Share this post


Link to post
Share on other sites
SHC    155

Let's say you would like to jump up for 3 tiles each of height 16, check this

 

int x_pos, y_pos; // Current position
boolean jump = false;

int jump_y = 0; // The position to jump

public void doFrame(){
    if (onGround()){ // Implement this method
        if (isPressed(VK_SPACE) && !jump){
            jump = true;
            jump_y = y_pos - 16*3; // 3 tiles of height 16 px
        }
    }
    if (jump){
        y_pos -= y_vel;    // Move the character up
        if (y_pos <= jump_y){
            jump = false;
        }
    } else {
        // Apply gravity
        y_pos += y_vel;
    }

}

 

 

Hope this code helps.

Edited by SHC

Share this post


Link to post
Share on other sites
ghigared    133

(By the way, you can apply this downward force all the time--which is physically realistic anyway.

 

So when this force is applied all the time, when there is collision it doesn't change correct?

 

this :

player.setLocation(x, y=(float)(y +(9.8*gravity)));

 

seems to be working to pull an item forcefully down. I'm guessing that when there is collision the y location will be set to the position of the floor - the height of the player, and the gravity will be set to 0? Thank you very much for the help.

Share this post


Link to post
Share on other sites
ghigared    133

I figured it all out guys, thank you all for your help :) I'll keep this post alive, and show you guys my progress just incase any one is interested :)

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