an input handling routine

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
$ystem_meltdown

an input handling routine

Post by $ystem_meltdown »

I've been using irrlicht for about a month and I found a couple of small details that could be improved a bit. First is the camera movement (moving the camera in horisontal plane instead of line of view)and second the mouse input routine. Mouse movement isnt smooth at all in irrligcht. Heres the code i put together(a bit is taken from another thread on this forum)

Code: Select all

scene::ICameraSceneNode* camera = 0;

#define SCREEN_W 640
#define SCREEN_H 480
//Create a keys[] arry for key input
bool keys[irr::KEY_KEY_CODES_COUNT] ;
class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if(event.EventType==EET_KEY_INPUT_EVENT){
			keys[event.KeyInput.Key] = event.KeyInput.PressedDown; 
			return true;
		}
		
		return 0;
	}
};
//main input processing proc
void handleInput(){
	//Roteate the camera accordin to mouse movement
	int nXDiff,nYDiff;
	POINT g_currentMousePos;
	GetCursorPos(&g_currentMousePos);
	SetCursorPos(SCREEN_W/2,SCREEN_H/2);

	nXDiff=g_currentMousePos.x-SCREEN_W/2;
	nYDiff=g_currentMousePos.y-SCREEN_H/2;

	vector3df rot;
	rot=camera->getRotation();
	rot.Y+=(float)nXDiff/2;
	rot.X+=(float)nYDiff/2;
	//prevent the camera from rotating all the way arround
	if(rot.X<-80)
		rot.X=-80;
	if(rot.X>80)
		rot.X=80;
	camera->setRotation(rot);
	//end rotate camera
	//handle keyboard input
	if(keys['W']){
		vector3df forward( sinf( camera->getRotation().Y*PI/180.0f ), 
			0, cosf( camera->getRotation().Y*PI/180.0f ) ); 
		camera->setPosition( camera->getPosition() + forward*10.0f ); 
	}
	else if(keys['S']){
		vector3df forward( sinf( camera->getRotation().Y*PI/180.0f ), 
			0, cosf( camera->getRotation().Y*PI/180.0f ) ); 
		camera->setPosition( camera->getPosition() - forward*10.0f ); 
	}
	if(keys['A']){
		vector3df forward( sinf( (camera->getRotation().Y-90)*PI/180.0f ), 
			0, cosf( (camera->getRotation().Y-90)*PI/180.0f ) ); 
		camera->setPosition( camera->getPosition() + forward*10.0f );
	}
	else if(keys['D']){
		vector3df forward( sinf( (camera->getRotation().Y+90)*PI/180.0f ), 
			0, cosf( (camera->getRotation().Y+90)*PI/180.0f ) ); 
		camera->setPosition( camera->getPosition() + forward*10.0f );
	}
}
Post Reply