Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Platformer Jumping Issues  (Read 2203 times)
0 Members and 1 Guest are viewing this topic.
Offline Jay_PC

Senior Newbie





« Posted 2010-11-19 12:11:09 »

Im working on a Platforming shooter and for the Life of me right now I cant figure out how to make the player jump!

granted Im working in acceleration as well so it complicates things... My code is somthing like :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
byte gravity = 1;
byte jumpspeed = 10;
byte verticalAcceleration;

public void jump(){
  int y = player.getY;

  player.setY(y-verticalAcceleration); //Since negY is up

  if(verticalAcceleration <= 0){
     isFalling = true;
     isJumping = false;
  }

verticalAcceleration =  jumpSpeed - gravity;

}



I know that dosent quite work but if I had if figured out I wouldnet be asking for help!
Offline ryanm

Senior Duke


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #1 - Posted 2010-11-19 12:47:05 »

There seems to some confusion between speed and acceleration in your code.
  • Location - where your character is
  • Speed - rate of change of location
  • Acceleration - rate of change of speed

try something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
   float location = 0; // lets assume ground level is 0

   float speed = 0;

   float accel = -9.8f;// gravity

   /**
    * Call this every frame to update your location
    *
    * @param deltaSeconds
    *           The time period since the last frame, in seconds
    */

   public void advance( float deltaSeconds )
   {
      speed += accel * deltaSeconds;
      location += speed * deltaSeconds;

      if( location < 0 )
      { // we've hit the ground
         location = 0;
         speed = 0;
      }
   }

   /**
    * Call this when the jump key is pressed
    */

   public void jump()
   {
      if( location == 0 )
      {
         // this should cause us to jump about a meter high
         speed = 10;
      }
      else
      {
         // it's difficult to jump if you aren't standing on the ground
      }
   }
Offline dishmoth
« Reply #2 - Posted 2010-11-19 12:49:25 »

This may be closer to what you want.  If nothing seems to happen after calling startJump(), try increasing the jumpspeed value.  And if it's still not working, try printing out the player's y-position each frame (or step through the code in a debugger).
Simon

Edit: Posted before seeing Ryan's reply.  His looks better than mine. Roll Eyes

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
final int gravity = 1;
final int jumpspeed = 10;
int verticalSpeed = 0;

// called to start a jump
public void startJump() {
  verticalSpeed = jumpspeed;
  isJumping = true;
  isFalling = false;
}

// called once per frame
public void jump() {
  int y = player.getY;
  player.setY(y-verticalSpeed); //Since negY is up

  if(verticalSpeed <= 0){
     isFalling = true;
     isJumping = false;
  }

  verticalSpeed -= gravity;
}


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jay_PC

Senior Newbie





« Reply #3 - Posted 2010-11-20 11:33:48 »

Im having trouble understanding your code... I feel like the Falling action and the Jumping are combined.  I also have a state based collision system

touching GREEN isGrounded = true
touching blue isHitCeiling = true
 
#####
#####
#####
#####
#####

so the player falls if isGrounded is false as soon as it turns true the player stop falling, and if isHitCeiling becomes true the player will immediately start falling.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
float jumpSrartOne = 0; //this is the currrent Y of the player when on a platform. the Player stops falling when isGrounded = true
float jumpStartTwo = 0; //this is the current Y of the player then the player initializes a second jump


//these states change with collision between "floor" rectangles and the player.
boolean isGrounded; //True if player is on the ground
boolean isJumpOne; //True if player has jumped OR is no longer touching a "ground block"
boolean isJumpTwo; //True if player has jumped while in the air
boolean isFloating;   //True if payer has jumped twice and presses jump again
boolean isHitCeiling; //True if player has touched a ceiling block

boolean jumpOneOut; //when true player cant initialize another single jump until grounded
boolean jupTwoOut; //when true player cant initialize another double jump until grounded



final float GRAVITY = 9.8;
final float SINGLE_JUMP_POWER = 10;
final float DOUBLE_JUMP_POWER = 5;

