Key input events

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
Tannz0rz
Posts: 35
Joined: Fri May 08, 2009 12:25 am
Location: Orlando FL, USA

Key input events

Post by Tannz0rz »

I'm busy messing around with 3rd-person cameras, and I'm trying to get the camera to rotate in a fluent motion while the player is moving forward/backward/strafing. But if I attempt at having the player both running and rotating, it either does one or the other:

Code: Select all

bool pressedkeys[256];

void PlayerKeys()
{

	float speed  = 4.0; 
	core::vector3df r = player->getRotation(); 
	core::vector3df v = player->getPosition();

	if(pressedkeys[KEY_LEFT])
	{

		r.Y = r.Y - 4.0f; 
		player->setRotation(r);

	}

	if(pressedkeys[KEY_RIGHT])
	{

		r.Y = r.Y + 4.0f; 
		player->setRotation(r);

	}

	if(pressedkeys[KEY_KEY_W])
	{

	v.X = v.X + ( cos(r.Y * 3.14159265/180)*speed ); 
	v.Z = v.Z - ( sin(r.Y * 3.14159265/180)*speed ); 
	player->setPosition(v);

	}

	if(pressedkeys[KEY_KEY_S])
	{
	
	v.X = v.X - ( cos(r.Y * 3.14159265/180)*speed ); 
	v.Z = v.Z + ( sin(r.Y * 3.14159265/180)*speed ); 
	player->setPosition(v);

	}

	if(pressedkeys[KEY_KEY_A])
	{

	v.X = v.X + ( cos((r.Y-90) * 3.14159265/180)*speed ); 
	v.Z = v.Z - ( sin((r.Y-90) * 3.14159265/180)*speed ); 
	player->setPosition(v);

	}

	if(pressedkeys[KEY_KEY_D])
	{

	v.X = v.X + ( cos((r.Y+90) * 3.14159265/180)*speed ); 
	v.Z = v.Z - ( sin((r.Y+90) * 3.14159265/180)*speed ); 
	player->setPosition(v);

	}

}

class MyEventReceiver : public IEventReceiver 
{ 

public: 
	virtual bool OnEvent(const SEvent& event) 
	{ 

		if (event.EventType == EET_KEY_INPUT_EVENT) 
		{ 

			pressedkeys[event.KeyInput.Key] = event.KeyInput.PressedDown;
			PlayerKeys();

		} 

		return false; 
	} 

}; 
Does anyone know how to ensure that it actually does move in a fluent motion? My current setup simply isn't doing so.

As a side note, using something similar to this:

Code: Select all

if(pressedkeys[KEY_KEY_W] && pressedkeys[KEY_LEFT])
Doesn't make much of a difference.

If you're having trouble understanding the problem, I could make a video about the issue upon request.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Key input events

Post by randomMesh »

Try to move PlayerKeys(); into your mainloop. Besides that, your code is framerate dependend and very unoptimized.

Wrong forum, btw.
"Whoops..."
Tannz0rz
Posts: 35
Joined: Fri May 08, 2009 12:25 am
Location: Orlando FL, USA

Re: Key input events

Post by Tannz0rz »

randomMesh wrote:Try to move PlayerKeys(); into your mainloop. Besides that, your code is framerate dependend and very unoptimized.

Wrong forum, btw.
Okay, I'll give that a try. And I know that it's in the wrong forum, I realized that after I posted it. An admin could move it I suppose, lul.

EDIT: Oh, cool. It worked perfectly. Thanks a lot!
Post Reply