Controlling a character using keyboards only

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
savagedp
Posts: 5
Joined: Wed Oct 18, 2006 5:38 am

Controlling a character using keyboards only

Post by savagedp »

Hello everyone. I just started using irrlicht and need some help here...
i tried the tutorials in irrlicht and i managed to create a third person camera view on my model. it follows my character when it moves anywhere. But I've got a problem here. How to move the character move diagonally?

I'm using the class MyEventReceiver : public IEventReceiver provided by irrlicht in the examples.
I've made changes so that it accepts W A S D for movements.. however when i try to press more than 1 key at a time... for example W and A together to move diagonally left, it just takes on W or A depending on which one was pressed later...

how to I solve this problem?

I once thought of binding the FPS camera to the model while moving and use the camera's movements to control the character movement. but I ended up making the model fly sky high...

Oh I'm trying to make a game where the view is similar to Metal Gear Solid.. Where you can move your character in any direction and the camera doesn't really change view. like when you turn your character back, he just faces you...

Will appreciate it if i can get some help here
3ddev
Posts: 169
Joined: Tue Sep 19, 2006 6:51 am
Contact:

Here is how to fix the problem...

Post by 3ddev »

Hi! I don't know Metal Gear Solid myself, but I think I can help you. You seem to have the right idea for controlling your character. I assume this is FPS game? I began making one, similar to Star Wars Battlefield in XBox. I decided that the FPS camera was the way to go, but of course my character would fly all over the place. I eventually posted a thread in the forum for fixing this and JP was able to solve my problem. It is really is quite simple. Obviously you make your camera the Parent to your model first. Then you have to make sure the Y target is disabled for the FPS camera. This is the same as disabling the user from looking up or down; the FPS can rotate but only look level. By doing this the FPS camera can't change height. Here is the code for doing this. Note the use of Vector3df. You could just use simple ints but this would require more coding and would be not as well integrated with Irrlicht. Also be aware of the constant "height" which I have set to 500. Change this to anything you want:

Code: Select all

vector3df current_camera_target;
current_camera_position = camera->getTarget();
vector3df correct_camera_postion;
correct_camera_postion.Y = 500;
camera->setTarget(correct_camera_postion);
Just add this in the device->run() loop and this will correct the problem. Be aware that on some really, really slow computers this might make your app run a bit slowly and jiggly. For your info, you might have seen people suggest the "noVerticalMovement" flag for the FPS camera. This stops the camera from changing height, but the 3rd person view can be directed up and down. You character would then continue to fly all around! This is not what you want!:lol:

I hope this has helped,
3ddev
savagedp
Posts: 5
Joined: Wed Oct 18, 2006 5:38 am

Post by savagedp »

ermmmm thanks for the help
but the game i'm making is not really a FPS...

its a third person game.... just that player movement is not determined by mouse... rather the camera just follows the character wherever he goes...

camera never changes view. it just changes position.
its like setting the camera->settarget(MYMODEL)
and then setting teh camera to be some distance away from teh model .
then if the player moves left, the camera still looks at it and shift its position left by the same amount too. and also the same for forawrd back and right...

I think i'm not explaining very well. haha sorry about it.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

There have been loads of threads on 3rd person cameras so if you do a search for them you should be able to find an easy solution :)
Image Image Image
savagedp
Posts: 5
Joined: Wed Oct 18, 2006 5:38 am

Post by savagedp »

actually my issue wasn't the camera. i know how to settle the camera problem. its my character control via keyboard... haha
i couldn't press 2 keys at the same time to move the character...

but me and my friend has somehow found a method to over come it alreayd. thanks all anyway!!!

i think its my fault for not being clear in the first place :)
kneri
Posts: 32
Joined: Sun Sep 17, 2006 4:55 pm
Location: Austria

Post by kneri »

how about getting the keys from a 3rd party library or from the system?
you dont have to use the irrlicht callback for the keys.

If you use 3rd party librarys you can even offer the playercontrol with a joystick or someting,....
Dibalo
Posts: 30
Joined: Sat Aug 12, 2006 2:31 pm
Location: Finland
Contact:

Post by Dibalo »

Make your own key state class, which allows you to check key presses. You can apply my event-classes. They may help you: FILE

And the usage:

Code: Select all

input::EventHandler eventHandle;
   
IrrlichtDevice* device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(640, 480),
	       16, false, false, false, &eventHandle);

eventHandle.initMouseSupport(device);

input::MouseHandler Mouse(&eventHandle, device);
ipunt::KeyBoardHandler Keys(&eventHandle);

// ... other code....

// main loop:
while(!Keys.Hit(KEY_ESCAPE))
{

	// beginScene-function
	
	if(Keys.Down(KEY_KEY_A) && Keys.Down(KEY_KEY_B))
	{
		// do something...
	}

	Mouse.Update();
	Keys.Update();

	// endScene-function
}
Dattebayo!!
Post Reply