Skip to main content
GameDev.net gamedev.net
🔒 Locked

Question about smooth movement!!!

Started by koalacui Sep 17, 2005 at 8:40 AM 7 replies 2.1k views
Original Post
koalacui
koalacui
Hi, everybody, this is the first time i do the keyboard input part in OpenGL, now the program can run , the object also can move according the direction buttons, but when i want to do something like this, when i press UP and RIGHT button at the same time and without release them, the object keep moving towards the 45 degree direction, now when i press them, the object just move once towards the direction, so anyone can teach me how?? thanks a lot !! it is the problem in control codes or other part? this is the control part codes

void SpecialKeys(int key, int x, int y)
{
switch(key) 
	{
		case GLUT_KEY_UP :
			b += 5.5f;   // b stands for y direction
				break;
		case GLUT_KEY_DOWN :
			b -= 5.5f;
				break;
		case GLUT_KEY_LEFT : 
			a -= 5.5f;   // a stands for x direction
				break;
		case GLUT_KEY_RIGHT :
			a += 5.5f;
				break;
	}
}


-=[LOVE always, LIVE forever]=-
deavik
deavik
What you have to do is create an input manager, like so:

bool keys[206]; //for 206 keyscase KEYUP:keys[keycode] = false;case KEYDOWN:keys[keycode] = true;


I dont' exactly know the GLUT API as I don't use it, but you should by able to figure it out. After you've done that,

if (keys && keys[UP]) // if RIGHT and UP are the keycodes   diagonalmovement();if (keys)   rightmovement();


and so on. And BTW, this is funny, I'm posting in 2 posts, both still active (not solved) and both yours [lol]. Seriously, you should first wait for the solution to your other problem. There are quite a few replies you haven't read there.
koalacui
koalacui
deavik, thanks a lot for ur explaination...and the first problem is sloved....haha.....your suggestion and explaination is useful....haha...thanks ..thanks
-=[LOVE always, LIVE forever]=-
deavik
deavik
You're welcome!
Rasmadrak
Rasmadrak
Easy solution:

Use a lot of if's instead of a switch...


if (key == GLUT_KEY_UP)
b += 5.5f; // b stands for y direction

etc...
"Game Maker For Life, probably never professional thou." =)
koalacui
koalacui
Rasmadrak, seems cannot, cannot get the smooth diagonal movement, i saw somewords, but i am not very clear about that, can anybody write some sample code for me to understand, thanks a lot!!!

this is the words;

GLUT 3.7 have a keydown/keyup for the keyboardJust have a large array...bool keys[256];and whenever there is a keydown, set that key to truewhenever there is a keyup, set it to false....then on a timer function, you can move based on your arrays of keys.....


the last sentence i have no idea about that, can anybody explain that to me???

if my codes same as the first thread above, how to make the smooth movement happen in this way!!! thanks a lot!!!
-=[LOVE always, LIVE forever]=-
deavik
deavik
Just refer to the last section of code from my last post. It should work, if I understand correctly. Try it out and see!
deavik
deavik
I'm assuming that was you, koalacui. What do you mean by "program breaks"? Does it crash or hang, or are you getting an unexpected result? If the latter, what is happenning?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.