Author |
Message |
ZeroPaladn
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Andy
|
Posted: Wed Mar 22, 2006 2:12 pm Post subject: (No 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. |
|
|
|
|
![](images/spacer.gif) |
ZeroPaladn
|
Posted: Wed Mar 22, 2006 2:24 pm Post subject: (No 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? |
|
|
|
|
![](images/spacer.gif) |
do_pete
![](http://i38.photobucket.com/albums/e112/do_pete/1943.gif)
|
Posted: Wed Mar 22, 2006 2:33 pm Post subject: (No 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
|
|
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Wed Mar 22, 2006 8:21 pm Post subject: (No 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.
- 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.
- 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. |
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Wed Mar 22, 2006 8:28 pm Post subject: (No 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 |
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Wed Mar 22, 2006 9:16 pm Post subject: (No 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. |
|
|
|
|
![](images/spacer.gif) |
person
|
Posted: Wed Mar 22, 2006 9:57 pm Post subject: (No 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Andy
|
Posted: Thu Mar 23, 2006 1:47 am Post subject: (No 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? |
|
|
|
|
![](images/spacer.gif) |
codemage
![](http://usera.imagecave.com/codemage/codemage-small.gif)
|
Posted: Thu Mar 23, 2006 9:03 am Post subject: (No 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). |
|
|
|
|
![](images/spacer.gif) |
person
|
Posted: Thu Mar 23, 2006 7:47 pm Post subject: (No 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. |
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Thu Mar 23, 2006 8:21 pm Post subject: (No 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.. |
|
|
|
|
![](images/spacer.gif) |
person
|
Posted: Thu Mar 23, 2006 8:32 pm Post subject: (No 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 |
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Thu Mar 23, 2006 8:51 pm Post subject: (No 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 |
|
|
|
|
![](images/spacer.gif) |
person
|
Posted: Thu Mar 23, 2006 8:58 pm Post subject: (No 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 |
|
|
|
|
![](images/spacer.gif) |
|