• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Basic Jumping and Gravity Implementation

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

Posted 12 April 2013 - 08:04 PM

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.

Edited by Endemoniada, 12 April 2013 - 11:02 PM.

### #2apatriarca  Members

Posted 13 April 2013 - 06:04 AM

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?

Posted 13 April 2013 - 08:52 AM

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.

### #4apatriarca  Members

Posted 17 April 2013 - 08:53 AM

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.

### #5HypnotiC  Members

Posted 17 April 2013 - 09:47 AM

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?

### #6Bacterius  Members

Posted 23 April 2013 - 07:26 AM

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.