# Problem with DirectX scene-graph

I'm trying to implement a basic scene graph in DirectX using C++. I am using a left child-right sibling binary tree to do this. I'm having trouble updating each node's world transformation relative to its parent (and its parent's parent etc.). I'm struggling to get it to work recursively, though I can get it to work like this:

for(int i = 0; i < NUM_OBJECTS; i++)
{
// Initialize to identity matrix.
D3DXMatrixIdentity(&mObject[i].toWorldXForm);

int k = i;
while( k != -1 )
{
mObject[i].toWorldXForm *= mObject[k].toParentXForm;
k = mObject[k].parent;
}
}


toWorldXForm is the object's world transform and toParentXForm is the object's transform relative to the parent. I want to do this using a method within my object class (the code above is in my main class). This is what I've tried but it doesn't work (only works with nodes 1 generation away from the root)

if (this->sibling != NULL)
this->sibling->update(toParentXForm);
D3DXMatrixIdentity(&toWorldXForm);
this->toWorldXForm *= this->toParentXForm;
this->toWorldXForm *= toParentXForm;
toParentXForm *= this->toParentXForm;
if (this->child != NULL)
this->child->update(toParentXForm);


Sorry if I've not been clear, please tell me if there's anything else you need to know. I've no doubt it's merely a silly mistake on my part, hopefully an outside view will be able to spot the problem.

• Search for skeletal animation, they solve similar problem of propagating transformations. – user712092 Aug 10 '11 at 18:03

I believe what you are trying to do is simply stack the matrix of each child to generate the toWorldMatrix from a set of toParentMatrices?

Given:

class Object
{

void GenerateWorldMats();

cMatrix toWorld;
cMatrix toParent;

Object* mParent;
Object** mChildren;
uint32 mNumChildren;
};


You can...

void Object::GenerateWorldMats()
{
if(mParent == NULL)
{
toWorld.Identity();
}
else
{
toWorld = mParent.toWorld * toParent;  //Or Swapped order depending on pre or post mult
}

for(int i = 0; i < mNumChildren; ++i)
{
mChildren[i]->GenerateWorldMats();
}
}