Jump to content
• Advertisement

object iteration

This topic is 4499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Suppose, in C++, I have an object of type CScene. CScene can contain objects that have implemented the class CSceneObject. How do I iterate through ALL scene objects to use the functions or properties of the base class? (The base class contains information to use in collision detection. To test a wall against a camera, wich ofcause are of different type) Any comment would be helpfull and appreciated. Dennis

Share this post

Share on other sites
Advertisement
If using inheritance and the base class function has not be overwritten then the base class function will be called.

Share this post

Share on other sites
That's right, but I want to itterate through all different subclasses.

Share this post

Share on other sites
You may introduce a class CSceneGroup that inherits CSceneObject and can contain several other CSceneObject instances. Notice please that CSceneGroup is able to contain other CSceneGroups since it is derived from CSceneObject. This is called the composite pattern. You may derive CScene from CSceneGroup if you want. The only drawback is that theoretically you can add another CScene into an existing CScene. That may or may not be senseful.

Now, having the above structure, you can implement routines that perform iteration. You may implement a functional purpose into this routine, but then you will end up implementing dozens of such routines (e.g. one for finding a node by name, one for extracting bounding boxes, one for BSP extraction, one for whatever else, ...). Normally it is better to implement the purpose in an external class, and let the composite structure only drive the iteration. E.g. the base class of all those algorithmic classes may be called CSceneVisitor. So the CSceneObject class may provide a routine CSceneObject::visitingDepthFirst(CSceneVisitor* visitor) for a depth first iteration, and so on. Then the visitor may provide a CSceneVisitor::visiting(CSceneObject*).

This concept has the drawback that a visitor has to determine the concrete type of any visited CSceneObject. However, it is nowadays consensus that nodes of a scene graph should not be specialized by inheritance too much. Instead, collaborators/aggregation should be the way of choice. Said so, it is relatively simple for visitors to ask visited nodes for special kind of aggregates. E.g. CSceneObject::getLocalBBox(). Making the bbox global may then be the task of the visitor.

Hope that this is something in the direction you are looking for?!

Share this post

Share on other sites
Quote:
 Original post by MarauderXThat's right, but I want to itterate through all different subclasses.

You say that "CScene can contain objects that have implemented the class CSceneObject." What method do you use to do this? Do you use an array? std::vector? std::list?

Also, how do you create your CSceneObject instances? Do you use new, and thus obtain pointers?

The general way to do this is to have a container of CSceneObject*. Then, just go through the whole container, and call the functions that you need.

//Initialization of a std::vector<CSceneObject*> called mSceneObjects:mSceneObjects.push_back(new CSceneObject_Box(1, 1, 1, 0, 0, 0));mSceneObjects.push_back(new CSceneObject_Box(1, 1, 1, 10, 0, 0));mSceneObjects.push_back(new CSceneObject_Box(20, 1, 20, 0, -1, 0));mSceneObjects.push_back(new CSceneObject_Monster(0, 1, 0));//Iteration later on:std::vector<CSceneObject*>::iterator iObject;for (iObject = mSceneObjects.begin(); iObject = mSceneObjects.end(); ++iObject){  (*iObject)->Update(Time);}//Alternate iteration using indices:std::vector<CSceneObject*>::size_type iObject;for (iObject = 0; iObject < mSceneObjects.size(); ++iObject){  mSceneObjects[iObject]->Update(Time);}//Cleanup:std::vector<CSceneObject*>::iterator iObject;for (iObject = mSceneObjects.begin(); iObject = mSceneObjects.end(); ++iObject){  delete *iObject;}mSceneObjects.clear();

Share this post

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 10
• 28
• 20
• 9
• 20
• Forum Statistics

• Total Topics
633409
• Total Posts
3011722
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!