Jump to content
• Advertisement

#### Archived

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

# Would this be considered Time Based Moving?

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

If you intended to correct an error in the post then please contact us.

## 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

##### Share on other sites
Advertisement
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 loopwhile (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 frameupdate(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]

Kewlness!

#### Share this 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

##### 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

##### 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

##### 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

##### Share on other sites
What header file do i use with swap??? I cant get it to work

#### Share this 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

##### 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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5
• Advertisement

• 12
• 16
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632659
• Total Posts
3007692
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!