Keyboard Not Responding.

Feb 23, 2012 at 6:14pm
closed account (2NywAqkS)
I have been beginning to use the Win32 API and I'm starting to making a simple top-down shooter however to get keyboard intput i used the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
case WM_KEYDOWN:
			{
				switch (wParam)
				{
				case 'w':
					{
						Xpos += cos(Soldier_angle) * 10;
						Ypos += sin(Soldier_angle) * 10;
						return 0;
					}
				case 's':
					{
						Xpos += cos(Soldier_angle+180) * 10;
						Ypos += sin(Soldier_angle+180) * 10;
						return 0;
					}
				case 'a':
					{
						Xpos += cos(Soldier_angle+270) * 10;
						Ypos += sin(Soldier_angle+270) * 10;
						return 0;
					}
				case 'd':
					{
						Xpos += cos(Soldier_angle+90) * 10;
						Ypos += sin(Soldier_angle+90) * 10;
						return 0;
					}
				default:
					break;
				}
			}


but nothing seems to respond.

this draws the Soldier:

1
2
3
4
5
6
7
8
glPushMatrix();
	glTranslatef(Xpos, Ypos, 1.0);
	POINT cursorPos;
        GetCursorPos(&cursorPos);
        Soldier_angle = atan2(cursorPos.y-Ypos,cursorPos.x-Xpos)*180/3.14159265;
	glRotatef(Soldier_angle, 0,0,1);
	Square(48,"Soldier 1.raw",48,48);
	glPopMatrix();


What could be the problem? any help is much appreciated.

Thanks Rowan.
Feb 23, 2012 at 6:17pm
IIRC the key codes are uppercase. Try 'A', 'S', etc instead of 'a', 's', etc.
Feb 23, 2012 at 6:21pm
closed account (2NywAqkS)
Thank you that solved it!
Feb 23, 2012 at 6:41pm
closed account (2NywAqkS)
Another problem with it is that all keys the soldier moves toward the mouse. I expected 'S' to move him backwards but that doesn't happen. the movement is also a bit weird he moves in a little circle before he moves towards the mouse. Why?
I changed the keyboard input code to:
1
2
3
4
5
6
7
8
9
10
11
case WM_KEYDOWN:
			{
				keys[wParam] = true;
				return 0;
			}

		case WM_KEYUP:
			{
				keys[wParam] = false;
				return 0;
			}

and made a key operations fuction:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void KeyOperations(void)
{
			if (keys['W'])
				{
					Xpos += cos(Soldier_angle) * 0.1;
					Ypos += sin(Soldier_angle) * 0.1;
				}
			if (keys['S'])
				{
					Xpos += cos(Soldier_angle+180) * 0.1;
					Ypos += sin(Soldier_angle+180) * 0.1;
				}
			if (keys['A'])
				{
					Xpos += cos(Soldier_angle+270) * 0.1;
					Ypos += sin(Soldier_angle+270) * 0.1;
				}
			if (keys['D'])
				{
					Xpos += cos(Soldier_angle+90) * 0.1;
					Ypos += sin(Soldier_angle+90) * 0.1;
				}
}
Feb 23, 2012 at 7:21pm
1) Looks like soldier_angle is in degrees based on this:

Soldier_angle = atan2(cursorPos.y-Ypos,cursorPos.x-Xpos)*180/3.14159265

Which means you need to convert back to radians before you use it here:

Xpos += cos(Soldier_angle) * 0.1; // <- need radians, not degrees



2) angles rotate counter-clockwise, not clockwise. So you have 'A' and 'D' backwards. +90 degrees would go left, not right.
Feb 23, 2012 at 7:50pm
closed account (2NywAqkS)
Thankyou everything fixed!
Feb 23, 2012 at 7:54pm
np. Glad it's working.

It always bugged me how OpenGL does things in degrees. Blech.
Topic archived. No new replies allowed.