Direct Input Lag

Hello !

Currently working on a game at work, i have a problem with direct input ( or maybe directx as a whole ).

When the framerate is quite low ( <30 fps) i have a lag on the controls ( tested on joysticks and keyboard) :  ie when I push the left key, the main character moves 0.7 s later. I measured it with a simple test : flash on screen on a regular basis and i press a key when i see it , then i substract the time the frame took (in order not to be distracted by the rendering time )

Results : 0.2s more at low fps ( 10-15) than at 30

Due to the game engine, i can not use multithreading and so the polling is done on every frame.

I already tried to put a sleep(1) at the end of the game loop and to use win32 api instead of dinput but it seems to have no effect...

Hope someone can help !
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

well, multithreading is certainly one good approach to keep a consistant input stream going.

also, <30 fps isn't 'quite low'.  manageable input should be decent down to 15 generally.

two things:
1. sprinkle input pollinh in a few places in the code, not just once per frame.  separate the polling from the translation to in-game events.
2. make sure you handle the actual input=>action translations just BEFORE rendering each frame, to make sure that frame has the closest update (that is, input=>physics, etc. should be done just before rendering).  if you do only one even poll, do it just before physics, which should be just before rendering.

I'm not sure how valid your test is, depends on reaction time, the input tests you are doing (key-down, I assume, not key-repeat), and where you are polling in the overall scheme of things.  The test is actually the other direction: hit a key quickly, and have it TOGGLE on (or off) a box on the screen.  You should be seeing the box toggle within 1 frame typically.

The only case I've ever seen 'lag' has been with certain joysticks and steering wheels.

madcowderAuthor Commented:
Hello !
Thanks for your help

I moved the process of the input after the one with the npc and just before the transform of the players. It seems to me there's a little less lag but there's one, still.

You say input should be decent down to 15fps .. Is there a reason ? Or is it just because a 15fps it become much more noticeable ?

I should say that i've got no problem running it on consoles at all.

As far as the testing method is concerned, i tried your way : the box definitly does not appear on the next frame ( I have a fps counter refreshin every frame on the topp of the screen )

Hope anyone can help me further
What kind of keyboard are you using?  Are you polling for key down or key repeat or key up?

I've used DInput on and off for almost a decade, and I don't know of any issue with 'input lag' that wasn't because of the code.  I believe the polling mechanism should get you a key-down event near-instantly.  You could write an app that tests that pretty easily.

Certainly, if you poll for input, then do update, then render, if your update phase takes long enough, and your render-setup phase takes long enough, your input could be after the poll but still before the rendering.  You could test that by doing a specific poll right before you finish rendering, and do your visual thing at that point, so that the poll and the finish/flip are as near as possible (there's still possible actual delay between the point you poll and how long it takes the frame to render and flip -- and syncing to VSYNC will make it longer).


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.