Switching between nodes with a keystroke

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
brick
Posts: 36
Joined: Sun Jul 10, 2011 12:15 pm

Switching between nodes with a keystroke

Post by brick »

I have placed two identical nodes on the scene, node1 and node2. I want to be able to alternate between them during runtime at the stroke of a key, and then to trigger the selected node's animations. I have created an additional pointer called "node" that points to one of the two nodes which the user has selected. So, whenever user pushes the S button, a different node is selected and the camera jumps to it, and everything in the while loop is done on the "node" pointer.

And it works with the following code, but it doesn't work perfectly. A lot of the times I have to push the key real hard for it to switch, or multiple times. Could anyone tell me if the problem with the switching is due to my code or is it something that can't be fixed?

Code: Select all

 
IAnimatedMeshSceneNode* node = node1;
int pom = 1;
 
//later, in the while(device->run()) loop
 
if(receiver.IsKeyDown(irr::KEY_KEY_S))
{
     if(pom == 1)
     {
           node = node2;
           pom = 2;
           cam->setPosition(core::vector3df(60,-10,-10));
           cam->setTarget(core::vector3df(70,0,50));
      }
      
      else
      {
            node = node1;
            pom = 1;
            cam->setPosition(core::vector3df(50,-10,-10));
            cam->setTarget(core::vector3df(50,0,50));
        }
}
 
 
Brainsaw
Posts: 1177
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Re: Switching between nodes with a keystroke

Post by Brainsaw »

I think the "isKeyDown" will return "true" as long is the key is pressed, so the nodes will be switched every frame. You should block switching after the first switch until the key is released (and re-pressed).
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
brick
Posts: 36
Joined: Sun Jul 10, 2011 12:15 pm

Re: Switching between nodes with a keystroke

Post by brick »

That makes sense. If the key is held for any longer than one frame, it switches back and forth. Do you have any suggestions about how I could block it? The class is basically the same one used in the movement tutorial, derived from EventReceiver.
teto
Posts: 159
Joined: Thu Dec 03, 2009 9:37 pm
Location: /home
Contact:

Re: Switching between nodes with a keystroke

Post by teto »

just react on a KeyInput event when PressedDown member equals 1 (or 0 but you have to check that condition)
Using trunk with mingw/gcc 4.6, Windows 7 64 bits driver opengl
brick
Posts: 36
Joined: Sun Jul 10, 2011 12:15 pm

Re: Switching between nodes with a keystroke

Post by brick »

Thanks! It works great now.

Now, is there a way for the switching between two camera positions to include movement, rather than cutting? What I mean is that right now, when I press the key that switches, it cuts from the first to the second position. Is it possible for it to do a smooth pan from one to the other?
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Switching between nodes with a keystroke

Post by serengeor »

brick wrote:Thanks! It works great now.

Now, is there a way for the switching between two camera positions to include movement, rather than cutting? What I mean is that right now, when I press the key that switches, it cuts from the first to the second position. Is it possible for it to do a smooth pan from one to the other?
You could try:
1) using an animator (fly straight?).
2) code something yourself that would interpolate between these two positions.
Working on game: Marrbles (Currently stopped).
Post Reply