Hold shift to make the FPS camera go faster

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.
Lorian
Posts: 20
Joined: Sat Feb 10, 2007 9:20 am
Location: United Kingdom
Contact:

Hold shift to make the FPS camera go faster

Post by Lorian »

Hi,

My question is quite simple: How do I make it so when I hold down shift, the movement speed of an FPS camera is increased?

Cheers.
Last edited by Lorian on Wed Nov 07, 2007 4:27 pm, edited 1 time in total.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Check the tutorials for how to use the event receiver. In the event receiver you just check if the event was the shift key being pressed down, if so you speed up the camera, when it's not pressed down then you return the camera to its normal speed.
Image Image Image
Lorian
Posts: 20
Joined: Sat Feb 10, 2007 9:20 am
Location: United Kingdom
Contact:

Post by Lorian »

Yeah I already have the event receiver all set up, and I'm sure I'd be able to get the events sorted, but I don't know how to change the speed of the camera. addCameraSceneNodeFPS just returns a regular old ICameraSceneNode, which has no functions or variables for changing the speed.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Yes, you're right, sorry about that!

You could have two cameras. One setup with the running speed and one setup with the walking speed and then when you press shift you switch the the running one (smgr->setActiveCamera(runningCam);) and then when you let go of shift you change it back to the walking cam.
Image Image Image
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

But if he wants a progressive speed-up, maybe attach the cam to an empty scene then move the scene in a progressive way so the camera would seem to accelerate while holding the shift.
About moving the empty scene, how can he make it progressive? (Moving according to time maybe?)
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Yeah that's another possible solution, you could move it progressively just by saying if the shift button is held down add on a chunk of 'speed' each frame (based on the elapsed time so you get a smooth increase whatever the framerate) until it reached the max speed for running and if shift isn't held down you reduce the speed similarly until it gets to the walking speed.

You'd obviously already have some code to move the dummy scene node, that the camera is attached to, around smoothly based on a speed parameter.
Image Image Image
Lorian
Posts: 20
Joined: Sat Feb 10, 2007 9:20 am
Location: United Kingdom
Contact:

Post by Lorian »

Thanks for the help.

I'm not bothered about a gradual increase in speed, it's not supposed to be representing a human running anyway.

I tried usig two cameras, here's the event receiver code:

Code: Select all

if (event.EventType == irr::EET_KEY_INPUT_EVENT &&
    event.KeyInput.PressedDown) {
	// Fast camera when shift is held down
	if (event.KeyInput.Key == irr::KEY_LSHIFT ||
	    event.KeyInput.Key == irr::KEY_RSHIFT) {
		graphics->fastCamera->setPosition(graphics->normalCamera->getPosition());
		graphics->fastCamera->setRotation(graphics->normalCamera->getRotation());
		graphics->smgr->setActiveCamera(graphics->fastCamera);
	}
} else if (event.EventType == irr::EET_KEY_INPUT_EVENT &&
    !event.KeyInput.PressedDown) {
    	// Normal camera speed when shift is up
    	if (event.KeyInput.Key == irr::KEY_LSHIFT ||
	    event.KeyInput.Key == irr::KEY_RSHIFT) {
		graphics->normalCamera->setPosition(graphics->fastCamera->getPosition());
		graphics->normalCamera->setRotation(graphics->fastCamera->getRotation());
		graphics->smgr->setActiveCamera(graphics->normalCamera);
	}
The camera jumps around when I press shift, sometimes moving quite a large distance from where it was. Any idea?
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

I guess because when you move the normalCamera the fastCamera stays where it was last time before you changed the camera from fast to normal so when you set the fast camera back on, it jumps from the place it was last used to where the normal camera is (before the switch).

To fix it maybe change the position of the fast camera even when it isn't used so when you switch between them it will start from the place the normal camera was last used.

I'm bit tired so forgive me for being maybe unclear about things I said..
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Lorian
Posts: 20
Joined: Sat Feb 10, 2007 9:20 am
Location: United Kingdom
Contact:

Post by Lorian »

MasterGod: Didn't help. :(
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

MasterGod wrote:To fix it maybe change the position of the fast camera even when it isn't used so when you switch between them it will start from the place the normal camera was last used.
right, and don't forget updateAbsolutePosition on the camera after this... ;)


But (in my opinion) the best way is to add a setSpeed function to the camera scene node !!! ;)
like I did with my IrrExtensions... 8)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Why updateAbsolutePosition()?
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Lorian
Posts: 20
Joined: Sat Feb 10, 2007 9:20 am
Location: United Kingdom
Contact:

Post by Lorian »

I tried using updateAbsolutePosition() but it made no difference.

I took a look at your IrrExtensions thing, but it appears to be Windows only (?), which is no good.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

the updateAbsolutePosition is often needed after setting position/rotation of a camera to prevent it from stuttering...


Yes, my IrrExtensions are only for Windoofs, sorry...
But this depends only for the program itself, the extensions are for Irrlicht and so they can be used on any OS...
contact me via email, maybe I can help you for this single extension... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
horvim
Posts: 11
Joined: Thu Aug 30, 2007 2:08 pm
Location: Hungary

Post by horvim »

Try attaching your second camera to the first one, so their positions always will be the same.
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Post by christianclavet »

One thing you could do is: (The way I'm thinking for doing my FPS with run and walk)
- Define 2 cameras a running fps camera and a walking fps Camera
- If you are in walking mode and press shift:
:arrow: Copy the values (position, rotation,etc) of the walking camera to the running one and switch the active camera to the running one.

- If you release the shift:
:arrow: Copy the values (position, rotation, etc) of the running camera to the walking one and switch the active camera to the walking one.

The only problem you will have that way is that you will not be able to have a progressive acceleration. It still could be possible to do a progressive acceleration, but will mean, creating on the fly a new camera with a new speed switch to it, then create another one with the new speed (time based), switch to it. etc. until you have reach the desired speed. That is not very efficient at all.

The best thing would have to control all of the aspect of the FPS camera controller, but the methods are not available at the moment. (So we could only change the camera speed based on time, and you would have your acceleration)

EDIT: Lorian, you code is nice. That would do it. But you could perhaps add a "state" variable to know if the camera is running or walking. (like a boolean). I think that could be the problem with the "jump". Because it's could switch from the fast camera to the slow camera about anytime.
Post Reply