Time for action — building a tree using scene nodes

This time, we are going to use another model besides Sinbad: the ninja.

  1. Remove all the code from the createScene() function.
  2. Create Sinbad like we always do:
    Ogre::Entity* ent = mSceneMgr->createEntity("MyEntity","Sinbad.mesh");
    Ogre::SceneNode* node = mSceneMgr->createSceneNode("Node1");
    node->setPosition(10,10,0);
    mSceneMgr->getRootSceneNode()->addChild(node);
    node->attachObject(ent);
    
  3. Now create a ninja, which will follow Sinbad everywhere he goes:
    Ogre::Entity* ent2 = mSceneMgr->createEntity("MyEntitysNinja","ninja.mesh");
    Ogre::SceneNode* node2 = node->createChildSceneNode("node2");
    node2->setPosition(10,0,0);
    node2->setScale(0.02f,0.02f,0.02f);
    node2->attachObject(ent2);
    
  4. Compile and run the application. When you take a closer look at Sinbad, you will see a green ninja at his left arm.
    Time for action — building a tree using scene nodes
  5. Now change the position to (40,10,0):
    node->setPosition(40,10,0);
    
  6. And rotate the model 180 degree around the x-axis:
    node->yaw(Ogre::Degree(180.0f));
    
  7. Compile and run the application.
  8. You should see that the ninja is still at the left-hand of Sinbad and Sinbad is rotated.
    Time for action — building a tree using scene nodes

What just happened?

We created a sneaky ninja who follows each step of Sinbad. We made this possible because we added the node the ninja model is attached to as a child to the scene node Sinbad is attached to. When we moved Sinbad, we used his scene node, so each transform we did is also done to the ninja, because this scene node is the child of the node we modify, and as said before, the transformation of a parent is passed to all its children. This fact about scene nodes is extremely helpful to create the following models or complex scenes. Say, if we wanted to create a truck which carries a complete house, we could create the house using a lot of different models and scene nodes. At the end, we would have a scene node where the whole house and its interior are added as children too. Now when we want to move the house, we simply attach the house node to the truck or anything else, and if the truck moves, the complete house moves with it.

What just happened?

The arrows show the direction the transformations are propagated along the scene graph.

Pop quiz — even more about the scene graph

  1. How is transformation information passed in a scene graph?

    a. From the leaves to the root

    b. From the root to the leaves

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.231.189