Followers 0

# Basic Jumping and Gravity Implementation

## 5 posts in this topic

Hi guys,

I'm still really not sure about how to implement jumping and gravity:

float3 position(0,0,0;
float3 velocity(0,0,0);

// this sums the forces
float3 sumForces()
{
float gravity=-4.0f;
return float3(0,gravity,0);
}

// this gets called every frame
void update(float dt)
{
// mass = 1 so acceleration = forces
float3 acceleration=sumForces();

position += velocity*dt;
velocity += acceleration*dt;
}

// this gets called when controller input changed
void onInput()
{
if(button_pressed){
// add jump but to what ?
forces.y=10.0f;
}
}


What do I do when the button is pressed for a jump ? I think I should add a positive y-value to the forces but where ? It has to go away after a second or two.

And my velocity y-value will continually decrease because of gravity so I have to clamp that to a lower limit, but what should that be ? Shouldn't my guy go faster and faster the deeper he falls ?

Thanks.

0

##### Share on other sites

It probably depends on the effect you are trying to achieve. You may for example apply an impulsive force which directly change the current y velocity to some value. Or you may apply some vertical force for a limited time. What kind of game are you trying to make? Do you want the effect to be realistic? Do you want the game to be very reactive?

0

##### Share on other sites

Here is some more info: the game has a first person perspective and jumping is pretty important, it doesn't have to be realistic but should feel good. The player needs to be able to jump across distances that he wouldn't be able to get to without jumping. Pretty much what you see in your run of the mill shooter.

Thanks.

0

##### Share on other sites

To simulate a "realistic" jump from a first person perspective it isn't enough to add some propelling force. You have to simulate the entire jumping movement. You somehow have to simulate the "crunching" movement used to accumulate energy for the jump.

1

##### Share on other sites

I don't quite understand why you were bothered...

1) when user hit jump button, you can simply give velocity a positive y value, and everything else remains the same.

2) I think you don't need to clamp velocity.y, but need to check for position.y so if you hit ground then velocity.y should be set to zero.

Do I understand your question correctly or completely wrong?

1

##### Share on other sites

I don't quite understand why you were bothered...

1) when user hit jump button, you can simply give velocity a positive y value, and everything else remains the same.

2) I think you don't need to clamp velocity.y, but need to check for position.y so if you hit ground then velocity.y should be set to zero.

Do I understand your question correctly or completely wrong?

Yeah but that will give a cartoony "bump" effect, Mario platformer style. I don't think that is what OP is looking for if he is working on shooter.

1

## Create an account

Register a new account