Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am kinda new in game programming, but I've got a semestral work to make a game, so I decided to do a clone of Geometry Wars. Despite that the game is really simple (in principle), I have ran into a problem I can not solve. I use SDL, OpenGL in C (vanilla C, so no C++/C# and stuff, only procedural ANSI (I think ANSI) C). When I get input from joystick, it is fine in the console (if I flush it to the output, it will make immediate action) but when I draw a simple dot (it shouldn't be that time consuming to draw a dot on screen) it have a slight to severe input lag (up to 3 sec.). But that's only the first part of my problem, because it not only renders slow, the movement is totally different from that I make with joystick. Here is the code:

void pollInput()
    SDL_JoystickUpdate ();
    double XYValue;
    if ( ( event.jaxis.value < -3200 ) || (event.jaxis.value > 3200 ) )
        if( event.jaxis.axis == 0)
            XYValue = event.jaxis.value / 2500.0;
            XYValue = floor(XYValue);
            player.playerX += (XYValue);
            printf("X: %.0lf\n", XYValue);
        if( event.jaxis.axis == 1)
            XYValue = event.jaxis.value / 2500.0;
            XYValue = floor(XYValue);
            player.playerY += (-XYValue);
            printf("Y: %.0lf\n", XYValue);

I am really stuck here. And another question came to my mind, is it a good idea to add the input from joystick to the XY coords of the player? Shouldn't I make it like acceleration? If yes, would you kindly write a pseudocode for that? Thank you for your replies guys!

share|improve this question
Acceleration can make it feel sluggish. But you may want to accelerate quickly to a top speed so it's a little smoother. – Almo Mar 2 '13 at 23:14
Off topic, but I'm not sure I agree with the characterization of Geometry Wars as "really simple" – Cameron Fredman Mar 3 '13 at 2:46
up vote 1 down vote accepted

Your problem is with how you're using SDL_PollEvent.

You want to do this:

while( SDL_PollEvent(&event) )
    // event-handling code goes here

The problem is that SDL generates many events per rendered frame, but your code is only processing one of those events, and leaves the others still pending. This is going to be where your lag is coming from -- any unprocessed events are left in the event queue, so when your code runs, it's picking up not-yet-processed events from the past, instead of current ones.

SDL_PollEvent() returns 1 to indicate that it has given you an event, or 0 if there was no event for you to process. So by putting it inside a while loop, you'll continue to process events each frame until there are no more left to process. This means that each frame, you will only process events which were generated for that frame, and you'll process all of them, instead of only one.

share|improve this answer
Also note that if you're picking up joystick values from SDL Events, you don't need to call SDL_JoystickUpdate(). That function is for updating the cached values returned by the SDL_Joystick*() functions, which you're not calling in the code you've shown here. – Trevor Powell Jun 1 '13 at 23:51
This is how you should handle SFML events as well. In fact, many systems with message/event queues should use this handling method. – Nick Caplinger Jun 2 '13 at 1:13

I assume player.playerX is the position of X. I am not sure how large event.jaxis.value can go, but

XYValue = event.jaxis.value / 2500.0;
XYValue = floor(XYValue);
player.playerX += (XYValue);

means the player will move at a speed proportionally to how far you move the joystick, I guess (I have no experience about joysticks.)

Apparently you seem you don't like the movement.

If you want to make it acceleration, you will need a velocity (for X and Y axis) variable for your player.

// I recommend you put the dividend it higher or the character may accelerate too fast.
XYValue = event.jaxis.value / 8000.0; 
XYValue = floor(XYValue);
player.velocityX += (XYValue);
if (sqrt(player.velocityX * player.velocityX + player.velocityY * player.velocityY) > 20) 
    player.velocityX = 20; // implement max speed
// same goes for Y axis, not repeating.

then, in your game loop,

player.playerX += player.velocityX;
// same goes for Y axis, not repeating.

Max speed would be necessary such that

  • your player won't move too fast that the player cannot follow.
  • prevent problems in collision detection (when player, in one frame, goes through an enemy without two shapes overlapping at all.)

EDIT about the input lag, I would suggest you look into

  1. Is the joystick input already laggy? Try write up a very simple code that just reads the joystick input and print some plain text output, presumably with printf, and do not do any other things.

  2. Look into FPS of your game - though it should be unlikely according to your description, but who knows. See how long, in wall clock time, it takes to complete an iteration of your game loop for your game.

share|improve this answer
it can go from -32k to +32k on both X and Y axis. I like the movement, but not the way it works, because it is inaccurate and there is big input lag. This is why I doubted my initial code – Citrus Mar 3 '13 at 7:28
I know it may be difficult to describe what you want. about the input lag, see my edit. – luiges90 Mar 3 '13 at 7:56
no, the joystick isn't laggy and I mentioned it in my question, so that is out. and for FPS, the FPS seems to be pretty high it draws me fast and seamlessly, but only the input is lagged. I can send you the whole code if you'd like and compile it under codeblocks – Citrus Mar 3 '13 at 8:30
Hm... You may want to look into SDL documentation about joysticks, Other than that, I don't know what other causes... And I don't have codeblocks or joysticks, sorry. – luiges90 Mar 3 '13 at 8:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.