Speed In Games

Hi, does anyone know of a way to control the speed at which an object
moved. I can do the x++ and the y++ but after a x and y = 3 it’s just way
to fast to play…

does anyone have any ideas to implement this?

thanks.

on Mon, 30 Aug 1999 Ryan Wahle wrote:

Hi, does anyone know of a way to control the speed at which an object
moved. I can do the x++ and the y++ but after a x and y = 3 it’s just way
to fast to play…

does anyone have any ideas to implement this?

thanks.

Hi,

what about the following:
<<<<<<<<<<<<<<<<<<<<<

float vx = 0.001; // the horizontal speed component in pixels / ms
float vy = 0.0005; // the horizontal speed component in pixels / ms

unsigned int x,y; // the position in pixels

unsigned int delta_t; // time since last position update in ms
//(… SDL_GetTicks())


x += (unsigned int)(vx * delta_t + 0.5);
y += (unsigned int)(vy * delta_t + 0.5);

ok, it uses floating point arithmetics, which might be replaced by fixed point
arithmetics.

the +0.5 converts float to integer more precisely. (as long as the values > 0)

regards,–
Karsten-O. Laux
klaux at student.uni-kl.de
http://www.rhrk.uni-kl.de/~klaux

I have never used the SDL_GetTicks… Could you explain that alittle more?

ThanksOn Mon, 30 Aug 1999, Karsten-Olaf Laux wrote:

on Mon, 30 Aug 1999 Ryan Wahle wrote:

Hi, does anyone know of a way to control the speed at which an object
moved. I can do the x++ and the y++ but after a x and y = 3 it’s just way
to fast to play…

does anyone have any ideas to implement this?

thanks.

Hi,

what about the following:
<<<<<<<<<<<<<<<<<<<<<

float vx = 0.001; // the horizontal speed component in pixels / ms
float vy = 0.0005; // the horizontal speed component in pixels / ms

unsigned int x,y; // the position in pixels

unsigned int delta_t; // time since last position update in ms
//(… SDL_GetTicks())


x += (unsigned int)(vx * delta_t + 0.5);
y += (unsigned int)(vy * delta_t + 0.5);

ok, it uses floating point arithmetics, which might be replaced by fixed point
arithmetics.

the +0.5 converts float to integer more precisely. (as long as the values > 0)

regards,


Karsten-O. Laux
klaux at student.uni-kl.de
http://www.rhrk.uni-kl.de/~klaux

on Mon, 30 Aug 1999 Ryan Wahle wrote:

I have never used the SDL_GetTicks… Could you explain that alittle more?

Thanks

SDL_GetTicks() returns the number of miliseconds which have passed since you
have started your application. Here is an excerpt from my mainloop (in fact it
is part of uapplication.cc from libu)
Limiting the gameloop to 40 revolutions per second does a great deal on linux
machines, since if your game is idle, it won’t use any cpu cycles,
since SDL_Delay() gives away cpu time - any notebook users will love you for
this :wink:

<<<<<<<<<<<<<<<<<<<
Uint32 lastticks;
Uint32 tick;
Uint32 delta_tick;

while(running_)
{
//Timer stuff
tick = SDL_GetTicks();
delta_tick = tick - lastticks;
lastticks = tick;

  //limit to 40 revolutions per second (40*25ms = 1 second)
  if(delta_tick < 25)
    {
      SDL_Delay(25 - delta_tick);
      tick = SDL_GetTicks();
      delta_tick = delta_tick + tick - lastticks;
      lastticks = tick;               
    }

 ///now delta_tick contains the number of ms since this line was last
 ///visited..      

 /// now do the game processing .................

} //while>>>>>>>>>>>>>>>>>>>>>>>>>>>


Karsten-O. Laux
klaux at student.uni-kl.de
http://www.rhrk.uni-kl.de/~klaux

Your code doesn’t work for very small velocitys (the object stays in
place) Store the current location as a float and convert to int when you
draw.

Stuart

on Mon, 30 Aug 1999 Stuart Anderson wrote:

Your code doesn’t work for very small velocitys (the object stays in
place) Store the current location as a float and convert to int when you
draw.

ah, good point. Thanks,–
Karsten-O. Laux
klaux at student.uni-kl.de
http://www.rhrk.uni-kl.de/~klaux

Won’t this slow down the whole game? What if I want one object to go very
fast and one to go very slow without slowing all the objects in the whole
game only go for 40 revolutions a second?

Also, I don’t understand how using floating point works because the
cords to blit a sprite to a screen at int. For example, you have xspeed =
.005. Wouldn’t that be an int 0… so if you had .007 would also be a int
0 also right?

Sorry I am asking all theses questions… I’m just trying to get a feel of
how and why a game processes the way it does.

ThanksOn Mon, 30 Aug 1999, Karsten-Olaf Laux wrote:

on Mon, 30 Aug 1999 Ryan Wahle wrote:

I have never used the SDL_GetTicks… Could you explain that alittle more?

Thanks

SDL_GetTicks() returns the number of miliseconds which have passed since you
have started your application. Here is an excerpt from my mainloop (in fact it
is part of uapplication.cc from libu)
Limiting the gameloop to 40 revolutions per second does a great deal on linux
machines, since if your game is idle, it won’t use any cpu cycles,
since SDL_Delay() gives away cpu time - any notebook users will love you for
this :wink:

<<<<<<<<<<<<<<<<<<<
Uint32 lastticks;
Uint32 tick;
Uint32 delta_tick;

while(running_)
{
//Timer stuff
tick = SDL_GetTicks();
delta_tick = tick - lastticks;
lastticks = tick;

  //limit to 40 revolutions per second (40*25ms = 1 second)
  if(delta_tick < 25)
    {
      SDL_Delay(25 - delta_tick);
      tick = SDL_GetTicks();
      delta_tick = delta_tick + tick - lastticks;
      lastticks = tick;               
    }

 ///now delta_tick contains the number of ms since this line was last
 ///visited..      

 /// now do the game processing .................

} //while


Karsten-O. Laux
klaux at student.uni-kl.de
http://www.rhrk.uni-kl.de/~klaux

on Mit, 01 Sep 1999 Ryan Wahle wrote:

Won’t this slow down the whole game? What if I want one object to go very
fast and one to go very slow without slowing all the objects in the whole
game only go for 40 revolutions a second?

The 40 updates per second has nothing to do with the speed of any moving
objects; it means that the screen is updated at a maximum rate of 40 times per
second. Every update rate above 30 gives us humans the impression of smooth
movement.
In uclient, which is a client for an online roleplaying game, I use
only 20 screen updates per second, which provides still very smooth movements
of the characters.

Also, I don’t understand how using floating point works because the
cords to blit a sprite to a screen at int. For example, you have xspeed =
.005. Wouldn’t that be an int 0… so if you had .007 would also be a int
0 also right?

As Stuart mentioned, you will also have to store the positions as floats, too.
Think of the floats as the coordinates in your gameworld. When drawing on the
screen you have to convert game-coordinates to pixel coordinates…

float fx, fy;
int x,y;

//simulation coordinates
fx = fx + vx * delta_t;
fy = fy + vy * delta_t;

//screen coordinates
x = (unsigned int)(fx + 0.5);
y = (unsigned int)(fy + 0.5);>Sorry I am asking all theses questions… I’m just trying to get a feel of

how and why a game processes the way it does.

Thanks


Karsten-O. Laux
klaux at student.uni-kl.de
http://www.rhrk.uni-kl.de/~klaux

The idea behind this is that it does not slow down the whole game. This
way you can have maximum control over the movement of your sprites so that
it is not dependent upon the speed of the cpu. Fixing your framerate like
this is a very good idea.

The reason why you use floating point even though it gets drawn to the
screen as an int is because it is easy. For example, I have a sprite that
moves to the right at a speed of .005 pixels / frame.

I would have something like this:

mySprite.speed = .005;
mySprite.Loc.h = 1.0; //gotta start somewhere
mySprite.Loc.v = 1.0;

while (!gameOver) {
mySprite.Loc.h += mySprite.speed;
DrawSprite(mySprite);
}

After 100 iterations of the loop (frames) mySprite.Loc.h would be .5. As
of yet the sprite has not moved, because the location was truncated to
still equal 1. After 200 iterations, however, mySprite.Loc.h would be 1
and the sprite’s new location would be 2. Our drawing routine would then
move the sprite 1 pixel to the right. See? I hope I was somewhat clear, I
am really kindof a beginner at this myself (or just a slow starter, I
guess).

Sean>Won’t this slow down the whole game? What if I want one object to go very

fast and one to go very slow without slowing all the objects in the whole
game only go for 40 revolutions a second?

Also, I don’t understand how using floating point works because the
cords to blit a sprite to a screen at int. For example, you have xspeed =
.005. Wouldn’t that be an int 0… so if you had .007 would also be a int
0 also right?

Sorry I am asking all theses questions… I’m just trying to get a feel of
how and why a game processes the way it does.

Thanks

On Mon, 30 Aug 1999, Karsten-Olaf Laux wrote:

on Mon, 30 Aug 1999 Ryan Wahle wrote:

I have never used the SDL_GetTicks… Could you explain that alittle more?

Thanks

SDL_GetTicks() returns the number of miliseconds which have passed since you
have started your application. Here is an excerpt from my mainloop (in
fact it
is part of uapplication.cc from libu)
Limiting the gameloop to 40 revolutions per second does a great deal on
linux
machines, since if your game is idle, it won’t use any cpu cycles,
since SDL_Delay() gives away cpu time - any notebook users will love you for
this :wink:

<<<<<<<<<<<<<<<<<<<
Uint32 lastticks;
Uint32 tick;
Uint32 delta_tick;

while(running_)
{
//Timer stuff
tick = SDL_GetTicks();
delta_tick = tick - lastticks;
lastticks = tick;

  //limit to 40 revolutions per second (40*25ms = 1 second)
  if(delta_tick < 25)
    {
      SDL_Delay(25 - delta_tick);
      tick = SDL_GetTicks();
      delta_tick = delta_tick + tick - lastticks;
      lastticks = tick;
    }

 ///now delta_tick contains the number of ms since this line was last
 ///visited..

 /// now do the game processing .................

} //while


Karsten-O. Laux
klaux at student.uni-kl.de
http://www.rhrk.uni-kl.de/~klaux

keeping the positions and the velocities of objects in the world as floats will
allow both very small and very large movements.

you convert the float representations to int when they are drawn, the rounding
at this level doesn’t matter. 0.5 may be zero, but soon enough it will be 1.001
… get the picture?

of course all of the objects will go very slowly if you use the same low valued
#define to set their velocities … you could always have a number of #defines,
maybe even one per object type :wink:

the last games i have written have all kept their objects moving at relatively
the correct speed no matter what power processor or complexity of renderer. this
is achieved by noting how long in milliseconds it has been since the last
update, and moving the objects on in relation to this. certain things become
more difficult for someone like me who isn’t hot in the maths department, like
accelleration functions, but otherwise it is less difficult to code this way
than it is to restrict yourself to integer speed increments and working with a
frame based time representation!

using a frame-relative time base will ensure that when the action hots up you
will experience catastrophic slowdown. you know the sort from the bad-old days…
not just a reduction in frames rendered per second, but also a visible slowing
down of the on-screen action. your users will love you! >:(

i am hoping to create a website soon where i will be posting a number of sdl
games and tools that i have written where techniques like these can be found in
use. (anyone out there offer any hints on web authoring?! :slight_smile:

Charlie Robson
Sony Computer Entertainment Europe

IMPORTANT NEWS ANNOUNCEMENT FROM SCEE MIS

New scee.net email address will replace playstation.sony.com**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
postmaster at scee.net

This footnote also confirms that this email message has been checked
for all known viruses.


SCEE 1999