Skip to main content
gamedev.net
🔒 Locked

Speed

Started by Grantyt3 May 14, 2006 at 3:03 PM 21 replies 1.9k views
Original Post
Grantyt3
Grantyt3
Could someone tell me how to make my game run at the same speed on all computers? I think it's done with timers right? I guess what usually happens, is it skips frames to keep it up to speed or something...
Fred304
Fred304
Instead of moving your character a number of pixels per frame, you make him move a number of pixels per second. You can calculate the amount of time that has passed since the last update of the character's position and then use this amount of time do calculate the new position.

x = x + v*dt

dt is the amount of time that has passed since the last frame, v is the velocity.
Grantyt3
Grantyt3
Oh, I see. How do I find the amount of time that's past?
Spoonbender
Spoonbender
You check the time when you start doing stuff, and then again when you're done. Then you subtract the two, and you're left with the difference, that is, how much time passed from the first timestamp to the second.
Grantyt3
Grantyt3
Well, I know that, but how do you check the time before and after. Isn't there something that's part of the standard library?
rip-off
rip-off
Quote:
Original post by Grantyt3
Well, I know that, but how do you check the time before and after. Isn't there something that's part of the standard library?


Maybe this?

If you are using an APi like SDL or whatever, they usually have easier to use functions.
Zakwayda
Zakwayda
SDL has a timer function (check the API or online docs for details). It's not terribly high precision, but it should be suitable for most purposes.
Ravuya
Ravuya
I use SDL's internal tick counter (SDL_GetTicks) to handle timing -- it's millisecond accurate which should be fine for most purposes.
Grantyt3
Grantyt3
Okay, I'm using that, but I have a little question.

It returns milliseconds. If vx is equal to 1 and frameTime is equal to 7 and I do this: x = vx * frameTime,

Then that's way too fast. I think the only way to resolve this is to either turn those milliseconds into a fraction of a second (0.007 instead of 7), or make my movement speeds into smaller fractions. Either way, it's going to have to convert the floats into integers when it gets sent to the display function. This doesn't seem very efficient to me, does anyone else know another way I could do this?
Zakwayda
Zakwayda
Quote:
Original post by Grantyt3
Okay, I'm using that, but I have a little question.

It returns milliseconds. If vx is equal to 1 and frameTime is equal to 7 and I do this: x = vx * frameTime,

Then that's way too fast. I think the only way to resolve this is to either turn those milliseconds into a fraction of a second (0.007 instead of 7), or make my movement speeds into smaller fractions. Either way, it's going to have to convert the floats into integers when it gets sent to the display function. This doesn't seem very efficient to me, does anyone else know another way I could do this?
I didn't read the whole thread so I don't know what kind of game you're making, but generally it's easier to perform game calculations (physics, collision detection, AI, movement, and so forth) in floating point. You may have to convert the integer milliseconds value returned by SDL to a floating-point 'seconds' value and/or float position values to integers for drawing via SDL, but a few such conversions are not going to hurt your performance.
Grantyt3
Grantyt3
The compiler has a "warning" for each time it will be converted. Should I be converting it beforehand, or should I just ignore that?
Grantyt3
Grantyt3
SDL is cross platform. Not standard, but cross platform.
Grantyt3
Grantyt3
Oh, nevermind, I got it. You can add (int) in front when sending the float as an argument.
Razor89
Razor89
If your using .NET, C# and VB.NET (well any .NET language anyhow) has a property
called Environment.Tickcount which is the number of milliseconds since the computer
started so in order to get the time you would probably do something like this:

Public Class CSomeGame{private float m_seconds;CSomeGame(){m_seconds = 0.0f;GameLoop;}void GameLoop(){int frameTimeBegin = Environment.TickCount;Input();Render();int frameTimeEnd = Environment.TickCount;// Since there are 1000 milliseconds in a second we// divide by 1000 to get the number of seconds passed.m_seconds = (frameTimeEnd - frameTimeBegin) / 1000.0f;}}
"You and I are friends; when you cry, I cry. When you laugh, I laugh. When you hurt, I hurt. When you jump off a bridge....... I am sure going to miss you!"
LowRad
LowRad
Look, it's easy... You just need to convert thing to the right scale/magnitude/whatever...

Let say you're making a car game. The player's car is moving foward at 120km/h (or whatever the system you use), and for simplicity let say a frame took 10ms to render.

So the question now, how much do you have to translate the car foward for it to look like he's going 120km/h

Car speed: 120km/h
Frame time: 10ms

Let's convert the car speed into m/ms (meter per Ms)
120Km/h / 60 / 60 / 1000 (60 min in a hour, 60 second per minute, 1000ms per second)

This is: 3.33333e-5 Km/ms
Now, 3.333e-5 * 1000 * 1000 (there's 1000meters in a KM, 1000CM in a meters)

So final answer:33.333cm/ms

So, its not a matter of converting to float or in or ms or .... Its simply a matter of doing the right thing ;)

Best luck,
Jonathan
Razor89
Razor89
Thats pretty cool Johnathan if you want to be able to define what speed you
want the player to move using a standard system that John Doe uses (like inches, milimeters, feet, yards, etc..)
however the given methods above define how fast you want the player to move in
pixels or units for 3D applications.

I'll be sure to save that though I might find some use for it sooner or later.
"You and I are friends; when you cry, I cry. When you laugh, I laugh. When you hurt, I hurt. When you jump off a bridge....... I am sure going to miss you!"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.