Getting Jerkyness with TimeDelta

Started by
13 comments, last by Neen10do 21 years, 1 month ago
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!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
Advertisement
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

Yes, if timedelta and velocity are integers, try casting them to floating point before doing the math
Disclaimer: "I am in no way qualified to present advice on any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)"
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!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
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.

Your line:
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]
Instead of dividint by 8, try this:

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

tweak that number until you get the results you desire!
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
what exactly is .02f?

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

Aero DX - Coming to a bored Monitor near you!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
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.
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
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.
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;

do all your game stuff

end loop

TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno

This topic is closed to new replies.

Advertisement