Computer Science Canada

Jumping Speed Algorithm??

Author:  ZeroPaladn [ Wed Mar 22, 2006 1:59 pm ]
Post subject:  Jumping Speed Algorithm??

Im wondering how i can make my character jump when i hit the UP key, i got the concept but it doesnt look real enough, what it does is that it moves at a constant speed upwards, untill it hits a point in the jump where it then falls at a constant speed back to the ground. Could somebody help me out with trying to make it look more realistic?

Author:  Andy [ Wed Mar 22, 2006 2:12 pm ]
Post subject: 

this will require some physics. use kinematic formulas to find the distant traveled at a given time with a set inital velocity and gravity.

Author:  ZeroPaladn [ Wed Mar 22, 2006 2:24 pm ]
Post subject: 

So your saying that i should set the initial speed of the jump, then reduce the speed using a gravity constant? i was thinking about multipling the initial speed of the jump by a decimal (something like .6) so that the speed decreases by 40% every time the program goes though the loop. would that work?

Author:  do_pete [ Wed Mar 22, 2006 2:33 pm ]
Post subject: 

You could go something like this:
code:
const GRAVITY := -0.1
loop
    if touchingground then
        yspeed := 0
    else
        yspeed += GRAVITY
    end if
end loop

Author:  Cervantes [ Wed Mar 22, 2006 8:21 pm ]
Post subject: 

ZeroPaladn wrote:
So your saying that i should set the initial speed of the jump, then reduce the speed using a gravity constant? i was thinking about multipling the initial speed of the jump by a decimal (something like .6) so that the speed decreases by 40% every time the program goes though the loop. would that work?

No, for two reasons.

  1. That's not how gravity works. That's more along the lines of how air resistance works. Gravity always accelerates you at a constant speed. Unless of course you're falling a really far distance, so the force of gravity changes significantly as you fall. But if you're falling from that high, you'd probably want to be thinking about friends and family, not the physics of your fall.
  2. That wouldn't decrease your speed. You'd jump, then slow down, and continue slowing down as your speed approaches zero. You'd hover there.

do_pete's got the right idea.

Here's a past thread about Jumping. There's lots of code, and lots of explaination. It goes into collision detection while jumping, too.

Author:  Andy [ Wed Mar 22, 2006 8:28 pm ]
Post subject: 

i think that's too complicated... use d=(v1)t+(1/2)(a)(t)^2 sub in a given time and you know the height that the person is supposed to be at

Author:  Cervantes [ Wed Mar 22, 2006 9:16 pm ]
Post subject: 

To each his own.

However, if this is a platformer or something else that might require horizontal movement, you'll need a horizontal speed variable. If you've got that, it seems logical to use a vertical speed variable.

Also, using the kinematics formula would require storing two variables. The y value, and the y value while in the jump. The original y value can't be hardcoded because of jumping on platforms (again, assuming this is a platformer or something similar). So we're using two variables for the vertical anyways. No better than do_pete's approach.

Author:  person [ Wed Mar 22, 2006 9:57 pm ]
Post subject: 

Andy wrote:
i think that's too complicated... use d=(v1)t+(1/2)(a)(t)^2 sub in a given time and you know the height that the person is supposed to be at


You'd be better off using the kinetic and potential energy at the top and the bottom of the jump.

Author:  Andy [ Thu Mar 23, 2006 1:47 am ]
Post subject: 

person wrote:
You'd be better off using the kinetic and potential energy at the top and the bottom of the jump.


errrr what? how are you going to factor time in with potential energy?

Author:  codemage [ Thu Mar 23, 2006 9:03 am ]
Post subject: 

As alluded to, just have an initial jump strength vector, a current vertical motion vector, and a gravity constant.

Let's say initial jump strength is 10 units (pixels), gravity is 2.

On jump, you move up 10 units,
next frame, you move up 10-2 = 8
next frame you move up 8-2 = 6...

eventually, gravity pulls you back down,..
you go down 2 units
then 4... until you hit your original jump point.


If you want a more accurate representation, you have to use quadratics. (Jumps over time form a parabolic arch - this is a simple curve).

Author:  person [ Thu Mar 23, 2006 7:47 pm ]
Post subject: 

Andy wrote:
errrr what? how are you going to factor time in with potential energy?

First use the energies to find the actual height that it'll reach.
Then use kinematics to find the time.

Author:  Andy [ Thu Mar 23, 2006 8:21 pm ]
Post subject: 

OR... you can just use that kinematics formula i said.. have you ever tried to make an equation solver with code? its not fun..

Author:  person [ Thu Mar 23, 2006 8:32 pm ]
Post subject: 

Andy wrote:
have you ever tried to make an equation solver with code? its not fun..


I know, but you dont need to, u can just have h = (v^2)/(2 * g) for the height, and then do the same thing with time by using the quadratic formula.

Andy wrote:
OR... you can just use that kinematics formula i said

but that won't be as realistic

Author:  Andy [ Thu Mar 23, 2006 8:51 pm ]
Post subject: 

why would it not be realistic? you set a timer, and you sub in the given time to find the displacement you are away from your height, then simply draw the character there

Author:  person [ Thu Mar 23, 2006 8:58 pm ]
Post subject: 

say you sub in 2 seconds....if the person jumps one meter, then its realistic but wat if its supperman and he jumps 20meters??

2 seconds would then be like watching it in fast motion

Author:  Andy [ Thu Mar 23, 2006 11:24 pm ]
Post subject: 

then reduce gravity, or give it an large initial velocity

Author:  person [ Thu Mar 23, 2006 11:31 pm ]
Post subject: 

If it has a large initial velocity, then it would look like it was in fast /slow motion if time was a given constant.

Author:  Andy [ Thu Mar 23, 2006 11:47 pm ]
Post subject: 

okay you're not making any sense to me... if you want him to jump higher and slow too, then just reduce "gravity"

Author:  person [ Thu Mar 23, 2006 11:49 pm ]
Post subject: 

but wasnt the initial problem that the jumping didn't look normal (ie: terrestrial)? so if u decrease gravity, then it still wouldnt look normal

Author:  Andy [ Fri Mar 24, 2006 8:23 am ]
Post subject: 

i would beg to differ. His original problem was that he was simply jumping up at a constant velocity and stopping to mid air. Try it my way, it'll work.. provided you program it right =P

Author:  person [ Fri Mar 24, 2006 10:39 am ]
Post subject: 

alright, lets just agree that they both work Smile

Author:  Andy [ Fri Mar 24, 2006 12:49 pm ]
Post subject: 

okay, but mine is still easier Cool

Author:  Cervantes [ Fri Mar 24, 2006 6:20 pm ]
Post subject: 

Person - Andy's way will work fine. Superman can jump 20 meters, and it won't look like he's moving faster than he should.

The reason is that we aren't changing time. We aren't saying that this jump has to be complete in 2 seconds, no matter how great his initial vertical velocity. Time is easiest represented as the number of passes of your main loop - like FPS. If Superman can jump higher, you just set the initial velocity of the jump (v1) to some larger value, and he will jump appropriately. The jump will demonstrate the same acceleration as other jumps, and it should, for we're all on the same planet.

Author:  person [ Fri Mar 24, 2006 6:28 pm ]
Post subject: 

but Andy's idea was to plug in a constant for time wasnt it?

Author:  Cervantes [ Fri Mar 24, 2006 10:05 pm ]
Post subject: 

A constant for time? As in,
code:
const t := 5

I don't think that was his idea, because that would result in the character not moving vertically at all.


: