Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by Byte56, David Gouveia, Noctrine Oct 20 '12 at 14:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
    
Thank you very much, sir! This has helped me greatly. –  x3zinja12 Oct 20 '12 at 12:53

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

share|improve this answer
2  
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. –  x3zinja12 Oct 20 '12 at 5:16
1  
-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. –  Byte56 Oct 20 '12 at 7:28
    
@x3zinja12 Sigh Yeah, I figured that out about 2 hours into my MtG game, sorry for that. I came back to adjust my answer. –  Angel Brighteyes Oct 20 '12 at 8:41
    
OK, as long as you know what you did was wrong. :) –  Byte56 Oct 20 '12 at 17:39
    
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. :$ –  Angel Brighteyes Oct 20 '12 at 18:47

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