Jump to content
  • Advertisement
Sign in to follow this  
FlameTheLoner

[XNA] Simulating Jump (Gravity)

This topic is 2595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a Platformer game. I got 'Walking' to work but am having trouble getting my character to jump.

Here is how it is now :
9a4uuh.jpg

This is all I could come up with for jumping :


//Jump

for (int i = 0; position.Y == 288; i++)
{
if (keyboardState.IsKeyDown(Keys.Up))
position.Y -= 10;

if (i == 90)
{
position.Y += 10;
i = 0;
}

}



And obviously, its nor working. :(

Does anyone know how I can make a character jump while simulating gravity (ie. it decelerates towards the down after a while). I want to do this own my own without using any Physics Engine.

Thanks. :)

Share this post


Link to post
Share on other sites
Advertisement
Since gravity can be represented as a simple downward force you should try to implement it like that
When you want to make your character jump you can apply an upward force to it, with force being mass * acceleration (note that you should try to use vectors to represent force and acceleration)
if you give your character a certain mass you can calculate its acceleration, which you can then use to determine its velocity and position each frame

Also every frame you take this force applied to your character and you substract your gravity from it (G = -9,81N e[sub]y[/sub] or -10N e[sub]y[/sub] rounded) which will change its acceleration, velocity and position

When the total force on your character is 0 it will be at its maximum height, you can now just keep on substracting your gravity until your character gets to the ground level, completing the jump

EDIT:

To get a better understanding of these concepts you should look into some elementary classical mechanics (especially kinematics)

Share this post


Link to post
Share on other sites
It's mostly really wise not to try to devise some fake physics and rather implement everything as real as possible. Because it's simpler!

Now, to put it simple enough, give your character a "vertical velocity" property. When you hit the jump key, set it to a positive number (positive means going up), representing a "jumping impulse". In each frame, subtract a (gravitational) constant (times delta time) from your velocity and then add the vertical velocity (times the delta time) to the vertical position. That's because velocity = acceleration * time (v=a*t [m/s = m/s/s * s]) and position = velocity * time (x=v*t [m=m/s * s]), see? (Everyone will excuse the oversimplification). Now you have a simple "Euler integrator" and your character should jump nicely :D If the vertical position goes below your obstacle (ground), clamp it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!