Public Group

# the movement of sprite

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

## Recommended Posts

I don't know how to draw a sprite that can move the same speed when the game run by other computer.i don't know how calculate the time between two frame to measure the movement of sprite .help me.please

##### Share on other sites
You could set up a timer, and move using delta time.

Here is how I move my camera in my 3D game:

Camera.Move(GetDeltaTime() * 0.1f);

##### Share on other sites
Quote:
 Original post by KaLickI don't know how to draw a sprite that can move the same speed when the game run by other computer.i don't know how calculate the time between two frame to measure the movement of sprite .help me.please

You can decide that each frame shall take a constant time step, based on how many frames per second (FPS) you want your sprite movement to be updated in. For example 30 times per second:

Timestep = 1.0 / 30.0

Then you can have a variable, TimeSinceLastMovement, that stores how much time that has elapsed since the last movement update, and do not update the sprite movement until that time has reached your constant Timestep.

If it has reached higher than the constant Timestep, update the movement once and subtract Timestep from TimeSinceLastMovement, repeat until TimeSinceLastMovement is less than Timestep.

So if you compute Timestep like above and wait with the sprite movement until TimeSinceLastMovement reaches Timestep, the sprite movement will only be updated 30 times per second, and will therefore be the same on different computers (since the time of a second is the same on different computers).

If you're programming for the Windows operating system, you can get/query how much time has elapsed between frames by using the QueryPerformanceCounter Function, and the QueryPerformanceFrequency Function.

Which operating system are you programming for?

##### Share on other sites
Figure out how much time has elapsed since the last game loop update, and multiply that with all speeds that affect motion and animation.

If you use floating point numbers, you'll have an easy time of it. If you use real numbers, such as int, short, long, etc, you'll have to do some tweaking to get something working well.

##### Share on other sites
Thanks everybody.
I'm using XP and DXSDK2007.
I write like that:i don't know the compution of sp_rate is right?
QueryPerformanceFrequency(&timerFreq);
while (!doing)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
if (msg.message==WM_QUIT)
doing=1;

TranslateMessage(&msg);
DispatchMessage(&msg);
}

QueryPerformanceCounter(&timeStart);

Game_Run(hWnd);
QueryPerformanceCounter(&timeEnd);
}
and in Gam_Run funtion,I move the sprite following sprite.move+=number*sp_rate
Is it right?

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001781
×