Multiple Key press

When I press W, my camera translates the scene inwards. When I press A, the camera translates the scene to the left. S and D do the opposites. When I press W and then S or D, it stops translating inward and starts translating across. How can I make it so that when I press W and S, it translates both across and inward at the same time?

Here is my camera code:

void camera (void) {
	int posz, posx;
	posz = zpos + 1;
	posx = xpos + 1;
	ypos = map[posx][posz];
	ypos += 2;

Here is my keypress code:

void normalKey (unsigned char key, int x, int y) {
	float yrotrad;
	switch (key) {
		case 'w' :
			yrotrad = (yrot / 180 * 3.141592654f);
			xpos += float(sin(yrotrad)) * 0.2;
			zpos -= float(cos(yrotrad)) * 0.2;

		case 'a' :
			yrotrad = (yrot / 180 * 3.141592654f);
			xpos -= float(cos(yrotrad)) * 0.2;
			zpos -= float(sin(yrotrad)) * 0.2;

And here is my main code:


(I am using GLUT in C++)

I think you’d have to stop using glut and use an API that can handle simultaneous key presses, like DirectX or SDL. (I’m not sure about SDL, as I haven’t used it but it should be there.) I believe that even the win32 can’t handle simultaneous key presses.

Umm, where did that come from? Because I know for a fact that GLUT can handle them. I downloaded some code for a tutorial on GLUT and keypress and it does it perfectly (from LightHouse 3D). I am just having trouble making it possible with my already existing code.

I also know that Win32 windows can handle multiple keypresses I used them in Visual Basic, im not sure about SDL either, and I don’t like Direct X.

Take a look at glutKeyboardUpFunc(). Use it in conjunction with glutKeyboardFunc() - in latter set press flag in former - reset it, and use flag value in your display function. You always will know whether you key is pressed or released and will be able to implement all appropriate movements…

The problem is because your keypress code is using a switch statement which is only telling the program what to do when you press one key OR the other - not both.

What happens after the ‘break’ statement in your switch is that it drops out of the code and doesn’t perform anything else. So it will only allow you to push one key at a time! It will only run the code for the ‘a’ key if it didn’t run the code for the ‘w’ key!

What you really need to use is an IF statement that says, if ‘w’ key is pressed then increase/decrease your forward movement value. Then you need another IF that says, if ‘a’ is pressed then increase/decrease your sideways movement value.

That way the two values work independantly of each other and should respond to your key pressed accordingly. :slight_smile:

Hmm, I will give it a go. But then how does the Lighthouse 3D tutorial work :confused: it uses the case function, and i switched over to it because it meant less code.

Hmm, just tried it and it doesn’t work.

Hmm… I am out of ideas.