Sign in to follow this  

[XNA] Simulating Jump (Gravity)

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 :

This is all I could come up with for jumping :


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
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


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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this