[XNA] Simulating Jump (Gravity)

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

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 :

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

EDIT:

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

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 If the vertical position goes below your obstacle (ground), clamp it.

Share on other sites
I've done highschool physics, and I was pretty good at it.

But converting those concepts to code is getting to me. xD

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• Forum Statistics

• Total Topics
633767
• Total Posts
3013739
×