Jump to content

Entity Hierarchy question


VeTaL
 Share

Recommended Posts

Now i'm working with Unity's Head Control sources: http://unity3d.com/support/resources/unity-extensions/head-look-controller

Everything goes good, but now i come to Unity's code

	// Setup segments
	foreach (BendingSegment segment in segments) {
		Quaternion parentRot = segment.firstTransform.parent.rotation;
		Quaternion parentRotInv = Quaternion.Inverse(parentRot);

		segment.referenceLookDir =
			parentRotInv * rootNode.rotation * headLookVector.normalized;
		segment.referenceUpDir =
			parentRotInv * rootNode.rotation * headUpVector.normalized;

So, now i need to run through all hierarchy of entities. What i do?

1) as i know that

m_pHead = FindChild(m_body, "Bip01 Head");

would find needed part of model, i go and search info about FindChild on wiki... I found it here: http://www.leadwerks.com/wiki/index.php?title=Category:Entity_Hierarchy

2) as i see, there are functions CountChildren() and ForEachEntityDo(). But problems are started here: if i write

int abc = CountChildren(m_body);

i got abc = 1 in debugger... ForEachEntityDo() is used for level entity, as i see from description: "Scans all entities in the current world, and calls a user defined function for each entity found."

 

So, the question is: how can i run through all hierarchy of entity: head, spine, neck, for example, and why CountChildren(m_body) returns 1 (i test crawler, and he has a lot of segments), when FindChild() works properly, while they both are functions from one category ?

Working on LeaFAQ :)

Link to comment
Share on other sites

CountChildren() counts all direct child entities (children of exactly this entity) while FindChild() finds children and subchildren (->recursive).

 

To loop through all bones of a skeleton, you'll have to write a recursive method that stores the current bone and calls the store method for all of it's chilren.

To do thaat, it would use CountChildren() and GetChild().

Link to comment
Share on other sites

Btw, is anybody need this code snippet

void LookForChild( TEntity ent) {
   int childNum = CountChildren( ent );

TurnEntity(ent, Vec3(45,0,0)); // call here any function you want for all sub-items, but this looks funny.

   for( int i = 1; i <= childNum; ++i )
       LookForChild( GetChild(ent, i) );
}

Working on LeaFAQ :)

Link to comment
Share on other sites

This would be more general purpose:

void ForEachChildDo(TEntity entity, void (*function)(TEntity,byte*), byte* extra, bool recursive = true, bool includeSelf = true)
{
   for(int i = 1; i <= CountChildren(entity); i++)
   {
       if (recursive)
           ForEachChildDo(GetChild(entity, i), function, extra);
       else
           function(GetChild(entity, i), extra);
   }
   if (includeSelf)function(entity, extra);
}

Link to comment
Share on other sites

  • 3 months later...

I just happened to be in need of this function myself and noticed that the current function I posted above limits the functionality that the generic "function()" might implement. With the above function, it wasn't possible to make it modify the hierarchy since that would lead to errors.

 

In order to allow the called function to do pretty much anything, ForEachChildDo should rather look like:

void ForEachChildDo(TEntity entity, void (*function)(TEntity,byte*), byte* extra = NULL, bool recursive = true, bool includeSelf = true)
{
   // Store current children to allow function() to modify the hirarchy without causing any severe errors.
   int iChildren = CountChildren(entity);

   if (iChildren > 0)
   {
       TEntity* children = new TEntity[iChildren];

       for (int i = 0; i < iChildren; i++)
           children[i] = GetChild(entity, i + 1);

       // Execute
       for (int i = 0; i < iChildren; i++)
       {
           if (recursive)
               ForEachChildDo(children[i], function, extra);
           else
               function(children[i], extra);
       }

       delete[] children;
   }

   if (includeSelf && EntityExists(entity))function(entity, extra);
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...