Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

perrs

bouncing

This topic is 5682 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a small game where the player bounces up and down on some springs. I've done so that the movment is dependent of the framerate meaning that the player moves at the speed no matter if it's a fast or a slow computer. My problem is that the player doesn't bounce the same height all the time. Apparently, he bounces higher if the computer has a lag. Here is a pseodo version of the code that updates the players position: // Called every frame. // time = seconds since the game was startet. void advance(double time) { double deltetime = time - oldTime; oldTime = time; velocity += gravety*(float)deltetime; pos += velocity*(float)deltetime; setPos(&pos); } void bounce() { velocity += bounceheight; // bounceheight = (0, 1, 0) } Can anybody tell me what I'm doing wrong? Thanks, Per Rasmussen. [edited by - perrs on January 21, 2003 4:29:26 PM]

Share this post


Link to post
Share on other sites
Advertisement
Part of your problem is that you''re running into numerical error, actually something called "truncation error". Since deltatime is not always repeatable, e.g., it is different on different computers running at different speeds, then your velocity and position are not updated in a perfectly repeatable way. This is a side effect of every numerical integration method. (You''re using a technique that is similar to, but not exactly, the simple Euler method.)

As long as deltatime is small enough, then you should be able to achieve close to the same height. But if deltatime gets too large, your character may just step past the time of maximum height, e.g., gravity will have acted on the character for too long and you won''t draw a frame at the time when the maximum jump height is reached.

One way to eliminate the truncation error would be to use a closed form equation for a projectile to represent the vertical portion of your motion. The closed form equation, which shows up in every high school physics textbook, doesn''t exhibit truncation error, but it does have roundoff error (less of a problem here). You still would have the problem of the character never being displayed at the time of the maximum jump height, e.g., you might have to draw a frame 1/10 of a second before the peak of the jump and 1/10 of a second after the peak of the jump. But the character would never appear to reach the peak of the jump. One reason its necessary to try and achieve consistent or at least sufficient frame rates on different computers.

Hope that helps a little bit. I know its an awkward description. Been a long day.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites
Ok, I think I know what you mean, but can''t this problem somehow be solved on a "per frame" basis? I''m thinking, if I didn''t use the time between each frame as "controller" but rather the time since the game was started. If I understand you correctly, this would minimize the truncation error.

BTW, if the error was small enough it would probably be acceptable, but in the tests I made there was a pretty big difference between the high and the low jumps.

Share this post


Link to post
Share on other sites
your problem is you calculate a linear movement when you calculate your new position:

velocity += gravety*(float)deltetime;
pos += velocity*time;

this isn''t accurate.

asuming a starting velocity of 0, the position is:
pos = (1/2)*accelleration*time*time

so try:

pos += velocity*time + (1/2)*gravety*deltetime*deltetime;
velocity += gravety*(float)deltetime;

i hope this will give you a more accurate position.
(haven''t checked the formula cause i am at work).


Share this post


Link to post
Share on other sites
Thanks OmniBrain!

That works much better. Here is the height of the first 8 bounces:

12.440435
12.447472
12.457978
12.450443
12.455718
12.455263
12.430817
12.431356

That should be an acceptable difference. From what I can guess, some of the difference originates from how far the player goes beneath the bouncing point.
I do not have any clue why this works, however. Can you direct me the some websites where I can learn more about physics applied to games?

BTW, in the line:

pos += velocity*time + (1/2)*gravety*deltetime*deltetime;

I assumed the "time" should have been "deltatime".

Share this post


Link to post
Share on other sites
ops, yes, sure it had to be deltatime.

well, i didn''t took this from any web page, it was applied school physics

I knew for an object starting with speed = 0 at position 0 and acceleration a, it''s position at time t will be
pos(t) = 1/2*at²

and for an object moving with a speed of v (starting at 0) it''s position at time t will be:
pos(t) = v*t

now you have to split the movement in a timeslice of a frame in two parts. the constant movement (v*t) and the accelerated part (1/2*at²).

you can simply add them up together to get your new position.

after you know the new position, you calculate the velocity at the end of this timeslice and you are done.

probably there are differences in your bounces because you do not sample the heigh in infinite small time steps. (you sample the position in a moment you haven''t reached the maximum, next sample is already when the ball is falling again)
if you run on a faster PC, the maximum of your bounce probably will get more accurate and theoretically bounce will finish at the same height.


Share this post


Link to post
Share on other sites
FYI,

OmniBrain''s solution is the closed form solution I mentioned, while perrs'' initial approach was a numerical integration. Thanks a bunch for the post, OmniBrain, .

I''m glad to see this was a big improvement for perrs!

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites
hmmmm, I can''t recall learning anything about that in school but then again, I can''t recall much of anything from highschool : )

Anyway, can you direct me to any good websites on the topic? I''d prefer tutorials which is made with game/computer physics in mind if you know any.

Share this post


Link to post
Share on other sites
Don''t know of any game physics tutorials, except perhaps the various physics articles from Game Developer Magazine, available on:

www.gamasutra.com

(Free registration)

Look for articles by Chris Hecker and Jeff Lander in particular.

Chris has PDF versions of a series of physics articles from a few years ago on his website:

www.d6.com/users/checker/dynamics.htm

You may also find some good material in the online archives from the Game Developer''s Conference:

www.gdconf.com/archives/

There are, of course, many highly advanced articles out there, which aren''t tutorials at all, e.g.:

www.techsem.com/gts2001/vault.htm

www.ri.cmu.edu/people/baraff_david.html#publications

www.ri.cmu.edu/people/witkin_andrew.html#publications



Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites
Thanks alot grhodes!

Those look like some great links.

Thanks again,
Per Rasmussen AKA Perrs.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!