Possible Duplicate:
How to make a character jump?

I have been working on a 2D platformer and am trying to implement jumping. At the moment I have it simply teleporting upward and then falling back down:

if(Keyboard.GetState().IsKeyDown(Keys.Space) && !jumping) _position2D.Y -= 64f;

Is there any way to smooth it out and make it more parabolic?


Some variables:

    Vector2 velocity;
    Vector2 fallSpeed;

    // Init
    velocity = Vector.Zero;
    fallSpeed = new Vector(0, 20)  // This is our pseudo-gravity

    // Time is needed 
    var elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

    velocity = new Vector2(0, velocity.Y); // Keep our change in the Y-axis from previous Updates.

    if (Keyboard.GetState().IsKeyDown(Keys.Space) && !jumping)
        jumping = true; // So we don't jump again til ready.

        velocity.Y = -500; // break through our pseudo-gravity coming up.

    if (jumping)
        velocity += fallspeed;  // not blocking this off will make you entity continue to drift down after he has landed.

    var moveAmount = velocity * elapsed; // store how much we moved in this given time period

    position = position + moveAmount;

//Assuming you have no other collision logic we need a way to tell our guy he is no longer in the state of jumping and can jump again.

    if (position.Y >= 300)
        jumping = false;

        velocity.Y = 0;

Tweak jump strength and fallSpeed to you liking


I would think the logic should be a loop up to the peek jump point like so:

if(Keyboard.GetState().IsKeyDown(Keys.Space) && !jumping) {
    float peek = 64f;
    while(_position2D.Y != peek) { _position2D.Y += 1; }

Personally, and this is just me, I would move the variable peek out to a more publicly accessible point so that it can be easily edited/referenced. That is just me though.

[edit] As pointed out in the comments, this might seem like good logic at first glance, but is made of fail in practice, for the reasons attached to the comments. I'm leaving this here for those wanting to learn from my uber mistake =D

  • 2
    \$\begingroup\$ This achieves the same effect, however, it only makes the jump bigger. The y position is being added far too quickly and so the player teleports upward. \$\endgroup\$
    – x3zinja12
    Oct 20 '12 at 5:16
  • 1
    \$\begingroup\$ -1 No, this won't work. Putting a while loop inside the update loop just runs through the entire while loop inside the update loop. That means the entire jump takes place inside a single frame. Which is exactly the same (visually) as just setting it to that value. \$\endgroup\$
    – House
    Oct 20 '12 at 7:28
  • \$\begingroup\$ @x3zinja12 Sigh Yeah, I figured that out about 2 hours into my MtG game, sorry for that. I came back to adjust my answer. \$\endgroup\$ Oct 20 '12 at 8:41
  • \$\begingroup\$ OK, as long as you know what you did was wrong. :) \$\endgroup\$
    – House
    Oct 20 '12 at 17:39
  • \$\begingroup\$ Yeah, once I realized it I felt kinda shameful about it, lol. It was funny I was sitting there and it felt like one of those slow motion moments of "Wait,... noooooooo", as what I had said hit me. :$ \$\endgroup\$ Oct 20 '12 at 18:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.