8 Replies - 2936 Views - Last Post: 23 December 2010 - 02:40 PM

#1 xorangejuicex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 31-August 10

Help with jumping logic

Posted 19 December 2010 - 08:02 PM

So I'm trying to make a platformer game but I can't figure out how to make my character jump. Here's my code:

            if (currentKBState.IsKeyDown(Keys.Space) == true)
            {
                Jump(gameTime);
                charPosition.Y -= jumpHeight;
                jumpHeight++;
                if (jumpHeight == 20)
                {
                    jumpHeight = 0;
                }
            }


Jump(gameTime) just takes care of switching the animation.

When I press space, the sprite with jump but it doesn't fall. It just keeps going up. And if I change my code to this
            if (currentKBState.IsKeyDown(Keys.Space) == true)
            {
                Jump(gameTime);
                charPosition.Y -= jumpHeight;
                jumpHeight++;
                if (jumpHeight == 20)
                {
                    charPosition.Y += jumpHeight;
                    jumpHeight--;
                }
            }


it will fall but continues to fall until I let go of space. However it won't go up again if I press spacebar. So if anyone could help me, that'd be great. Also, how would I got about making it so that the spacebar only has to be pressed to jump instead of being held down?

Is This A Good Question/Topic? 0
  • +

Replies To: Help with jumping logic

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Help with jumping logic

Posted 19 December 2010 - 08:24 PM

The best way to accomplish this would be to create a previousKBState variable to keep track of the button that was pressed in the previous frame. Here's how it's done:
// In the declarations section of your code
KeyboardState currentKBState;
KeyboardState previousKBState;

...

// Then you need to place this at the beginning of your jump logic
previousKBState = currentKBState;
currentKBState = Keyboard.GetState();

...

// Then when you check for the spacebar being pressed
if (currentKBState.IsKeyDown(Keys.Space) && !previousKBState.IsKeyDown(Keys.Space))
{
    // Do some stuff
}


Was This Post Helpful? 3
  • +
  • -

#3 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Help with jumping logic

Posted 20 December 2010 - 09:54 AM

As far as jumping goes, I like to assign the player a velocity and an acceleration:

public void Update(GameTime gameTime)
{
    float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
    float deltaTimeSquaredDividedByTwo = (deltaTime * deltaTime) / 2;

    Position.X += (Velocity.X * deltaTime) + (Acceleration.X * deltaTimeSquaredDividedByTwo);
    Position.Y += (Velocity.Y * deltaTime) + (Acceleration.Y * deltaTimeSquaredDividedByTwo);

    Velocity.X += Acceleration.X * deltaTime;
    Velocity.Y += Acceleration.Y * deltaTime;
}



When I initialize the player, I would set the acceleration:

player.Acceleration = new Vector2(0, 10);


Then, when the player wants to jump, I would set the velocity:

if (currentKBState.IsKeyUp(Keys.Space) && previousKBState.IsKeyDown(Keys.Space))
{
    Velocity.Y = -20;
}



Just another option. :)
Was This Post Helpful? 3
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Help with jumping logic

Posted 20 December 2010 - 12:14 PM

That's not a bad idea, lesPaul456. That gives the programmer the option to control how fast the character actually rises and falls with the jump.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,590
  • Joined: 27-December 08

Re: Help with jumping logic

Posted 20 December 2010 - 01:43 PM

Also, with lesPaul456's formula, you can extrapolate the maximum height and total time by differentiating the function and finding when the time that solves for when the derivative = 0.

I have a tutorial on Math and 2D Gravity you may find helpful. :)
Was This Post Helpful? 1
  • +
  • -

#6 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Help with jumping logic

Posted 20 December 2010 - 01:51 PM

That's awesome macosxnerd101. I'll definitely be reading that later.
Was This Post Helpful? 0
  • +
  • -

#7 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Help with jumping logic

Posted 21 December 2010 - 12:33 PM

Double Post!

I just wanted to mention that I just read through all three of your math tutorials, macosxnerd101 and now my head hurts a bit. Then again, I'm about half asleep right now because work is so boring today, so that might have something to do with it. Awesome little series of tutorials for game programming related math.
Was This Post Helpful? 0
  • +
  • -

#8 xorangejuicex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 31-August 10

Re: Help with jumping logic

Posted 21 December 2010 - 08:07 PM

Sorry for the late reply, but thank you guys for your tips and whatnot, it's definitely helped a bunch.
Was This Post Helpful? 0
  • +
  • -

#9 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Help with jumping logic

Posted 23 December 2010 - 02:40 PM

Did you manage to get the jumping logic working?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1