c++arraysogreogre3d

Ogre3d scenenode array


Currently I am working on a pcg city in ogre3d, basically getting close to a room marked as a city will spawn a city there. However I am kind of at a loss on how to "store" the buildings into an array and checking their positions to handle the collision. In the method I get the size of the plane the buildings need to spawn in, after that I create a _cityNode, which will house all the building nodes in it. These get set in the for-loop. In the buildings variable I try to get buildingNode in the array so i can check the collision in another method. I basically have 2 questions:

  1. How do I get the buildingNode into an array?
  2. Is this method approach of "uniqueness of buildingNodes correct or do I need another approach?

    void CityManager::generateCity(int sizeX, int sizeZ, int _numberOfBuildings)
    {
    
    #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
    FILE* fp;
    freopen_s(&fp, "CONOUT$", "w", stdout);
    _rootNode = GameManager::getSingletonPtr()->getSceneManager()->getRootSceneNode();
    _cityNode = _rootNode->createChildSceneNode("cityNode");
    printf(" number of buildings: %d \n", _numberOfBuildings);
    printf(" location of X: %d  location of Z: %d \n", sizeX, sizeZ);
    for (int i = 0; i < _numberOfBuildings; i++)
    {
        Ogre::SceneNode* buildingNode = _cityNode->createChildSceneNode("buildingNode" + i);
        Ogre::Entity* _buildingEntity = GameManager::getSingletonPtr()->getSceneManager()->createEntity("cube.mesh");
    
        buildingNode->createChildSceneNode()->attachObject(_buildingEntity);
        buildingNode->setPosition(rand() % sizeX , 50, rand() % sizeZ);
        buildingNode->setScale(rand() % 6+1 , rand() % 6 + 1, rand() % 6 + 1);
    
    
        Ogre::Vector3 buildingpos = buildingNode->getPosition();
        Ogre::Vector3 buildingscale = buildingNode->getScale();
        //_buildings = new Ogre::SceneNode[buildingNode];
        checkCollision();
        checkEntryWay();
    
        printf("positions of building nodes %f, %f, %f " , buildingpos.x, buildingpos.y, buildingpos.z);
        printf("scale of building nodes %f, %f, %f \n", buildingscale.x, buildingscale.y, buildingscale.z);
    
    
    }
    fclose(fp);
    #endif
    }
    

Solution

  • You can use any STL container you want, Ogre::SceneNode* is completely compatible with them.

    As an example you can collect your nodes in std::vector like

    std::vector<Ogre::SceneNode*> buildings_;
    for (int i = 0; i < _numberOfBuildings; i++)
    {
      /* ... */
      buildings_.push_back(buildingNode);
    }
    

    And later access them like

    for (auto i : buildings_) {
      Ogre::Vector3 buildingpos = i->getPosition();
      Ogre::Vector3 buildingscale = i->getScale();
    }