Advertisement

Strange error with lesson 7 keyboard control

Started by December 16, 2001 01:03 PM
9 comments, last by BauerGL 23 years, 2 months ago
Hi, I have a problem with lesson nr 7 and the keyboard control part. The thing is this, that everything works well except that when I for example push the VK_LEFT button, the object spins according to what it should. But when I push the VK_RIGHT button the object just stops and i can''t rotate it on that axis anymore. The same goes for zooming and VK_UP/DOWN. It''s probably just some stupid typo somewhere but i can''t find it. :[ For the code part, i have it almost exactly like lesson 7, except that i have all the keyboard control in a separate function. So any tips on what could be wrong is appreciated. Thanks, Bauer. // Can''t come up with somethin good.

@mikaelbauer

Super Sportmatchen - A retro multiplayer competitive sports game project!

Might be a point to begin with :

Check that you''re not setting a wrong rotation value by pressing VK_RIGHT. It would indeed stop rotating if you set the rotation value to 0 or something like that. Further, you might check that you''re not re-setting the rotation-value anywhere in the program. You might do something like that for example for testing purposes, and forget that you did it later...
Newbie programmers think programming is hard.Amature programmers think programming is easy.Professional programmers know programming is hard.
Advertisement
Yeah, but the strange thing is that i can press VK_LEFT, and the object spins accordingly. But when i want it to rotate the other way around it just stops. And after it has stopped I cant rotate it anymore... thats the strange thing.

@mikaelbauer

Super Sportmatchen - A retro multiplayer competitive sports game project!

maybe you can post some code? might be easier for others to help you that way
maybe a forgotten break; in a switch statement?

--
New feature - Windows users, press ALT-F4 to launch the IQ test.
--New feature - Windows users, press ALT-F4 to launch the IQ test.
Here is the Winmain code:
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)	// Programmets start{	Window.done = FALSE;										// Används för att gå ut ur loopen	if(MessageBox(NULL,"Fullskärmsläge?","Fullskärm",MB_YESNO | MB_ICONQUESTION) == IDYES)		// Fråga efter fullskärm		Window.fullscreen = TRUE;								// Om NEJ, sätt fullskärm till falskt	if(!CreateGLWindow(title,WINDOW_WIDTH,WINDOW_HEIGHT,WINDOW_BPP,Window.fullscreen))			// Skapa GL fönster		return 0;												// Om det misslyckas skicka felmeddelande	while(!Window.done)	{		if(PeekMessage(&Window.msg,NULL,0,0,PM_REMOVE))			// Finns det ett meddelande?		{			if(Window.msg.message == WM_QUIT)					// Är det ett quit meddelande				Window.done = TRUE;								// Sätt done till TRUE			else			{				TranslateMessage(&Window.msg);					// Översätt meddelandet				DispatchMessage(&Window.msg);								}		}		else													// Om det inte finns några meddelanden		{			if((Window.active && !DrawGLScene()) || Key[VK_ESCAPE])									// Är programmet aktivt			{				Window.done = TRUE;							// avsluta programmet			}				else											// annars rita scenen				{					SwapBuffers(hDC);							// Byt buffer (dubble buffer)										// Kolla om någon knapp blir nedtryckt					LookKeyDown();					if(Key[VK_F1])					{						Key[VK_F1] = FALSE;						KillGLWindow();						Window.fullscreen =! Window.fullscreen;											if(!CreateGLWindow(title,WINDOW_WIDTH,WINDOW_HEIGHT,WINDOW_BPP,Window.fullscreen))						{	return 0;}					}				}		}	}	// Stäng ner	KillGLWindow();												// Döda fönstret											return Window.msg.wParam;									// Avsluta programmet} 


And here is the LookKeyDown() function code, which looks for pressed keys:
void LookKeyDown()												// Se efter tangentnedtryckningar{	if(Key[VK_PRIOR])		z -= 0.02f;	if(Key[VK_NEXT])		z += 0.02f;	if(Key[VK_UP])		xspeed -= 0.1f;	if(Key[VK_DOWN])		xspeed += 0.1f;	if(Key[VK_LEFT])		yspeed -= 0.1f;	if(Key[VK_RIGHT])		yspeed += 0.1f;} 


