Skip to main content
gamedev.net
🔒 Locked

Jump Physics

Started by programering 9 replies 1.6k views
Original Post
programering
programering
Hi there! I am making an game engine to a super mario bros 2 like game with square tiled world, what's that kind of game called? Anyway, for the player to jump I just wonder how it really works with the physics math. There's jump motion power/force to weight against its gravity. How shall I do?
programering
programering
if the player is in the air, how does it work with falling.
if i specify the player's weight, how much will he fall?
how does it works with gravity?
the downfall delta?
VThornheart
VThornheart
The physics of falling is not affected by the weight of an object, so no worries about that =)

Basically, to calculate any object falling it is the same as calculating that of a projectile falling to earth:

X(1) + X(2) = v*t + (1/2)g * t ^ 2

Where x(1) = the original position of the object,
x(2) is its final position
v = the initial velocity of the object
g = the gravitational constant
t = the time of the fall.

Actally, those x's should be y's but that's okay.

The initial velocity will be whatever the y velocity is (that is, the speed of his jump, so whatever you want).

g = 9.8 meters/second^2, but you can interpret that any way you want since the screen is obviously in pixels and not meters.

Anyways, calculate that for every frame, using t as the time between the last frame and the current one, and you're set!
-Vendal Thornheart=) Programming for a better tomorrow... well,for a better simulated tomorrow. ;)
xorjesus
xorjesus
You gave him a way to calculate the distance he will fall based on his initial velcocity and the time he has fallen. I don't know if you were joking or not. Anyway 'Programming', allowing players or entities is jump is pretty hard without fundamental knowledge in physics. You need to know the difference between velocity, and acceleration, and have a basic idea of what momentum and energy is. Just google for some basic tutorials on physics, and within a few days of study you'll definitely be able to get your player to jump. GL.
I study day and night, memorizing the game.
VThornheart
VThornheart
Joking? nay... that was what he was asking for, right? Or am I mistaken? I could've read it wrong, but I read it again and it still seems like that's what he wants.

The 1/2vt^2 takes care of the falling, and the vt takes care of the opposing nature (his rising). Over time, the former overcomes the latter and he begins to fall instead of rise: thus is the nature of a jump, unless I'm mistaken.

I mean, you have to manipulate the function to have it solve for the variable that you want it to, but it's there for the usin' =)

EDIT: Oh, and for a simple, Mario-style game, you won't need to worry much about momentum and energy... especially energy. You're going to need to know your equations based on given time, and energy equations won't help you much with that. Stick to classic kinematics... in fact, you can do a google search for "kinematics" and it'll give you most of the basic stuff you'll need.
-Vendal Thornheart=) Programming for a better tomorrow... well,for a better simulated tomorrow. ;)
VThornheart
VThornheart
OH, I see now... upon reading it again... did you want to use force equations to determine how high he'll jump Programming? In that case, you would need the objects' weight... I just assumed you might've been mistaken on the nature of gravity. =) However, you could probably save yourself some computational time by leaving it simple and not doing a force calculation each frame. Have your jumper, upon jumping, recieve a positive "Y" velocity and then use the equation I gave. It'll save a headache... and unless it was much more complicated than I give it credit for, I assume it's how Mario did it.

EDIT: Oh, the link that the other fellow gives above provides an interesting insight! I'd never thought of doing it, but it's a bit hidden in there so I'll post it here:

//if user presses jump key (shift), make dot "jump" (via "displacement", NOT acceleration)
//
// NOTE: two ways to 'do' jumping:
// 1) mario-style:
// -when jump is pressed, displace ninja.y upward (-y) for a single frame, AND disable gravity
// -if the player releases jump OR the 'jump-time-limit" runs out, re-enable gravity
// 2) smash bros. style:
// -when jump is pressed:
// - IF (numJumps < maxNumJumps):
// - displace ninja.y upward (-y) for a single frame, AND increment "numJumps"
// -in order for this to work, we need to set numJumps = 0 whenever the ninja collides with
// the ground (we also apply friction if the player is colliding w/ the ground).
// -note that it would be cool to be able to change the value of maxNumJumps (i.e you start with one,
// and gain more via items/levelUp/etc.

That's freakin' brilliant! In truth, it's a lot better than my idea. Go for that.
-Vendal Thornheart=) Programming for a better tomorrow... well,for a better simulated tomorrow. ;)
CombatWombat
CombatWombat
What you could do is,
Store your characters X and Y location. Also give it variables for velocity and acceleration ( in X and Y directions, consider using vectors).

You could then use simple Euler integration.
1) Set netForce to zero.
2) Add up all the forces on the object. (gravity, jumping impulse, collisions with the floor/walls/etc)
so,
netForce += force1, force2, etc etc (gravity would just be a force equal to the weight of the object (mass*gravity) in the negative-y direction.)

acceleration = netForce / mass;
velocity += acceleration *dt; // dt is time between frames
position += velocity *dt;

By having the land (and other objects) apply a force when you collide with them (look up collision response for different ways to do this) the simple physics above will take into acount bumping into walls, hitting your head on things, etc. Everything falls into place.

You could then adjust the worlds gravitational strength, the mass of your character, and the jumping force to modify the behavior until it acts as you want it to. If you wanted to get really fancy you could make the sideways force (running) change depending upon the friction of the ground you are standing on. Or you could do the SuperMario3 "duck slide" manuever by setting the friction of the ground low when you press duck.

Just scream real loud if you need clarification on my ramblings.

VThornheart
VThornheart
Whoa, fascinating! I'm saving the link to this thread... there have been a lot of interesting ideas that I honestly never thought of before. That's an excellent idea Wombat! I never thought of doing it that way before... and you're right, it would all fit quite intuitively!

Someone mod that man up =)
-Vendal Thornheart=) Programming for a better tomorrow... well,for a better simulated tomorrow. ;)
higherspeed
higherspeed
In answer to your other question, this type of game is usually called a platform game. Sonic and Knuckles 3 being the best ever, and no that isn't an opinion, it's a fact! ;)
NafdahliX
NafdahliX
Quote:
Original post by higherspeed
In answer to your other question, this type of game is usually called a platform game. Sonic and Knuckles 3 being the best ever, and no that isn't an opinion, it's a fact! ;)

You havn't played Klonoa: Door to Phantomile then?

Topic Locked

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

Sign in to reply to this topic.