-2
\$\begingroup\$

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;
        }
\$\endgroup\$
4
  • 4
    \$\begingroup\$ 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/… \$\endgroup\$
    – House
    Oct 30, 2012 at 4:59
  • \$\begingroup\$ yes, i am trying to make gravity :) \$\endgroup\$ Oct 30, 2012 at 5:24
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Roy T.
    Oct 30, 2012 at 9:24
  • \$\begingroup\$ @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. \$\endgroup\$
    – House
    Oct 30, 2012 at 18:12

2 Answers 2

-3
\$\begingroup\$

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Maybe at some links for further reading to support your concepts \$\endgroup\$
    – floAr
    Oct 30, 2012 at 9:59
  • \$\begingroup\$ 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) \$\endgroup\$
    – LucianMLI
    Oct 30, 2012 at 11:28
  • \$\begingroup\$ @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. \$\endgroup\$ Oct 30, 2012 at 11:40
  • \$\begingroup\$ -1 This answer is considerably less useful than the answers linked in my first comment. \$\endgroup\$
    – House
    Oct 30, 2012 at 13:15
  • \$\begingroup\$ and thank you too Marcks Thomas this actually is help i didn't do that yet :) \$\endgroup\$ Oct 30, 2012 at 21:44
1
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .