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 can I implement gravity?

I want to make the jumping that my character does more realistic. This is what I've tried so far but it doesn't seem very realistic when the player jumps. I want it to jump up at a certain speed then slow down as it gets to the top then eventually stopping (for about one frame) and then slowly going back down but going faster and faster as it goes back down. I've been trying to make the speed at which the player jumps up slow down by one each frame then become negative and go down faster... but it doesn't work very well

    public bool isPlayerDown = true;
    public bool maxJumpLimit = false;
    public bool gravityReality = false;
    public bool leftWall = false;
    public bool rightWall = false;

    public float x = 76f;
    public float y = 405f;



      if (Keyboard.GetState().IsKeyDown(up) && this.isPlayerDown == true && this.y <= 405f)
        {
            this.isPlayerDown = false;
        }

        if (this.isPlayerDown == false && this.maxJumpLimit == false)
        {
            this.y = this.y - 6;
        }

        if (this.y <= 200)
        {
            this.maxJumpLimit = true;
        }

        if (this.isPlayerDown == true)
        {
            this.y = 405f;
            this.isPlayerDown = true;
            this.maxJumpLimit = false;
        }

        if (this.gravityReality == true)
        {
            this.y = this.y + 2f;
            this.gravityReality = false;
        }

        if (this.maxJumpLimit == true)
        {
            this.y = this.y + 2f;
            this.gravityReality = true;
        }

        if (this.y > 405f)
        {
            this.isPlayerDown = true;
        }
share|improve this question

marked as duplicate by Byte56, Jonathan Hobbs, Laurent Couvidou, Tetrad Oct 30 '12 at 23:10

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.

4  
This isn't the place for "Here's my code, how do I fix it?", it's too localized. Check the other questions on the site about implementing jumping. It sounds like you're trying to implement something like gravity. gamedev.stackexchange.com/questions/20441/… gamedev.stackexchange.com/questions/8492/… –  Byte56 Oct 30 '12 at 4:59
    
yes, i am trying to make gravity :) –  Seth Taddiken Oct 30 '12 at 5:24
1  
If you have a parabolic formula f(x) that describes the height you can take the derivative f'(x) for speed and it will roughly give you the effect you want. –  Roy T. Oct 30 '12 at 9:24
    
@SethTaddiken See the question: gamedev.stackexchange.com/questions/15708/…. Please search the site before posting in the future. We try to keep duplicates to a minimum. Thanks. –  Byte56 Oct 30 '12 at 18:12
    
Thank you Roy T! –  Seth Taddiken Oct 30 '12 at 21:42

2 Answers 2

I think you're overcomplicating things. The usual way to implement realistic jumping goes something like this:

private float verticalSpeed;
private float verticalPosition;

// ...

float groundLevel = highestSurfaceUnderPlayer();

boolean airborne = (verticalSpeed > 0 || verticalPosition > groundLevel);

if ( !airborne && jumpKeyPressed() ) {
    verticalSpeed = initialJumpSpeed;
    airborne = true;  // just jumped off the ground
}
if ( airborne ) {
    float speedChange = -gravity * timestep;  // could add other forces here
    verticalSpeed += speedChange;
    // second order formula minimizes effect of timestep on trajectories:
    verticalPosition += (verticalSpeed - speedChange / 2) * timestep;
    // update airborne state in case we just hit the ground:
    airborne = (verticalSpeed > 0 || verticalPosition > groundLevel);
}
if ( !airborne ) {
    verticalSpeed = 0;
    verticalPosition = groundLevel;  // don't let player fall through ground
}

Of course, you also need to update the horizontal position and velocity of the player too, but I've left that out for the sake of simplicity. Also, you'll probably want to implement ceilings too.

share|improve this answer

Define speed and acceleration(gravity). Start with max speed and apply acceleration to it based on time(or frames). There are a few simple equations on the net no big deal. Basic physics apply, it's no rocket science.

share|improve this answer
1  
Maybe at some links for further reading to support your concepts –  floAr Oct 30 '12 at 9:59
    
en.wikipedia.org/wiki/Classical_mechanics <br/>d+=jump_speed; <br/>jump_speed+=acceleration <br/> since jump is up and gravity is down acceleration would be negative so speed would end up 0 at top and (-jump_speed) down. start with jump_speed positive(initial speed) –  LucianMLI Oct 30 '12 at 11:28
    
@FloAr: As it stands, the answer might not be very informative, but I think these concepts are so profoundly basic that they can be assumed to be familiar. Any hit on a Google search for 'physics' can rightfully be classified as further reading. –  Marcks Thomas Oct 30 '12 at 11:40
    
-1 This answer is considerably less useful than the answers linked in my first comment. –  Byte56 Oct 30 '12 at 13:15
    
and thank you too Marcks Thomas this actually is help i didn't do that yet :) –  Seth Taddiken Oct 30 '12 at 21:44

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