Question about moving sprites

Hi to all.

I have a very simple questions. I have a game with a player that is
controlled with the keyboard. I want the sprite to move to the same
speed on any hardware, not depending of frames displayed or other
things. Mostly because the game may have network support, and so
everything needs to go to the same speed.

Would it be a good idea to have a function ran every 20ms (for example)
that moves the sprite according to what it needs to (the event are saved
before, like need to be moved UP, DOWN, LEFT, RIGHT, etc), so we are
sure it does move at the same speed everywhere ?

Any hint, or better, url about this concern is appreciated :slight_smile:

Hi to all.

I have a very simple questions. I have a game with a player that is
controlled with the keyboard. I want the sprite to move to the same
speed on any hardware, not depending of frames displayed or other
things. Mostly because the game may have network support, and so
everything needs to go to the same speed.

Oooh, this one’s not very simple at all, actually… Well, the
theory is actually pretty simple, but it’s not quite that easy in
real life, at least not if you also want optimal animation quality
and accurate (maybe even 100% repeatable) game logic. You will want
both for any serious gaming.

Would it be a good idea to have a function ran every 20ms (for
example) that moves the sprite according to what it needs to (the
event are saved before, like need to be moved UP, DOWN, LEFT,
RIGHT, etc), so we are sure it does move at the same speed
everywhere ?

Basically, yes - but you can’t actually implement it that way, unless
you’re fine with capping the animation frame rate and/or messing with
thread safe interaction between the game logic and the graphics
engine.

Any hint, or better, url about this concern is appreciated :slight_smile:

I recently hacked a playable example, demonstrating (among other
things) my favourite approach: Fixed Logic Frame Rate +
Interpolation. This design lets you get away with a plain, fixed
logic frame rate (as it was done in the days of PAL/NTSC based
systems) and a single thread for the whole game, and still get the
maximum possible rendering/animation frame rate on any hardware.

http://olofson.net/examples.html	(pig-1.0)

The same approach is also used in the game Kobo Deluxe, which was
originally (XKobo) a fixed frame rate game:

http://www.olofson.net/kobodl/

I suggest you look at the Pig first, though. Kobo Deluxe is layers and
layers of (mostly) C++ code, and it was hacked to be played - not
analyzed. Don’t mess with it unless you’re interested in the game
itself. :slight_smile:

Meanwhile, Fixed Rate Pig was hacked specifically to be a minimal but
complete example of some techniques that people ask a lot about
around here. (Fixed logic frame rate, motion interpolation, tiled
backgrounds, sprite animation, smart updating with pageflipping,
basic platform game logic etc.) It even comes with a document
describing what most of that cryptic code is all about. :wink:

//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Wednesday 24 March 2004 18.16, Fabien Penso wrote:

The simplest method:

Uint32 lastT = SDL_GetTicks();
Uint32 dt = 42;
while (…)
{
Uint32 t = SDL_GetTicks();
while ((t - lastT) >= dt)
{
yourPhysicsHere();
lastT += dt;
}
}

I’m using it on a game which needs accurate physics. It runs at 1600 FPS
(with the ugliest possible display of course), running Lua functions in the
physics loop etc. It works very well and the display doesn’t look “late”.
IIRC i use 40ms for dt.

At 18:16 24/03/2004 +0100, you wrote:>Hi to all.

I have a very simple questions. I have a game with a player that is
controlled with the keyboard. I want the sprite to move to the same
speed on any hardware, not depending of frames displayed or other
things. Mostly because the game may have network support, and so
everything needs to go to the same speed.

Would it be a good idea to have a function ran every 20ms (for example)
that moves the sprite according to what it needs to (the event are saved
before, like need to be moved UP, DOWN, LEFT, RIGHT, etc), so we are
sure it does move at the same speed everywhere ?

Any hint, or better, url about this concern is appreciated :slight_smile:


Hibernatus

I wrote:

IIRC i use 40ms for dt.

Sorry, it’s 4 ms actually :slight_smile: Had to calculate it since my code is a little
more complicated.
In case it wasn’t clear, “while (…)” is your game loop, where you get
events, display everything, etc.–
Hibernatus

Wed, 24 Mar 2004 18:52:52 +0100, tu as dit :

Hi to all.

I have a very simple questions. I have a game with a player that is
controlled with the keyboard. I want the sprite to move to the same
speed on any hardware, not depending of frames displayed or other
things. Mostly because the game may have network support, and so
everything needs to go to the same speed.

Oooh, this one’s not very simple at all, actually… Well, the
theory is actually pretty simple, but it’s not quite that easy in
real life, at least not if you also want optimal animation quality
and accurate (maybe even 100% repeatable) game logic. You will want
both for any serious gaming.

Few weeks later … :slight_smile:

Would it be a good idea to have a function ran every 20ms (for
example) that moves the sprite according to what it needs to (the
event are saved before, like need to be moved UP, DOWN, LEFT,
RIGHT, etc), so we are sure it does move at the same speed
everywhere ?

Basically, yes - but you can’t actually implement it that way, unless
you’re fine with capping the animation frame rate and/or messing with
thread safe interaction between the game logic and the graphics
engine.

Any hint, or better, url about this concern is appreciated :slight_smile:

I recently hacked a playable example, demonstrating (among other
things) my favourite approach: Fixed Logic Frame Rate +
Interpolation. This design lets you get away with a plain, fixed
logic frame rate (as it was done in the days of PAL/NTSC based
systems) and a single thread for the whole game, and still get the
maximum possible rendering/animation frame rate on any hardware.

http://olofson.net/examples.html (pig-1.0)

The same approach is also used in the game Kobo Deluxe, which was
originally (XKobo) a fixed frame rate game:

http://www.olofson.net/kobodl/

I suggest you look at the Pig first, though. Kobo Deluxe is layers and
layers of (mostly) C++ code, and it was hacked to be played - not
analyzed. Don’t mess with it unless you’re interested in the game
itself. :slight_smile:

Hmm i’ve quickly looked at pig, but it seems to be pretty long code just
to know how to do that. I guess it’ll take longer that I thought to find
a solution for my simple (sic) problem.

Thanks.> On Wednesday 24 March 2004 18.16, Fabien Penso wrote:

I’ve written a library in SDL for such a thing, based on animated sprites:
http://burningsmell.dyndns.org/crossfire/spritelib.htmlOn Tuesday 30 March 2004 14:41, Fabien Penso wrote:

Hmm i’ve quickly looked at pig, but it seems to be pretty long code just
to know how to do that. I guess it’ll take longer that I thought to find
a solution for my simple (sic) problem.

Thanks.