public void update(){
    //this is called every frame
    if(player.getY >= jumpStartOne+maxSingleJump){
          jumpOneOut = true;
    }

    if(player.getY >= jumpStartTwo+maxDoubleJump){
          jumpTwoOut = true;
    }

    if(isGrounded){
           //do Nothing
      }else if(isJumpOne && !jumpOneOut){
           singleJump();
      }else if(isJumpTwo && !jumpTwoOut){
            doubleJump();
      }else if(isFloating){
            //float();
      }else{
            //fall();
      }
}


public void jump(){
     //called on jump key pressed
     if(isGrounded){
         isJumpOne = true;
     }else if(isJumpOne){
         isJumpTwo = true
     }else if(isJumpTwo){
         isFloating = true;
     }else if(isFloating){
         isFloating = false; //shuts off floating to free fall again
     }
}

public void groundPlayer(){
      //called when player touches a floor
      isJumpOne = false;
      isJumptwo = false;
      isFloating = false;
      isGrounded = true;
}

public void singleJump(){
        //working on single jump logic
}

public void doubleJump(){
       //working on double jump logic
}

public void float(){
      //working on Float logic.
}

public void fall(){
    //working on Fall logic
}



also fun fact my game takes a heavy influence from Vectorman for the sega genesis and My art style looks closer to the Scott Pilgrim game for the 360 Arcade. if you want to know what my inspiration was, and want to look em up.
Offline dishmoth
« Reply #4 - Posted 2010-11-21 10:21:51 »

Im having trouble understanding your code... I feel like the Falling action and the Jumping are combined.
When you reach the top of a jump, and you start moving down instead of up, then the behaviour is identical to falling.  At least that's how it works in the real world.  How it works in your game is entirely up to you.

