Thread: Issues with polymorphism, I guess?

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    Issues with polymorphism, I guess?

    Alright, here is the idea.

    A child of a parent node needs a combined matrix from the parent node.

    A child of a parent node, can also be a parent node.

    A parent node, doesn't have anything before it, so it has to create the combined matrix

    A child node needs a combined matrix from a parent node, to create a new combined matrix

    PROBLEM: The overridden virtual update function needs to take a local matrix, which is combined with the parents combined matrix, to make a new combined matrix. Problem lies within the fact that we now need to send this combined matrix to the child, via the update function, I can't do that! :\.

    CODE:

    Scene Node base class.
    Code:
    class CSceneNode
    {
    public:
    	CSceneNode() { }
    
    	virtual ~CSceneNode() { Destroy(); }
    
    	void Release() { delete this; }
    
    	virtual void Update()
    	{
    		for( std::list<CSceneNode*>::iterator i = m_lstChildren.begin();
    			i != m_lstChildren.end(); i++ )
    		{
    			(*i)->Update();
    		}
    	}
    
    	void Destroy()
    	{
    		for( std::list<CSceneNode*>::iterator i = m_lstChildren.begin();
    			i != m_lstChildren.end(); i++ )
    		(*i)->Release();
      
    		m_lstChildren.clear();
    	}
    
    	void AddChild( CSceneNode* pNode )
    	{
    	   m_lstChildren.push_back(pNode);
    	}
    
    protected:
    	// list of children
    	std::list<CSceneNode*> m_lstChildren;
    };
    Geometry Node Subclass
    Code:
    class CGeometryNode : public CSceneNode
    {
    public:
    
    	CGeometryNode(boost::weak_ptr<MS3DModel> Resource_Observer, float m[4][4]) 
    	{ 
    		Resource_Observer = Geometry;
    		for( int i = 0; i < 4; i++ )
    			for( int j = 0; j < 4; j++ )
    				LocalMatrix[i][j] = m[i][j];
    	}
    
    	~CGeometryNode() { }
    
    	void Update(float LocalMatrix[4][4])
    	{
    		// create combined matrix
    		// Draw Geometry Here!
    
    		CSceneNode::Update(); // need to send a new combined matrix to children to work with!!!
    	}
    
    
    private:
    	float LocalMatrix[4][4];
    	float CombinedMatrix[4][4];
    	boost::weak_ptr<MS3DModel> Geometry;
    };
    Somehow I need to give all children access to the parent node's combine matrix.

    Probably more of a design issue...
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Could you elaborate your desing a little bit? It looks like you are trying to implement the composite design pattern but not quite getting there. Have a look at these pages:
    http://www.google.com/search?q=composite+design+pattern

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    In return I'll give you a link showing you exactly what I'm doing

    http://www.gamedev.net/reference/pro...es/scenegraph/

    Lets see, to explain more...

    A geometry node is a scene node

    A geometry node can be a parent to child nodes, and can also be a child node


    Every geometry node has a Local translation

    Every geometry node has a combined transformation

    Every child geometry node creates a combined transformation node out of adding its own local transformation to the parents combined transformation.

    The virtual update function is overridden in the geometry class, it now takes the local translation of that geometry class.

    In virtual update function we have to create a new combined transformation matrix, but we need the parents transformation matrix before that... And we have to pass the new combined transformation to the next children

    NOTE:

    I'm thinking a simple pointer to the parent node will solve this problem.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    A geometry node is a scene node

    A geometry node can be a parent to child nodes, and can also be a child node
    OK, that would translate into:
    Code:
    class SceneNode
    {
    };
    
    class GeometryNode : SceneNode
    {
        vector<GeometryNode> children;
    
        void update(float matrix[4][4]);
    
        member functions to get add and remove children
    };
    
    void GeometryNode::update(float matrix[4][4])
    {
        update myself using "matrix"
        update my children using my updated transformation
    }
    (Or something like that, I'm still not too familiar with the C++ syntax)

    I'm thinking a simple pointer to the parent node will solve this problem.
    Maybe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Number guessing.
    By Lunatic Magnet in forum C Programming
    Replies: 5
    Last Post: 04-07-2006, 12:43 AM
  4. Guess the number game
    By Ninestar in forum C Programming
    Replies: 12
    Last Post: 12-08-2005, 11:30 AM
  5. Replies: 3
    Last Post: 11-13-2005, 02:53 AM