I think thats all code that has to do with the keyboard control.
Advertisement
Uhm that didn''t go too well... sorry for that one, im totally knew here. Please just ignore the above post.

Here we go again, this is the WinMain code:
  int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)	// Programmets start{	Window.done = FALSE;										// Används för att gå ut ur loopen	if(MessageBox(NULL,"Fullskärmsläge?","Fullskärm",MB_YESNO | MB_ICONQUESTION) == IDYES)		// Fråga efter fullskärm		Window.fullscreen = TRUE;								// Om NEJ, sätt fullskärm till falskt	if(!CreateGLWindow(title,WINDOW_WIDTH,WINDOW_HEIGHT,WINDOW_BPP,Window.fullscreen))			// Skapa GL fönster		return 0;												// Om det misslyckas skicka felmeddelande	while(!Window.done)	{		if(PeekMessage(&Window.msg,NULL,0,0,PM_REMOVE))			// Finns det ett meddelande?		{			if(Window.msg.message == WM_QUIT)					// Är det ett quit meddelande				Window.done = TRUE;								// Sätt done till TRUE			else			{				TranslateMessage(&Window.msg);					// Översätt meddelandet				DispatchMessage(&Window.msg);								}		}		else													// Om det inte finns några meddelanden		{			if((Window.active && !DrawGLScene()) || Key[VK_ESCAPE])									// Är programmet aktivt			{				Window.done = TRUE;							// avsluta programmet			}				else											// annars rita scenen				{					SwapBuffers(hDC);							// Byt buffer (dubble buffer)										// Kolla om någon knapp blir nedtryckt					LookKeyDown();					if(Key[VK_F1])					{						Key[VK_F1] = FALSE;						KillGLWindow();						Window.fullscreen =! Window.fullscreen;											if(!CreateGLWindow(title,WINDOW_WIDTH,WINDOW_HEIGHT,WINDOW_BPP,Window.fullscreen))						{	return 0;}					}				}		}	}	// Stäng ner	KillGLWindow();												// Döda fönstret											return Window.msg.wParam;									// Avsluta programmet}  


And here is the LookKeyDown() function, which looks for pressed keys.
  void LookKeyDown()												// Se efter tangentnedtryckningar{	if(Key[VK_PRIOR])		z -= 0.02f;	if(Key[VK_NEXT])		z += 0.02f;	if(Key[VK_UP])		xspeed -= 0.1f;	if(Key[VK_DOWN])		xspeed += 0.1f;	if(Key[VK_LEFT])		yspeed -= 0.1f;	if(Key[VK_RIGHT])		yspeed += 0.1f;}  


Please forgive me for my n00biness above :]

Thanks, Bauer.

@mikaelbauer

Super Sportmatchen - A retro multiplayer competitive sports game project!

I had a similar problem once. I think I fixed it by putting Parenthses in the if statements like this.

if (Key[VK_UP])
{
xspeed -= 0.1f;
}

don''t ask me why but that worked for me.


"To err is human, to really mess up requires a computer"
"To err is human, to really mess up requires a computer"
Seems like your keyPressed[MY_KEY] values are never reset to false and therefore, left and right keys cancel each other. Are you sure you get the WM_KEYUP (and not only WM_KEYDOWN) message ? Could you post your winProc function ?
quote:
BauerGL
Uhm that didn''t go too well... sorry for that one, im totally knew here. Please just ignore the above post.



Since you''re new I''ll introduce you to a nifty feature:
1) Hit edit on the post you want to remove
2) On that page check the box that says whether or not you want to delete it.
3) Hit the modify button (Don''t forget to include your name and pass)
4) Ta-Da! Your post has been removed freeing up the valuable GDNet server space.

Thanks!

This topic is closed to new replies.

Advertisement