[GAME] Smooth translation problem using keyboard input

Hi everyone !
If my english isn’t perfect, excuse me, I am a belgian IT student.

I’ll try to make it simple to understand. We have to code a 2D game and therefore, we have to move our character through the map.

So, each time I press, let’s say ‘d’, the y coordinate is updated and affect the glTranslatef(x36.0, y36.0, 0) which precedes the drawing function.

Consequence on screen : player move from his current position, and is translated 36 pixels further on the right. No problems to signal.

BUT, if I want a continuous translation through the entire map, I want to be able to do it by pressing continuously the keyboard’s ‘d’ button. Problem comes here. When doing so, the translation through the map isn’t smooth at all, like there were a break every 36 pixels, or every time the programs updated the key status and sees that it’s still set as “true” meaning it can repeat the glTranslatef once again.

I’d like that to be fluid, smooth, and without mini breaks that renders the move in a pretty ugly way. After hours of research and tries, I still can’t figure out what’s going on and how to fix it.

Does anyone know what the problem is and how to fix it? I would be very grateful for it, as I am struggling and losing my nerves.

Thanks to all !

if (keyStates[27])
		exit(0);
	else if (keyStates['d']){
		if (timer['d'] > TIME_2_PRESS){ 
			timer['d'] = 0;
			moveRight(&game, map); //increase y coordinate by 1
		}
		else
			timer['d']++;
		
	}
void display_dragon(){ //draws my player
	
	glColor4f(1, 1, 1, 1);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, dragon);
	
	glPushMatrix();
	glTranslatef(yr*36.0, xr*36.0, 0.0);	
				
	glBegin(GL_QUADS);
   		glTexCoord2d(0.0, 1.0); glVertex2i(0,36);
    	        glTexCoord2d(0.0, 0.0); glVertex2i(0,0);
    	        glTexCoord2d(1.0, 0.0); glVertex2i(36,0);
    	        glTexCoord2d(1.0, 1.0); glVertex2i(36,36);
   	glEnd();

   	glPopMatrix();
    		
    glDisable(GL_TEXTURE_2D);    
}
void display_map(){ //display function called by glutDisplayFunc(display_map) in main function
	keyOperations();
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
	glLoadIdentity();// Clear display window

	drawMap();	

	display_dragon();
		
	glutSwapBuffers();	
}

Little video of my game screen so far, so you can better understand what I mean when telling you, movement isn’t smooth and fluid.

Move less quickly through your screen coordinates.
Jeff

I think it is more subtle than that, because I’ve already tried that, result remains the same, it moves slowly but still doesn’t make a uniform translation when keeping the ‘d’ button pressed.

I know it’s possible the fix this problem, I just cannot find how yet, that’s why I am posting here.

I’ve tried to change the FPS rate with glutTimerFunc(), but that doesn’t fix anything.

You should be moving a small amount every frame, not moving a large amount every N frames.

If you want smooth movement, you should either scale the movement distance by the time elapsed since the previous movement, or (better) arrange for the movement code to be called at a fixed rate regardless of the frame rate.

Yes, I agree, base your movement on pixels per time, rather than pixels per frame.

Jeff