Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: need help with basic jump command

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default need help with basic jump command

    here is what i am trying to do:
    I am trying to make my player jump if user hit the space bar. i am just trying to set up basic jummping nothing too hard.


    the problem:
    when user hold space bar, player move up 30 pix(jump) and it stay at the point. if user let go of space bar then player come down.
    i want it so that when user hit space bar, player go up and when reach max jump height than move down. even though user is holding down space bar.


    variable infor
    this code is in while loop.
    jump = true -> means user has press space bar
    jump = false -> means user has let go of space bar
    jump_height -> is the height of jump. so it cant jump height than 20 pixs.
    jump_counter ->start at 0. which will count the pix which player jump. - this way u cant jump height 30 pix


    if(jump == true)         //user hit space bar
    	      {
    		 if(jump_counter < jump_height)    //(0 < 30)
    			{
    				y -= dy;           //move player up
    				jump_counter++;	 //counter howmany pix it's going up
    			}
    		}
     
    		if(jump == false)     //user has let go of space bar
    		       {
    			       if(jump_counter > 0)      //counter should be 30 now 
    				{
    					y += dy;             //move player down
    					jump_counter--;  //count how many pix it going down
    				}
    			}


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: need help with basic jump command

    You indentation is horrendous making it difficult to read your code.

    When does jump get set to false? I understand your answer will be when user releases the space bar but in the code above changing the value of jump is done elsewhere so the above code must be executed twice: once when jump is true so the first if is executed and character stays in mid-air, the second time jump is false, second if is executed and then the character is moved done.

    So why have two if statements at all?

    Pseudocode:
    if player pressed space bar {
        move character up x pixels
        move character down x pixels
    }
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: need help with basic jump command

    yes, jump get to set to false when user let go of space bar. the code for that is in different file. so here is what i thinking:

    if(jump == true){
    if(jump_counter < jump_height){ //move up 30 pix's
    y -= dy;
    jump_counter++;
    }
    else if(jump_counter == jump_height) //if it hit 30 pix, move down
    {
    y += dy;
    jump_counter--;
    }

    }//end of main if


    i the problem will be it will go in
    if(jump_counter < jump_height)
    than
    else if(jump_counter == jump_height)
    than
    if(jump_counter < jump_height)
    than
    else if(jump_counter == jump_height)
    and that will go on for ever

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: need help with basic jump command

    Use a gravity to have a constant downward force.

    When jump is pressed set the vertical speed to a higher number. 10 for example. Every cycle apply gravity. In this example lets call gravity 2.
    Immediately when jump is pressed the player is going "up" (jump direction up) at a speed of 10. Every cycle 10 gets reduced by 2. So 10, 8, 6, 4, 2, 0....
    This means on the first cycle the position on the y axis (in this example) went from 0 to 10. Then from 10 to 18 on the next cycle, still going "up" but not as fast as before. Next cycle puts player at 24. Then at 28. Next step 30, followed by the final step of 0, leaving player at height of 30 for two cycles in a row. This gives that slight float effect as the player crests over the height of the jump. Now as high as we could possibly jump, still being pulled by gravity downward, the next cycle pulls player down to 28. Gravity remains constant of 2 downward, but speed increases the longer player falls. Y speed goes up by 2 each cycle on the way down. So from 28 player adjusts 4 down, putting player at 24. Then 18. Then 8. 10. and finally back on the ground.

    Trigger the jump cycle from when the button was pressed down, and don't trigger it again until the next time the button is pressed down, (which means it has to have been released first).

    This will give greater control over the player's positioning and will not matter how long someone may hold the space bar when jumping. Use the held down spacebar to trigger things like float effects and the like. Trigger double jump by pressing the space bar again while not on a surface.

  5. The Following User Says Thank You to jps For This Useful Post:

    hwoarang69 (October 22nd, 2012)

  6. #5
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: need help with basic jump command

    ah i see. so i just want to make sure i understand this right.
    vertical will be how height the player can jump. in this ex it's 10 pixs.
    gravity will be the force pulling down.


    [code]private int gravity = 2;
    private int vertical = 10;

    while(true){
    if(jump == true){
    y -= vector;
    vector -= gravity;
    }
    }[code]


    so now what is happening is that player will jump up fast, slow down at top and come down fast?


    also this code will keep going down for ever. to fix this problem i will have to set of if statment inside of "if(jump == true)"?

    so for ex

    while true
    if jump == true
    if vector != 0 ???
    y -= vector;
    vector -= gravity;
    else

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: need help with basic jump command

    vertical will be how height the player can jump. in this ex it's 10 pixs.
    Actually the 10 refers to the speed on the y axis. When jump is pressed, the "up" speed is 10. How you treat this depends on how you treat the coordinate system on the screen.
    Java counts the top left corner as 0, 0. That is x is at 0 and y is at 0.
    The far right hand side is the highest value x can have and still be drawn on the screen. Known as x.maximum or display.width.
    The bottom of the screen is the highest value y can have and still be drawn on the screen. Known as y.maximum or display.height.

    Now that we have a common reference point, in this system the ySpeed or verticalSpeed would be set to -10 when jump is pressed.

    Continue the previous example.
    Lets say the top of the dirt we walk on, ground, is at 100.
    So in code we may have a class Ground, of which we have an object, ground.
    ie Ground ground = new Ground(100);//100 sets the height of the top of the surface in the Ground constructor, lets say.....
    Now ground.height will return the 100....

    In code we may have a Player class, of which we have an object, player.
    ie Player player = new Player();//A player.
    Now we can get various info from our player.
    player.footHeight //100 right now because we just spawned on the ground.
    player.eyeHeight //120 right now because the eyes are 20 pixels higher than the feet.
    player.headHeight //125 right now because the player is 25 pixels high overall
    player.ySpeed //0.0 because when standing on the ground, we are not jumping or falling

    The same thing for the gravity in this level.
    Level level = new Level(2.0);//2.0 sets gravity in the constructor
    level.gravity //returns the 2

    Now a series of steps during the game loops.

    loop: player did not jump | apply gravity to ySpeed | adjust yPos by ySpeed
    _ _ _ nothing to do yet! _ _ (2.0) + (0.0) = (2.0) _ _ (100) + (2.0) = 102 __But 102 triggers the restriction of ground = 100, so yPos is set to 100 and ySpeed set to 0.0 for hitting the ground.

    loop: player did jump | apply gravity to ySpeed | adjust yPos by ySpeed
    _ _ _ ySpeed = -10 _ _ (2.0) + (-10) = (-8.0) _ _ (100) + (-8.0) = 92

    loop: player in jump | apply gravity to ySpeed | adjust yPos by ySpeed
    _ _ _ still jumping _ _ (2.0) + (-8) = (-6.0) _ _ (92) + (-6.0) = 86

    still jumping (2.0) + (-6) = (-4.0) .... 82
    still jumping (2.0) + (-4) = (-2) ...... 80
    still jumping (2.0) + (-2) = (0) ....... 80 //note the slight float effect again
    now falling (2.0) + (0) = (2) ......... 82 //increasing y moving player toward bottom of screen
    falling (2.0) + (2) = (4) ............... 84 //falling faster and faster til ground again..
    falling (6) .................................. 90
    falling(8) ................................... 98
    falling(10) ................................. 106 // But 106 triggers the restriction of ground = 100, so yPos is set to 100 and ySpeed set to 0.0 for hitting the ground.

    so now what is happening is that player will jump up fast, slow down at top and come down fast?
    Yea, exactly. The real life design.
    But you could do it different, if that is the effect you want. Something like the above, but say instead of setting ySpeed to -10 for a jump, set it to -4. Then when jump is pressed, apply an additional -4 to ySpeed for a count of x number of cycles. Then your up speed is constant during a jump. On the way down, set ySpeed to 4 (for example) to maintain a constant fall speed.
    You could also limit fall speed to a cap, so long falls don't move the player at undesired speeds.
    if(ySpeed > Level.MAX_FALL_SPEED) { ySpeed = Level.MAX_FALL_SPEED); }



    I assume you are using a KeyListener? With the keyPressed method?
    In the keyPressed method, just call playerJumped();
    This method should handle everything involved in a jump as far as a jump is concerned.
    Signal for a jump sound to be played.
    Make necessary adjustments to the player's existing movement code.
    (Adjustments would include setting y speed to an additional -10 (according to our example).)

    Now as the natural cycle of player movement occurs, a push upward has been entered into the cycle. At this point all of the calculating positions and speeds take place. But all of this happens every cycle anyway. All we need to do to cause a jump was throw in that upward push and let gravity pull player back down. All of this type code belongs in a method like tickPlayerPos or updatePlayerLocation or something to the like, and the ySpeed is a variable we can set from playerJumped to introduce a jump by subtracting -10. Later when the player location update occurs, the -10 begins the upward fling.

  8. The Following User Says Thank You to jps For This Useful Post:

    hwoarang69 (October 22nd, 2012)

  9. #7
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: need help with basic jump command

    let me first say, thanks alot for helping me out, jps. I am starting to see how gravity works in 2d game, still have just a difficult.


    i am trying to give a effect if jump up fast. slow down at top. and come down fast. and stop on ground.

    later ill change my while loop to timer. i just want to get my jump set up first.
    in my main.java, i have 2 keylistener set up
    1st keyevent for press
    if (keys == KeyEvent.VK_UP){
    player_class.hitJUMP();
    }


    ... than 2nd keyEvent for release
    if (keys == KeyEvent.VK_UP){
    player_class.stopJUMP(); //thsi method just set ---jump = false;
    }


    ..player.java
    dx = 2; //speed of player
    dy = 30; //height of player
    velocity_Y = -10; //speed of jump
    gravity = 2;


    ...

    while(true){
    if(jump == false) {
    velocity_Y -= gravity; //dont think i need this
    if(y+30 > g.getY()) //if player hit ground(+30 for player height)
    {
    y = g.getY()-30; //set player y pos (-30 for height)
    velocity_Y = 0; //hit ground?
    }
    }

    else if(jump == true && jump_lock == false){ //user hit jump and player on ground
    velocity_y += gravity; //
    y -= velocity_y; //change y position
    }

    else if(jump == true && jump_lock == true){ //user hit jump and player is in air
    velocity_y += gravity;
    y -= velocity_y;
    }
    }




    when jump is false that seem to be working find.
    but when u hit jump. player goes down. and when u let go of jump key, it come back on ground

  10. #8
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: need help with basic jump command

    yaa. I almost got it to working.

    [B]private int dx = 1; //player speed
    private int dy = 30; //height of jump
    private int gravity = 2;
    private int velocity_Y = 10; //speed of jump


    while(true){
    if(jump == true) {
    velocity_Y += gravity;
    y -= velocity_Y;
    }

    else if(jump == false) {
    velocity_Y -= gravity; //dont think i need this
    y -= velocity_Y;
    if(y+30 > g.getY()) //if player hit ground(+30 for player height)
    {
    y = g.getY()-30; //set player y pos (-30 for height)
    velocity_Y = 0; //hit ground
    }
    }
    }


    my player seem to be jumping almost fine now. but there are 2 problem.

    pro#1 - when i hold up key it keep up going up for ever. but when i let go it does move down on ground fine.
    pro#2 - right now it move up really fast. i want some control over it. for ex how height can player jump. how fast can player jump.

    is dy -height can player jump?
    velocity_Y - fast can player jump?
    Last edited by hwoarang69; October 23rd, 2012 at 12:41 AM.

  11. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: need help with basic jump command

    i am trying to give a effect if jump up fast. slow down at top. and come down fast. and stop on ground.
    Give a higher starting jump speed than used in the example. Then increase the downward pull of gravity to compensate. Adjust both values until the jump gives the arch you desire.



    please see the announcements page for the use of code tags.



    In post #8 the code has an infinite loop. I do not understand what the code is supposed to do.



    is dy -height can player jump?
    velocity_Y - fast can player jump?
    There is no reason to calculate how high a player can jump. You add an upward force to a player's vertical speed, and they begin to go up. As long as they have a vertical speed of any value the player is either moving up or down. The height a player jumps depends on the height the player is at when jumping, and how much upward force is applied. Once that push has been given to the player, all of the code that has to do with the jump is finished. From that point forward the code that normally moves the player is responsible for completing the upward move, the float at the climax, and the landing. I get the impression you are trying to do all of the move in the jump method. Maybe you are. Maybe that is the effect you want.

    I feel like my hints of how some things work are turning into a "write your code my way" post. That is not my intention. I suggest you get a good idea of what you want to happen, and write down the steps to make it happen. Finally turn those steps into code.

  12. The Following User Says Thank You to jps For This Useful Post:

    hwoarang69 (October 23rd, 2012)

Similar Threads

  1. Second command to socket doesn't get through
    By treshr in forum JDBC & Databases
    Replies: 11
    Last Post: August 27th, 2011, 09:05 AM
  2. Program.jar -command
    By luigi10011 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 23rd, 2011, 09:49 AM
  3. [SOLVED] Send a F5 command
    By mds1256 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 18th, 2011, 03:23 AM
  4. jump use
    By TarunN in forum Java Theory & Questions
    Replies: 6
    Last Post: April 30th, 2010, 04:03 AM
  5. How To Make The Program Jump To The Next Function..
    By Kumarrrr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 3rd, 2010, 12:33 AM