Ogre 3D 1.7 Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action — setting the position of a scene node

  1. Add this new line after the creation of the scene node:
    node->setPosition(10,0,0);
    
  2. To create a second entity, add this line at the end of the createScene() function:
    Ogre::Entity* ent2 = mSceneMgr->createEntity("MyEntity2","Sinbad.mesh");
    
  3. Then create a second scene node:
    Ogre::SceneNode* node2 = mSceneMgr->createSceneNode("Node2");
    
  4. Add the second node to the first one:
    node->addChild(node2);
    
  5. Set the position of the second node:
    node2->setPosition(0,10,20);
    
  6. Attach the second entity to the second node:
    node2->attachObject(ent2);
    
  7. Compile the program and you should see two instances of Sinbad:
    Time for action — setting the position of a scene node

What just happened?

We created a scene which matches the preceding diagram. The first new function we used was at step 1. Easily guessed, the function setPosition(x,y,z) sets the position of the node to the given triple. Keep in mind that this position is relative to the parent. We wanted MyEntity2 to be at (10,10,20), because we added node2, which holds MyEntity2, to a scene node which already was at the position (10,0,0). We only needed to set the position of node2 to (0,10,20). When both positions combine, MyEntity2 will be at (10,10,20).

Pop quiz — playing with scene nodes

  1. We have the scene node node1 at (0,20,0) and we have a child scene node node2, which has an entity attached to it. If we want the entity to be rendered at (10,10,10), at which position would we need to set node2?

    a. (10,10,10)

    b. (10,-10,10)

    c. (-10,10,-10)

Have a go hero — adding a Sinbad

Add a third instance of Sinbad and let it be rendered at the position (10,10,30).