Page 1 of 1

Switching between nodes with a keystroke

Posted: Tue Aug 30, 2011 12:59 pm
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));
        }
}
 
 

Re: Switching between nodes with a keystroke

Posted: Tue Aug 30, 2011 1:12 pm
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).

Re: Switching between nodes with a keystroke

Posted: Tue Aug 30, 2011 1:17 pm
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.

Re: Switching between nodes with a keystroke

Posted: Tue Aug 30, 2011 2:17 pm
by teto
just react on a KeyInput event when PressedDown member equals 1 (or 0 but you have to check that condition)

Re: Switching between nodes with a keystroke

Posted: Tue Aug 30, 2011 3:32 pm
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?

Re: Switching between nodes with a keystroke

Posted: Tue Aug 30, 2011 3:36 pm
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.