TIGSource Forums

Developer => Technical => Topic started by: danlthemanl on April 22, 2011, 09:32:15 PM



Title: Velocity, Acceleration, Gravity help
Post by: danlthemanl on April 22, 2011, 09:32:15 PM
I've been trying to get smooth platforming down all day, and I can't seem to figure it out. I've gotten smooth left/right movement, but jumping is stumping me.

Can somebody explain to me how velocity, acceleration, friction and gravity are suppose to work together? I am lost.


Title: Re: Velocity, Acceleration, Gravity help
Post by: genericuser on April 23, 2011, 03:15:46 AM
For platform jumping, you'll most likely want to think of them like this:

  • Gravity is a constant force, pointed down.
  • Platform-style jumping is a sudden acceleration, pointed up.

In other words - the vertical velocity of the player goes up only when the player is on the ground, and when the player presses the jump key. Meanwhile, you apply gravity for each step; most likely you'd do this by subtracting a small amount from the vertical velocity for each step.

To get smooth jumping, you can tune the gravity and the amount of acceleration added when the player jumps.

Put as pseudo-code:

Code:
-- in a loop --
IF player is grounded AND player presses jump key
    set vertical acceleration of player to <a certain amount>
IF player is not grounded
    subtract <a small amount> from vertical acceleration of player


Title: Re: Velocity, Acceleration, Gravity help
Post by: J. Kyle Pittman on April 23, 2011, 07:17:25 AM
Your basic Euler integration should look something like this:

Code:
// Acceleration is set each frame depending on the state of the game.
acceleration = (whatever)

// Velocity changes over time based on acceleration.
velocity = velocity + (acceleration * time since last frame)

// Position changes over time based on velocity.
position = position + (velocity * time since last frame)

So for a platformer with gravity and friction, your acceleration would be something like:

Code:
// Always apply gravity, even if we're already on the ground.
// (Assumes that there is a collision system to prevent us from falling through the ground.)
acceleration = gravity constant * downward vector

// If we're on the ground, also include a frictional force opposite the direction of movement.
if on ground:
    acceleration = acceleration + (surface friction constant * -velocity)

velocity = velocity + (acceleration * delta time)
position = position + (velocity * delta time)

Then, like GenericUser said, you'll want to apply an immediate increase in vertical velocity when jump conditions are met (player is on the ground and the jump key is pushed).

Code:
if on ground and player pressed jump:
    velocity = velocity + (jump constant * upward vector)

If you have a fixed frame time, you can also further simplify these functions by removing the multiplication by delta time. You might have to adjust your constants to compensate, though.

You can also split these up into separate X and Y calculations if you prefer not to work with vectors. The math will be the same in either case.


Title: Re: Velocity, Acceleration, Gravity help
Post by: danlthemanl on April 23, 2011, 06:27:50 PM
I'm still having troubles, Whenever I press the jump key it jumps fast and very jerky.
Code:
               position.y += velocity.y;

//gravity
velocity.y += accel.y * delta;
if (velocity.y > 9.5){
velocity.y = 9.5f;
}
if (position.y <= 300){
onGround = false;
}
else{
onGround= true;
}
if(onGround){
//ground resistance
velocity.y -= 9.5f;
}
}
private void jump(int delta, Input input){
if (onGround && Keyboard.isKeyDown(Input.KEY_UP)){
//sudden acceleration;
velocity.y -= 3.0f;
}
}


Title: Re: Velocity, Acceleration, Gravity help
Post by: ThemsAllTook on April 25, 2011, 07:24:00 AM
Code:
-- in a loop --
IF player is grounded AND player presses jump key
    set vertical acceleration of player to <a certain amount>
IF player is not grounded
    subtract <a small amount> from vertical acceleration of player

You might run into some problems if you only apply gravity if the player is not grounded. For example, when they walk off an edge without jumping, unless you have some additional code to detect that case, they'll just keep walking through the air as if it's solid. The way I do this is that for each step of game logic, I set grounded to false, apply gravity, run collision detection, then set grounded to true for that step if there was a ground collision.


Title: Re: Velocity, Acceleration, Gravity help
Post by: David Pittman on April 25, 2011, 09:23:00 AM
I'm still having troubles, Whenever I press the jump key it jumps fast and very jerky.

You're not multiplying your velocity by delta time when integrating position.
Code:
position.y += velocity.y;
should be
Code:
position.y += velocity.y * delta;

I believe it's also more common to integrate velocity first, then integrate position using the newly-solved velocity. You'll get more stable results that way. (http://en.wikipedia.org/wiki/Semi-implicit_Euler (http://en.wikipedia.org/wiki/Semi-implicit_Euler)).


Title: Re: Velocity, Acceleration, Gravity help
Post by: jotapeh on April 25, 2011, 09:29:15 AM
I'd recommend you get the basics of this down in a fixed timestep first, before you try to account for a variable timestep..


Title: Re: Velocity, Acceleration, Gravity help
Post by: J. Kyle Pittman on April 25, 2011, 10:37:02 AM
I'd recommend you get the basics of this down in a fixed timestep first, before you try to account for a variable timestep..

I think it's important to take the delta time into consideration regardless of whether the frame time is variable or fixed. Even for a fixed 60Hz rate, I would write the Euler integration as:

Code:
const float FIXED_FRAME_TIME = 1.0f / 60.0f;

acceleration = (whatever)

velocity += acceleration * FIXED_FRAME_TIME;

position += velocity * FIXED_FRAME_TIME;

This makes it easier to convert to a variable timestep in the future, but more importantly, it make the math less ambiguous. Even with a fixed timestep, you're still integrating over time, and if you just discard the multiplication by delta time and adjust your constants to compensate, you lose some clarity about what the math is actually doing.


Title: Re: Velocity, Acceleration, Gravity help
Post by: TheLastBanana on April 25, 2011, 01:42:45 PM
I think his point is that it would be easier to learn how to work with velocity and acceleration in games without considering delta time. It's a concept that can get a bit complicated, and it's important to get the basics down first.


Title: Re: Velocity, Acceleration, Gravity help
Post by: danlthemanl on April 25, 2011, 04:08:02 PM
Quote
it's important to get the basics down first.
Can someone point me in the direction to learn the basics?