Sign in to follow this  
KaLick

the movement of sprite

Recommended Posts

KaLick    122
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 this post


Link to post
Share on other sites
deadstar    536
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 this post


Link to post
Share on other sites
Dim_Yimma_H    557
Quote:
Original post by KaLick
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

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 this post


Link to post
Share on other sites
Kest    547
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 this post


Link to post
Share on other sites
KaLick    122
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);
sp_rate=( (float)timeEnd.QuadPart - (float)timeStart.QuadPart )/ timerFreq.QuadPart;
}
and in Gam_Run funtion,I move the sprite following sprite.move+=number*sp_rate
Is it right?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this