Jump to content
  • Advertisement
Sign in to follow this  
kubapl

[C++/OpenGL] Movement Question

This topic is 3750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey so a couple of weeks ago I've put together a 3dloader and got it to work that it also loads in a texture and applies it to the object. It loads in ase files exported from 3d studio max. Anyways my question is about general movement handling. I wrote a switch statement that handles my orientation for the object and was wondering why whenever I hit a key it takes like 1 second for it to start moving the object. And also why is it that I cant hit multiple keys and the keys do there thing at the same time? Here's a sample of how I'm handling movements:
void myKeyboard( unsigned char key, int x, int y )
{
	CVector vOri = Model->getOrientation();
	
	switch( key )
	{
	case 27:
		{
			exit(0);
			break;
		}	
	case 'w':
		{
			vOri.fy += 0.05f;
			break;
		}
	case 's':
		{
			vOri.fy -= 0.05f;
			break;
		}
	case 'd':
		{
			vOri.fz += 0.05f;
			break;
		}
	case 'a':
		{
			vOri.fz -= 0.05f;
			break;
		}
	case 'q':
		{
			vOri.fx += 0.05f;
			break;
		}
	case 'e':
		{
			vOri.fx -= 0.05f;
			break;
		}
	default:
		break;
	}
	
	Model->setOrientation( vOri );

}


And then this is of course called in my main game function:
int main( int argc,  char** argv )
{
	glutInit( &argc, argv );
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
	glutInitWindowSize( SCREENWIDTH, SCREENHEIGHT ); 
	glutInitWindowPosition ( 100, 100 );
	glutCreateWindow("3D Loader");	
	
	myInit();

	glutDisplayFunc( myDisplay );
	
	glutReshapeFunc( myReshape );
	glutKeyboardFunc( myKeyboard );
	glutSpecialFunc( SpecialKey );
	glutIdleFunc( Idle );
	glutMainLoop();


	return 0;
}


And you can download a copy of the 3dloader here: 3DLoader @ http://kubapl.ath.cx/projects/3DLoad.zip Controls are: [w] - rotate on the y axis [s] - rotate on the y axis [a] - rotate on the z axis [d] - rotate on the z axis [q] - rotate on the x axis [e] - rotate on the x axis So any help on this would be nice and also tell me what you think about the loader I'm hoping that I can make a game out of this. Thank Kubapl

Share this post


Link to post
Share on other sites
Advertisement
The reason you can't do multiple keys at once is because you're using a switch statement and breaking each key. So basically, if you had w, and d pressed, it would process w and break the case statement. The keyboard event only fires once even if multiple keys are selected so alternatively you could use if statements to cover each key, don't use else if statements, it needs to be one individual if statement per key. Removing the breaks really isn't an option because then it will process every key after the first valid one.

As for why the model takes 1 second to process the change, how often do you redraw, you might try outputting the status of the model to the screen at every redraw (the v0ri information) and see if it is updating that data immediately on keypress or if there is a delay, that will at least narrow it down to your keypress event or your draw event.


Share this post


Link to post
Share on other sites
Yeah I donno dude switching it from case statement to just if statement made no difference.

But thanks for the input.

Share this post


Link to post
Share on other sites
maybe you used 'else if' in your movement(its still like switch with breaks).Try this kind of code:



if(key==27){
exit(0);
}
else{
CVector vOri = Model->getOrientation();

if(key=='w'){
vOri.fy += 0.05f;
}
else if(key=='s'){
vOri.fy -= 0.05f;
}
if(key=='d'){
vOri.fz += 0.05f;
}
else if(key=='a'){
vOri.fz -= 0.05f;
}
if(key=='q'){
vOri.fx += 0.05f;
}
else if(key=='e'){
vOri.fx -= 0.05f;
}

Model->setOrientation( vOri );
}





hope it helps

Share this post


Link to post
Share on other sites
Quote:

Yeah I donno dude switching it from case statement to just if statement made no difference.

But thanks for the input.


Now that i look back on it, i just realised i never got multiple simultaneous keys working either, i did however find an old thread about the same issue.

Check out this thread
http://www.gamedev.net/community/forums/topic.asp?topic_id=466416

Hopefully the links in the second post will give you the answer you need.

Essentially the keyboard handler itself only takes one key input, so no mater how many are pressed, it's the latest one that gets the attention, so you need to monitor the state of the keys (up or down) and process based on keydown, not on keypress.

If you're using visual C++ it has handlers for keydown
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx

I hope this helps and makes sense.



Share this post


Link to post
Share on other sites
One method that may help is doing something like this:


/*Heading of your main file*/
bool move_up;
bool move_down;
bool move_left;
bool move_right;

/*key-press event*/
void myKeyboardDownFunc( unsigned char key, int x, int y )
{

switch( key )
{
case 27:
{
exit(0);
break;
}
case 'w':
{
move_up = true;
break;
}
case 's':
{
move_down = true;
break;
}
case 'd':
{
move_right = true;
break;
}
case 'a':
{
move_left = true;
break;
}

}

}

/*Then you'll need a release key function too. GLUT can also do this by the way*/
void myKeyboardUpFunc( unsigned char key, int x, int y )
{

switch( key )
{
case 27:
{
exit(0);
break;
}
case 'w':
{
move_up = false;
break;
}
case 's':
{
move_down = false;
break;
}
case 'd':
{
move_right = false;
break;
}
case 'a':
{
move_left = false;
break;
}

}

}

/*Finally the game-update function*/
void Update( float delta_time )
{

if( move_up )
{
vOri.fy += 0.05f * delta_time;
}

if( move_down )
{
vOri.fy -= 0.05f * delta_time;
}

if( move_left )
{
vOri.fz += 0.05f * delta_time;
}

if( move_right )
{
vOri.fz -= 0.05f * delta_time;
}

}





That should do the trick. I multiplied the rotating/moving speed by delta_time which is just a variable which you'll have to declare which is the computer's processing speed which just makes it work smoothly on every computer you try it on.

I hope this helped!

[Edited by - PCN on June 19, 2008 10:59:32 PM]

Share this post


Link to post
Share on other sites
if you were going to monitor key down then you would also have to monitor key up, unless key down stays on while the button stays down....

Share this post


Link to post
Share on other sites

void myKeyboard( unsigned char key, int x, int y )
{
if(key == 27) exit(0);

CVector vOri = Model->getOrientation();

if(key == 'w') vOri.fy += 0.05f;
if(key == 's') vOri.fy -= 0.05f;
if(key == 'd') vOri.fz += 0.05f;
if(key == 'a') vOri.fz -= 0.05f;
if(key == 'q') vOri.fx += 0.05f;
if(key == 'e') vOri.fx -= 0.05f;

Model->setOrientation(vOri);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!