Using the keyboard to rotate a FPSCamera?

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
Magnetikal

Using the keyboard to rotate a FPSCamera?

Post by Magnetikal »

I'm messing around with the source to the demo that comes with Irrlicht. I've been trying to add keyboard control so that you can use e.g. [a] and [d] to rotate left and right. The engine only seems to have built-in support for using the mouse for rotation.

I have keyboard rotation sort of working, but there must be a better way. What I've done for now is comment out the lines in the switchToNextScene() function that set [a] and [d] to EKA_STRAFE_LEFT and EKA_STRAFE_RIGHT.

Then, in the OnEvent() function, I have the following:

Code: Select all

if (event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown == true)
{
	if (event.KeyInput.Key == KEY_KEY_A)
	{
		FPScamera->setRotation(FPScamera->getRotation() + core::vector3df(0,-3,0));
	}
	else if (event.KeyInput.Key == KEY_KEY_D)
	{
		FPScamera->setRotation(FPScamera->getRotation() + core::vector3df(0,3,0));
	}

	FPScamera->OnEvent(event);
...
}
The problem is that this is dependent on the computer's key repeat rate. So just like in a text editor, when you hold down [a] it rotates left slightly, pauses for a split second, then continues to rotate. It's also a problem because the rotation isn't smooth, it's basically rotates every x ms depending on the key repeat rate.

Can anyone help me on how I can add better keyboard support for rotation? I'm hoping it doesn't require serious mucking around with the engine source code.
Dogs
Posts: 195
Joined: Wed Sep 15, 2004 1:04 am
Location: michigan

Post by Dogs »

I was also interested in doing this at one time also so any info that anyone gives you ill be back to read about as well.... :) Im sure that someone here in this forum can help...
jikbar
Posts: 62
Joined: Wed Aug 25, 2004 4:48 pm
Location: Canada
Contact:

Post by jikbar »

in your if statement, get rid of event.KeyInput.PressedDown == true

i think that's how you do it
zgock
Posts: 12
Joined: Wed Sep 29, 2004 2:06 pm
Location: Japan
Contact:

Post by zgock »

Why dont u use keyMapArray on add addCameraSceneNodeFPS() ? :roll:
see Reference :D
Magnetikal

Post by Magnetikal »

in your if statement, get rid of event.KeyInput.PressedDown == true
Unfortunately that doesn't change anything.
Why dont u use keyMapArray on add addCameraSceneNodeFPS() ?
That's what I'm currently doing. The problem is that there is no support for keyboard actions like 'look left' or 'look right'. For example, I can set keys to 'EKA_STRAFE_LEFT' but there is no 'EKA_LOOK_LEFT'. Unless I'm misunderstanding your suggestion?
Guest

Post by Guest »

In your OnEvent() I would just check, if [a] or [d] is pressed or released. Set a boolean variable to true (pressed) or false (not pressed).

Then, in another function, called every frame, you check if the key is pressed and you do the rotation. You can multiply the rotation degrees with the time that passed since last frame got rendered, that should be smooth.

I'm doing it this way and it is working nicely.


Hope, that helps

Ronin
Magnetikal

Post by Magnetikal »

Thanks very much, that's a great help! One more question - how can I call a method every frame? Is there some sort of method called every frame (like OnEvent() is called on any event) that I can put my method call in?
Guest

Post by Guest »

Your game loop should be the right place to call that method.
My game loop consist of two parts, the rendering part, with beginScene() and endscene() and stuff and the run part, where all update stuff of all objects is handled.

in my main.cpp it looks something like this:

Code: Select all

CGameEngine Engine;

while(Engine.Run())
{
    Engine.Render();
}
You can take a look at ICE by Keless, which is a very good framework to learn from. And if I'm serious, before I looked at ICE I didn't have any clue where to start. :)

Ronin
Post Reply