Beyond that, it's not really clear to me what you're stuck on (or if you're stuck at all).

It's probably stating the obvious, but you should get the code for a basic jump working before you start adding code for double-jumps, floating, hitting-the-ceiling, etc.

Simon

Offline Jay_PC

Senior Newbie





« Reply #5 - Posted 2010-11-22 00:24:34 »

When you reach the top of a jump, and you start moving down instead of up, then the behaviour is identical to falling. 


I got it! hahahaha fall is cause by Gravity which constantly increases verticalSpeed unless player isGrounded, then jump only needs to change verticalSpeed's value in order to jump. then gravity will slowly change it back to falling, until isGrounded which sets verticalSpeed to 0.

Thank you for putting up with my crazy, I think better when I try to explain what im thinking. I get this part now.
1  
2  
speed += accel * deltaSeconds;
location += speed * deltaSeconds;


My thing is, how do I make That translate to pixels per frame?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2010-11-22 00:28:31 »

Thank you for putting up with my crazy, I think better when I try to explain what im thinking. I get this part now.
1  
2  
speed += accel * deltaSeconds;
location += speed * deltaSeconds;


My thing is, how do I make That translate to pixels per frame?

You don't, you simply render the sprite at (int)location.x

If you want to jump to a certain height, you have to either calculate the initial acceleration, or do some trial and error to find the value.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Jay_PC

Senior Newbie





« Reply #7 - Posted 2010-11-22 00:40:43 »

You don't, you simply render the sprite at (int)location.x

If you want to jump to a certain height, you have to either calculate the initial acceleration, or do some trial and error to find the value.

Edit: Ok I tried to implement this. This is my code:

1  
2  
3  
4  
5  
6  
7  
8  
   public void update(float deltaMilliseconds){
      float deltaSeconds = (deltaMilliseconds/100);
      verticalSpeed += GRAVITY * deltaSeconds;
      float location = verticalSpeed * deltaSeconds;
     
      r.setY(location);
      System.out.println(location);
   }


My results are interesting, because the delta value waivers back and forth.. sometimes making the location change in the opposite direction rapidly Example:
1  
2  
3  
4  
5  
6  
7  
8  
0.66247994
0.11759999
0.72128
1.9315802
0.63504
28.92176
0.21364
3.4251
Offline SimonH
« Reply #8 - Posted 2010-11-22 01:57:07 »

Check out the maths here then think of your character as a real thing in real time with real weight & gravity - that way it'll move like it's real! Don't worry about frames; when you draw transform the location from 'real' coordinates to 'screen' coordinates (pixels) and draw as it is in real time.

PS use real numbers! (float)

People make games and games make people
Offline Jay_PC

Senior Newbie





« Reply #9 - Posted 2010-11-22 02:31:10 »

Ill look into it, certain things I might leave out for simplicity sake, if anything I use Phys2D or somthing
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #10 - Posted 2010-11-22 03:21:48 »

Going a little further with Ryan's code:

Position The object's X/Y location at any given time, draw the object and calculate collision from here.
Velocity How fast the object is moving, in X/Y.
Acceleration Forces that change the velocity, like gravity or jumping.

Position is never directly updated. Velocity changes position.
Velocity is never directly updated. Acceleration changes velocity.


This means that you change an object's position only be applying an acceleration. Jumping is a big upward acceleration. Gravity is a constant downward acceleration.

See my work:
OTC Software
Offline Jay_PC

Senior Newbie





« Reply #11 - Posted 2010-11-22 07:02:05 »

Going a little further with Ryan's code:

Position The object's X/Y location at any given time, draw the object and calculate collision from here.
Velocity How fast the object is moving, in X/Y.
Acceleration Forces that change the velocity, like gravity or jumping.

Position is never directly updated. Velocity changes position.
Velocity is never directly updated. Acceleration changes velocity.


This means that you change an object's position only be applying an acceleration. Jumping is a big upward acceleration. Gravity is a constant downward acceleration.

I get that, I actually have something working now. AND it includes Double Jumping and Floating... i'm working on Understanding how to play with it now and trying to make it more.... friendly... its state based but Shoot me.

My issue is how to make the player not Fall Halfway through the floor. and how to make the jumps have longer hang time.

I also Realized I COMPLETELY failed in my last code snipit... I have the Values Initializing in the function. resetting them every time
Offline Jay_PC

Senior Newbie





« Reply #12 - Posted 2010-11-22 07:54:59 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
private float x = 0f;
   private float y = 0f;
   
   //these states change with collision between "floor" rectangles and the player.
   private boolean isGrounded = false; //True if player is on the ground
   private boolean isJumpOne; //True if player has jumped OR is no longer touching a "ground block"
   private boolean isJumpTwo; //True if player has jumped while in the air
   private boolean isHovering;   //True if payer has jumped twice and presses jump again
   private boolean isHitCeiling; //True if player has touched a ceiling block
   
   private Vector2f position = new Vector2f(x,y);
   private Rectangle boundingBox = new Rectangle(x,y,64,128);
   
   private final float MAX_LEFT_FORCE = 9.8f;
   private final float MAX_RIGHT_FORCE = 9.8f;
   private final float MAX_SINGLE_JUMP_FORCE = 9.8f;
   private final float MAX_DOUBLE_JUMP_FORCE = 9.8f;


   private final float GRAVITY = 9.8f;
   
   private float gravityMod;
   private float jumpForce = 0;
   
   private float leftForce = 0;
   private float rightForce = 0;
   
   private float verticalVelocity = 0f;
   private float horizontalVelocity = 0f;

   
   
   public void update(float deltaMiliseconds){
      float deltaSeconds = (deltaMiliseconds/1000);
      verticalVelocity += (GRAVITY * gravityMod) * deltaSeconds;
      verticalVelocity += jumpForce * deltaSeconds;
      position.y += verticalVelocity *deltaSeconds;
      horizontalVelocity += leftForce * deltaSeconds;
      horizontalVelocity += rightForce * deltaSeconds;
      position.x += horizontalVelocity *deltaSeconds;
      boundingBox.setX(position.x);
      boundingBox.setY(position.y);
   }
   
   public void jump(){
      if(isGrounded){
         jumpForce = -2;
         isGrounded = false;
         isJumpOne = true;
      }else if(isJumpOne){
         jumpForce = -2;
         isJumpOne = false;
         isJumpTwo = true;
      }else if(isJumpTwo){
         gravityMod = .1f;
         isJumpTwo = false;
         isHovering = true;
      }else if(isHovering){
         gravityMod = 1f;
         isHovering = false;
      }else{
         
      }
   }

Any Opinions? Suggestions?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (32 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!