[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] frame independant movement



Christopher Night wrote:
But of course, it's extremely easy to do constant-acceleration motion as well:

x += vx * dt + 0.5 * g * dt ** 2
vx += g * dt

Yes, you can do this kind of thing if the differential equations
governing your physics have an exact closed-form solution. But
that's not always the case -- simulating an object orbiting a
planet is one example.

dt = max(clock.tick() * 0.001, 0.1)
for j in range(5):
    for sprite in sprites:
        sprite.think(dt/5.)

In other words, use a small enough time step that the difference
is not big enough to be a problem. But if you're doing that, you
might as well pick one sufficiently small time step and use a
variable number of them per frame, so that the physics is
always predictable whatever the frame rate.

Although... you probably *do* want the physics to slow down if
the frame rate gets too low, otherwise the game becomes unplayable
due to the player not getting enough feedback.

Anyway, it's fine to have a library to do this, but there are some simple things that have always worked for me.

Even in the simple cases, there is benefit in having a library
that you can rely on to do it all right, instead of having to
think about whether the assumptions behind the particular shortcuts
you're using are valid in the circumstances.

--
Greg