• ### Announcements

#### Archived

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

# Getting Jerkyness with TimeDelta

## Recommended Posts

i orignally had my 2d game set up so that the velocities were constant, which could cause the game to go at different rates dependent on the frame rate they got (i didn''t know this was a problem at the time i was writing all of my movement functions etc). so now i implemented something like this: Object.x += Object.Velocity * (TimeDelta/8); when doing something like this... i got jerkiness (note when i did not divide by 8... it went extremely fast) i tested timedelta on my machine... and it was 7 or 8 most of the time (using timGetTime()). do you know why im getting jerkiness, im usuing integers, is this a problem, should i use float? ---------------------- i code therefore i am. Aero DX - Coming to a bored Monitor near you!

##### Share on other sites
you should use floats all the way through the system and only truncate them to Int''s right before you use them.

So Object.x should be a float as well as Object.Velocity

##### Share on other sites
Yes, if timedelta and velocity are integers, try casting them to floating point before doing the math

##### Share on other sites
i made the x,y,xspeed, and yspeed values all floats, and casted top unsigned long for the bltflast method, and im still getting jerkiness, whats the deal?

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!

##### Share on other sites
Read the posts...what they're saying is this: if TimeDelta is an integer, then when your computer does:

TimeDelta / 8, you get an integer return. That is, if timeDelta = 7, then using integer math, 7 / 8 = 0. Aka it thinks no time has passed. When it turns to 8, 8 / 8 = 1.

Object.x += Object.Velocity * (TimeDelta/8);

Try:
Object.x += Object.Velocity * ((float)TimeDelta/8);

or

Object.x += (Object.Velocity * TimeDelta) / 8;

edit: removed junk about other integers which neen said were allready floats and I didnt see...hurry up and post stupid gamedev!

[edited by - MaulingMonkey on March 3, 2003 8:44:57 PM]

##### Share on other sites
Instead of dividint by 8, try this:

Object.x += Object.Velocity * (TimeDelta*0.02f);

tweak that number until you get the results you desire!

##### Share on other sites
what exactly is .02f?

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!

##### Share on other sites
It is a modifier, just like your /8

0.02f just says it is a floating point number.

I am assuming that TimeDelta is a float.

multiplying by a modifier is much faster than dividing by one, so it will eliminate your jerkiness.

##### Share on other sites
quote:
Original post by Neen10do
what exactly is .02f?

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!

Umm... 0.02f is a fancy way of saying 0.02 (why the f, I have never understood...)

oh yeah, and 0.02 = 1/50 if that''s what you mean.
1/8 = 0.125 (exactly) if you want to use that.

If the /8 is an arbitrary number especially for that particular function, I''d suggest one of my methods...however, if you /8 all over the place, I''d suggest a global variable of some sort...

aka:

const float deltaMulti = 0.125;

then:

Object.x += Object.Velocity * (TimeDelta * deltaMulti);

or make it a non-const if you want to tweak it in-program, and/or modifiable for speed.

##### Share on other sites
One more thing, you know that time elapsed should be calculated similar to this
float CurrentTime;
float LastTime;
float ElapsedTime;
LastTime=TimeGetTime();

begin loop
CurrentTime=timeGetTime();
TimeElapsed=CurrentTime-LastTime;
LastTime=CurrentTime;

end loop

##### Share on other sites
quote:
Original post by RhoneRanger
It is a modifier, just like your /8

0.02f just says it is a floating point number.

I am assuming that TimeDelta is a float.

multiplying by a modifier is much faster than dividing by one, so it will eliminate your jerkiness.

um...he said TimeDelta wasn't a float, and two: dividing by a float isn't much of a difference...sure, if you loop a million times a second it is, but at 30fps or so...nah...sorry...

(Neen - that dosn't mean you shouldn't still multiply rather than divide! (and again, /8 = *0.125 (or *0.125f)) )

quote:
Original post by RhoneRanger
One more thing, you know that time elapsed should be calculated similar to this
float CurrentTime;
float LastTime;
float ElapsedTime;
LastTime=TimeGetTime();

begin loop
CurrentTime=timeGetTime();
TimeElapsed=CurrentTime-LastTime;
LastTime=CurrentTime;

end loop

He allready has a TimeDelta...he dosn't need to be told how to get one again (TimeElapsed = TimeDelta) (I hope, anyways...) (oh yeah, change the decleration from float ElapsedTime to TimeElapsed...it helps when the names are exactly the same).

[edited by - MaulingMonkey on March 3, 2003 9:37:41 PM]

##### Share on other sites
I hope he meant like 8 miliseconds, but whatever!

still should multiply by a modifier than divide!

##### Share on other sites
well the TimeDelta/8 was because TimeDelta was making it too fast.

this basically did nothing if my timedelta was 8 (which it normally was). this eliminated the purpose.

so what i did then was create the speeds at decimals... then multipled by just TimeDelta.

this worked, but was a little weird, as well.

i used timeDelta * .02f (and returned the velocities back to whole numbers) and it was a lil slow, so i did 1.0f, not knowing what the hell i was doing, and it seemed to work ok--- that is, on my computer. my friend who usually tests my game on her machine just converted to win98 (i know i know) and is getting directx errors. so i cant tell if this time independent movement has made a difference.

would it?

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!

##### Share on other sites
quote:
Original post by RhoneRanger
I hope he meant like 8 miliseconds, but whatever!

still should multiply by a modifier than divide!

Wouldn't be too teribly sure...I remember some great fun with a clock() function in time.h giving me hundredths of a second... which I had to multiply by 10 to prevent myself from getting horribly confused . And I still did agree that he should multiply ^_^.

quote:
Original post by Neen10do
well the TimeDelta/8 was because TimeDelta was making it too fast.

this basically did nothing if my timedelta was 8 (which it normally was). this eliminated the purpose.

so what i did then was create the speeds at decimals... then multipled by just TimeDelta.

this worked, but was a little weird, as well.

i used timeDelta * .02f (and returned the velocities back to whole numbers) and it was a lil slow, so i did 1.0f, not knowing what the hell i was doing, and it seemed to work ok--- that is, on my computer. my friend who usually tests my game on her machine just converted to win98 (i know i know) and is getting directx errors. so i cant tell if this time independent movement has made a difference.

would it?

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!

Depends on how you got TimeDelta. If it's based on a function that returns things like milliseconds, hundredths of a second, or whatever, then yes it will make a difference.

If it's based on processor speed, well then it'll change for each computer (the speed), but it will definatly still keep the program running at a smooth pace on any computer (just faster/slower than you'd like ).

Gah...DX...*shudders* :-p

[edited by - MaulingMonkey on March 3, 2003 9:44:50 PM]

##### Share on other sites
The only problem with what you just said (neen10d0) that if you multiply by 1.0f, you get the same result as if you did nothing!

any number * 1 = any number.

I personally use QueryPerformanceCounter for calculating TimeElapsed, but if you use timegettime, you should do your TimeDelta like I showed above, returning time in miliseconds.

*NOTE You shouldnt get directx errors by multiplying by a float???

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627686
• Total Posts
2978634

• 11
• 14
• 12
• 10
• 12