Skip to main content
gamedev.net
🔒 Locked

[XNA] Simulating Jump (Gravity)

Started by FlameTheLoner Aug 15, 2011 at 10:55 AM 2 replies 2.2k views
Original Post
FlameTheLoner
FlameTheLoner
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.
Radikalizm
Radikalizm
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)
I gets all your texture budgets!
pcmaster
pcmaster
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.
FlameTheLoner
FlameTheLoner
I've done highschool physics, and I was pretty good at it.

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.