Jump to content

  • Log In with Google      Sign In   
  • Create Account

I need help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 ghigared   Members   -  Reputation: 133

Like
0Likes
Like

Posted 24 January 2013 - 12:02 AM

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

Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6985

Like
1Likes
Like

Posted 24 January 2013 - 01:26 AM

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, 24 January 2013 - 01:26 AM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 thok   Members   -  Reputation: 692

Like
2Likes
Like

Posted 24 January 2013 - 05:44 AM

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, 24 January 2013 - 05:52 AM.


#4 SHC   Members   -  Reputation: 124

Like
0Likes
Like

Posted 24 January 2013 - 07:21 AM

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, 24 January 2013 - 07:21 AM.

Harsha...ch


#5 ghigared   Members   -  Reputation: 133

Like
0Likes
Like

Posted 25 January 2013 - 11:08 AM

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



#6 ghigared   Members   -  Reputation: 133

Like
0Likes
Like

Posted 25 January 2013 - 01:16 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS