Archived

This topic is now archived and is closed to further replies.

tHaPuTeR

Would this be considered Time Based Moving?

Recommended Posts

Ok, I wanting to move a sprite the same speed for any computer i run this program on... Is this what I need to do? if (keys[VK_RIGHT] && SpriteMoveTest() == true) { player.xpos++; } .... .... bool SpriteMoveTest() { Time = TimerGetTime(); if (Time - OldTime >= Speed) { OldTime = Time - (Time - OldTime - Speed); return true; } else return false; } If this is not correct, could someone please tell me how to do it the right way. Im using NeHe''s Timing code!

Share this post


Link to post
Share on other sites
well, i'm too tired to figure out if yours will work, but it's not the standard way. the standard way is to use the physics equation

p = p0 + v * dT

where:
p = position now
p0 = last frames position
v = velocity (speed)
dT = time since last frame

so you structure things like this:


    
//main game loop

while (1) {

//QueryPerformanceCounter is preferred b/c it gives more

//accurate times, but i haven't gotten around to changing

//my code yet

static float timeLast = (float)GetTickCount() / 1000;

float timeNow = (float)GetTickCount() / 1000;
float dT = timeNow - timeLast;
timeLast = timeNow;

update(dT);

}

//update your objects' positions for this frame

update(float dT) {
for all your objects {
p.x = p.x + v.x * dT;
p.y = p.y + v.y * dT;
p.z = p.z + v.z * dT;
}
}


you mentioned sprites, so you're probably doing 2D motions so ignore the p.z line of code.

i assume you are alredy braking your 2D game movement into x and y speeds, so just put those in place of the v.x and v.y

-me

[edited by - Palidine on July 28, 2002 4:24:08 AM]

Share this post


Link to post
Share on other sites
If your game loop is too blindingly fast, you can get zero for your elapsed time due to rounding errors. You really should use the performance counter.

Share this post


Link to post
Share on other sites
Well... this is what I ended up with!

I am still useing the timing code from NeHe''s site so..
while(1)
{
float time = TimerGetTime();
...
DrawEverything();
...
float dT = TimerGetTime() - time;

//Then I move everything with dT...
MoveSprite(dT);
}

///////////////////////////////////
MoveSprite(float dT)
{
player.x = player.x + player.xvel * dT;
}

is this correct??? it works fine for me!

Share this post


Link to post
Share on other sites
Well... this is what I ended up with!

I am still useing the timing code from NeHe''s site so..
while(1)
{
float time = TimerGetTime();
...
DrawEverything();
...
float dT = TimerGetTime() - time;

//Then I move everything with dT...
MoveSprite(dT);
}

///////////////////////////////////
MoveSprite(float dT)
{
player.x = player.x + player.xvel * dT;
}

is this correct??? it works fine for me!

Share this post


Link to post
Share on other sites
not entierly. you don''t measure the time for movesprite. while thats (currently) not much, it is a little flaw..

i''ll suggest you my tiny codesnipped:


  
DWORD measure_update(DWORD& _cur,DWORD _new) {
swap(_new,_cur); // swap in stl of c++ is templated

return _cur - _new;
}

int main() {
DWORD time = timeGetTime();
while(g_running) {
Update(measure_update(time,timeGetTime()));
Draw();
}
}


where Update is a function taking DWORD as timestep in milliseconds (and calls for example your MoveSprite function

and Draw is where you draw your sprites..

thats a quite handy way for variable timestep, means measuring each frametime and updating according to this.. (other way is fixing the timestep, most complicated one is fixing the timestep for Update() but not for Draw() but even that is quite easy if you found the way around it)

hope my code helps..

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
eighter

  
#include <algorithm>

or

  
template<typedef _Type>
void swap(_Type& a,_Type& b) {
_Type c = a;
a = b;
b = c;
}

thats basically the same, and works for all things, ints,floats,objects that have a copy constructor..

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
I still have a problem guys... The code I posted above works fine, or at least I think. I tried it on three different computers and all have the same speed. I dont want to sound like I know what Im talking about because I dont. Im just really confused. Like in specific terms... what would you say that is wrong with the code that I posted twice by accident. Will it get me by? Im just writing a simple side scrolling space shooter game. I hope not to offend anyone but Im really lost and just need someone to explain it to me in "dummies" terms!

thanx
tHaPuTeR

Share this post


Link to post
Share on other sites