Advertisement

Let the player jump

Started by February 17, 2003 03:03 PM
4 comments, last by ClassifiedOne 22 years ago
I want to make my 2d player jump. The height of its jump should depend on a gravity value and a speed constant. Without considering the frametime, the code would look something like this: pl.y = pl.y + pl.jumpspeed pl.jumpspeed = pl.jumpspeed - gravity But I need to involve the frametime, because the player should jump on other computers as fast as on my computer. So I need something like this: pl.y = pl.y + pl.jumpspeed * frametime pl.jumpspeed = pl.jumpspeed - gravity * frametime But it doesn''t work, because the proportion of the jumpspeed to the gravity varies. For example (jumpspeed=10; gravity 20): frametime = 12: y = 10*12 = 120 jumpspeed = 20*12 = 240 difference = 120 frametime = 25: y = 10*25 = 250 jumpspeed = 20*25 = 500 difference = 250 This causes the jump-height to change with the frametime. How can I make my player jump the same speed and same height but with different frametimes? Thanks ClassifiedOne
You need to interpolate from the last frame to the current frame.

There are a lot of ways you can do this, but the simplest and probably most accurate is:

For x = 0 to TimeSlicesSinceLastFrame
Update Y and JumpSpeed
Next X

You would then need to scale your jumpspeed according to the minimum time slice you want to work on.

Another way would be to define the jump as a sin-like wave function. Then you could simply query the function with a given elapsed time to get the height. This wouldn''t help with object collision, but if you are not worried, this should work.

I''m basically using the first method at the moment. In the loop I do collision checking for each timeslice so that the character can not pass through anything else because of bad frame rate.

Learning to fly is easy, but as a tortoise, the landings are really rough.
Always prey on the weak, the timid and the stupid. Otherwise you'll just get your butt kicked
For a tortoise, this is extremely hard to do, but when you get it right... the expression on their faces ...
Advertisement
Have you actually tried viewing this at different framerates? You have to remember that your jumpspeed should change at a different rate at different framerates (it may have to catch up so to speak)
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
quote:
Original post by Thunder_Hawk
You have to remember that your jumpspeed should change at a different rate at different framerates (it may have to catch up so to speak)


Yes of course, but the jump height shouldn''t change...

quote:
Original post by SoaringTortoise
You need to interpolate from the last frame to the current frame.

How do I interpolate? What is a timeslice? How do I implement the updates?
I just googled and found the equation I was looking for:

I assume frametime == delta time
pl.y = pl.y + frametime * pl.jumpspeed + frametime * frametime * a / 2;
pl.jumpspeed = pl.jumpspeed + frametime * a;

where a is equal to the signed gravity (i.e. -9.8m/s2)

[EDIT] The equation you were using assumes that frametime * frametime * a / 2 is insignificant, which is very often the case. However, I'm still not sure what you felt was wrong with your original setup...

[edited by - Thunder_Hawk on February 17, 2003 6:21:01 PM]
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Hey, thank you very much!

quote:
Original post by Thunder_Hawk
The equation you were using assumes that frametime * frametime * a / 2 is insignificant


That is the point. In my setup small changes of the frametime affect the height of the player. With your equation this difference is indeed insignificant (unless you have a frametime about 100).

This topic is closed to new replies.

Advertisement