• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
belfegor

Bone parent indices to linked list

10 posts in this topic

I have an array of bone parent indices (where i marked root bone as -1). At some point i need to traverse thru all children of some arbitrary bone but this seems tough to do with this setup. I cannot come up with algorithm to create linked list like hierarchy from this , one like in DX9 animation system:

typedef struct D3DXFRAME {
...
D3DXFRAME* pFrameSibling;
D3DXFRAME* pFrameFirstChild;
};

 

where i can simply use recursion to achieve this.

 

Any pointers?

0

Share this post


Link to post
Share on other sites
A hierarchy is a tree, not a list. If you want to reconstruct a tree you first need some method like [tt]AddChild[/tt], then you use a simple map of indices to tree nodes (actually a array/vector could work just fine). Map and add children as you go. Pseudocode:
foreach(node: nodes)
{
    map[node.index] = node;
    if(node.parent >= 0)
        map[node.parent].AddChild(node);
}
This works if no child comes before its anchestor. Otherwise you need two passes: Create nodes first, then assign to parents.
I don't think you can use a (sensible) recursive algorithm here. A recursion needs a hierarchical relation, which you actually want to figure out wink.png

Alternatively, construct a list of indices of the children for each bone, then you can work with the initial array. Assuming children have consecutive indices, then pair of (startindex, endindex) suffices. The latter is the case if e.g. the indices were created using a level-order tree traversal.
2

Share this post


Link to post
Share on other sites


I don't think you can use a (sensible) recursive algorithm here. A recursion needs a hierarchical relation, which you actually want to figure out wink.png

 

I have explain it badly.

Before, with dx9 animation system i was able to traverse thru only children of some bone (and childrens children... and their children) using recursion because that was with tree structure, and now i cannot do that easely with this array. I want to imitate "old" way by building this "tree" which i dont see how yet, still working on it. Thanks anyway.

0

Share this post


Link to post
Share on other sites

I don't think it is necessary to store any explicit hierarchy of the bones - In my animation system I just have a list of bones, and each bone has an associated parent bone index (in the list). This stores the hierarchy implicitly. Then, when computing the final matrices for each bone, I just iterate through the list and lazily compute the concatenated object-space matrices of each bone (and any parent bones, if they have not yet been computed). There doesn't seem to be any reason to store the skeleton hierarchy in a traditional tree structure.

2

Share this post


Link to post
Share on other sites

I have no problem with animation, i was thinking about IK . Then for one character i need to go thru whole structure couple of times in one frame (head facing certain target, legs position on uneven terrain, arms reaching for items...) , with "old" way i could update only what is needed.

0

Share this post


Link to post
Share on other sites

I think i got it without creating tree like hierarchy.

#include <iostream>
#include <vector>

struct Bone
{
    int data;
};

std::vector<int>  vParentIndices;
std::vector<Bone> vBones;

void printChildData(int parentInx)
{
    for(std::size_t i = 0; i < vParentIndices.size(); ++i) // my children
    {
        if((vParentIndices[i] == parentInx) && (i != parentInx))
        {
            std::cout << vBones[i].data << std::endl;
            printChildData(i); // child children
        }
    }
}

int main()
{
    // copied from my character bone structure
    vParentIndices.resize(47);
    vParentIndices[0]  = -1;
    vParentIndices[1]  = 0;
    vParentIndices[2]  = 1;
    vParentIndices[3]  = 2;
    vParentIndices[4]  = 3;
    vParentIndices[5]  = 4;
    vParentIndices[6]  = 5;
    vParentIndices[7]  = 2;
    vParentIndices[8]  = 7;
    vParentIndices[9]  = 8;
    vParentIndices[10] = 9;
    vParentIndices[11] = 2;
    vParentIndices[12] = 11;
    vParentIndices[13] = 12;
    vParentIndices[14] = 13;
    vParentIndices[15] = 14;
    vParentIndices[16] = 15;
    vParentIndices[17] = 15;
    vParentIndices[18] = 15;
    vParentIndices[19] = 15;
    vParentIndices[20] = 14;
    vParentIndices[21] = 20;
    vParentIndices[22] = 21;
    vParentIndices[23] = 22;
    vParentIndices[24] = 23;
    vParentIndices[25] = 24;
    vParentIndices[26] = 25;
    vParentIndices[27] = 23;
    vParentIndices[28] = 27;
    vParentIndices[29] = 28;
    vParentIndices[30] = 23;
    vParentIndices[31] = 30;
    vParentIndices[32] = 31;
    vParentIndices[33] = 14;
    vParentIndices[34] = 33;
    vParentIndices[35] = 34;
    vParentIndices[36] = 35;
    vParentIndices[37] = 36;
    vParentIndices[38] = 37;
    vParentIndices[39] = 38;
    vParentIndices[40] = 36;
    vParentIndices[41] = 40;
    vParentIndices[42] = 41;
    vParentIndices[43] = 36;
    vParentIndices[44] = 43;
    vParentIndices[45] = 44;
    vParentIndices[46] = 2;

    vBones.resize(47);
    for(std::size_t i = 0; i < 47; ++i)
    {
        vBones[i].data = i; // data = inx
    }

    //printChildData(-1);
    printChildData(15);

    return 0;
}

 

Can someone confirm if this could fail?

 

Thanks everybody.

0

Share this post


Link to post
Share on other sites

Can someone confirm if this could fail?

 

Not sure what you mean by "fail." There are, of course, ways it could "fail." You could enter a wrong number. You could mistype some line of code. You could change the number of parent indices and fail to change the hard-coded "47" you have several places. Etc., etc.

 

As an experiment, as coded, assuming all the numbers are correct, to avoid setting up a hierarchy, it should work. However, just as a comment, your method is a very inefficient way to determine the children of a node. You access the entire array of possibilities each time you want to know the children of a single node.

 

In any case, to generalize a bit more, if you want to use a std::vector, it'll make your code easier to revise:

vParentIndices.clear();
vParentIndices.push_back( -1 );
vParentIndices.push_back( 0 );
vParentIndices.push_back( 1 );
...
vParentIndices.push_back( 2 );

vBones.clear();
for( size_t i = 0; i < vParentIndices.size(); ++i) vBones.push_back(i);
Edited by Buckeye
1

Share this post


Link to post
Share on other sites
^ I cannot follow steps from your post #6
 
 
#include <iostream>
#include <vector>

struct Bone
{
    int myInx; // tmp data to print later

    Bone* sibling;
    Bone* child;
};

std::vector<int>  vParentIndices;

void buildTree(Bone* tree)
{
    for(std::size_t i = 0; i < vParentIndices.size(); ++i)
    {
        if(tree->myInx == vParentIndices[i])
        {
            Bone* p = new Bone;
            p->myInx = i;
            p->child = nullptr;
            p->sibling = nullptr;
            if(tree->child == nullptr)
            {
                tree->child = p;
            }
            else
            {
                if(!tree->child->sibling) // step 6
                {
                    tree->child->sibling = p;
                }
                else
                {
                    if(tree->sibling->sibling)
                    {
                        // goto step 6; //HOW to jump to step 6
                    }
                }
            }
        }
    }
}

int main()
{
    //vParentIndices.resize(47);
    vParentIndices.push_back(-1);
    vParentIndices.push_back(0);
    vParentIndices.push_back(1);
    vParentIndices.push_back(2);
    vParentIndices.push_back(3);
    vParentIndices.push_back(4);
    vParentIndices.push_back(5);
    vParentIndices.push_back(2);
    vParentIndices.push_back(7);
    vParentIndices.push_back(8);
    vParentIndices.push_back(9);
    vParentIndices.push_back(2);
    vParentIndices.push_back(11);
    vParentIndices.push_back(12);
    vParentIndices.push_back(13);
    vParentIndices.push_back(14);
    vParentIndices.push_back(15);
    vParentIndices.push_back(15);
    vParentIndices.push_back(15);
    vParentIndices.push_back(15);
    vParentIndices.push_back(14);
    vParentIndices.push_back(20);
    vParentIndices.push_back(21);
    vParentIndices.push_back(22);
    vParentIndices.push_back(23);
    vParentIndices.push_back(24);
    vParentIndices.push_back(25);
    vParentIndices.push_back(23);
    vParentIndices.push_back(27);
    vParentIndices.push_back(28);
    vParentIndices.push_back(23);
    vParentIndices.push_back(30);
    vParentIndices.push_back(31);
    vParentIndices.push_back(14);
    vParentIndices.push_back(33);
    vParentIndices.push_back(34);
    vParentIndices.push_back(35);
    vParentIndices.push_back(36);
    vParentIndices.push_back(37);
    vParentIndices.push_back(38);
    vParentIndices.push_back(36);
    vParentIndices.push_back(40);
    vParentIndices.push_back(41);
    vParentIndices.push_back(36);
    vParentIndices.push_back(43);
    vParentIndices.push_back(44);
    vParentIndices.push_back(2);

    Bone* root = new Bone;
    root->myInx = 0;
    root->child = nullptr;
    root->sibling = nullptr;
    buildTree(root);

    return 0;
}
 
Can you help some more please. Thanks. Edited by belfegor
0

Share this post


Link to post
Share on other sites

DISCLAIMER: This is off the top of my head, so be careful - look through it, test it out and make sure it's compatible with your indices structure. It ASSUMES every frame has a parent, other than root.

D3DXFRAME *rootFrame = new D3DXFRAME;

CreateHierarchy(-1, rootFrame);
...
void CreateHierarchy(int parIdx, D3DXFRAME* parFrame)
{
    for( int child = 0; child < (int)indices.size(); ++child)
    {
        // find any child with parent == parIdx
        if( indices[i] == parIdx )
        {
            D3DXFRAME* newChild = new D3DXFRAME;
            childFrame->pFirstChild = childFrame->pSibling = NULL;
            // do other initialization of childFrame needed HERE
            AddChild( newChild, parFrame );
            CreateHierarchy( child, newChild ); // add any children of this frame
        }
    }
}

void AddChild(D3DXFRAME* childFrame, D3DXFRAME* parFrame)
{
    // find an empty pointer
    if( NULL == parFrame->pFirstChild )
    {
        parFrame->pFirstChild = childFrame;
        return;
    }
    // childNum is a sibling of pFirstChild
    D3DXFRAME *tmpFrame = parFrame->pFirstChild;
    while( tmpFrame->pSibling != NULL ) tmpFrame = tmpFrame->pSibling;
    tmpFrame->pSibling = childFrame;
}

Edited by Buckeye
1

Share this post


Link to post
Share on other sites

Thank you very much on your time and patience Buckeye. smile.png

I think i love you (tho no homo). tongue.png

 

I think that this works. Printed some indices and got expected results, replaced all pointers to shared_ptr so i don't have to worry about freeing memory.

Hopefully i didn't make any mistakes.

 

#include <iostream>
#include <vector>
#include <memory>

struct Bone;

typedef std::shared_ptr<Bone> sp_Bone;

struct Bone
{
    int myInx;
    sp_Bone sibling;
    sp_Bone child;

    Bone( int index ) : myInx(index), sibling(nullptr), child(nullptr) {}
    ~Bone() { }
};

std::vector<int>  vParentIndices;

void AddChild(sp_Bone childFrame, sp_Bone parFrame)
{
    // find an empty pointer
    if( nullptr == parFrame->child )
    {
        parFrame->child = childFrame;
        return;
    }
    // childNum is a sibling of pFirstChild
    sp_Bone tmpFrame = parFrame->child;
    while( tmpFrame->sibling )
        tmpFrame = tmpFrame->sibling;
    tmpFrame->sibling = childFrame;
}

int CreateHierarchy(int parIdx, sp_Bone parFrame)
{
    int count = 1;
    for( int i = 0; i < (int)vParentIndices.size(); ++i)
    {
        // find any child with parent == parIdx
        if( vParentIndices[i] == parIdx )
        {
            sp_Bone newChild = std::make_shared<Bone>(i);
            AddChild( newChild, parFrame );
            count += CreateHierarchy( i, newChild ); // add any children of this frame
        }
    }
    return count;
}

void printTree(sp_Bone tree)
{
    if(tree)
    {
        std::cout << tree->myInx << std::endl;
        
        printTree(tree->sibling);
        printTree(tree->child);
    }
}

void printChildren(sp_Bone bone)
{
    if(bone->child)
    {
        std::cout << bone->child->myInx << std::endl;
        printChildren(bone->child);
    }
}

void printSiblings(sp_Bone bone)
{
    if(bone->sibling)
    {
        std::cout << bone->sibling->myInx << std::endl;
        printSiblings(bone->sibling);
    }
}

sp_Bone findBone(sp_Bone root, int inx)
{
    if(root)
    {
        if(root->myInx == inx)
            return root;
        auto sb = findBone(root->sibling, inx);
        if(sb)
            return sb;
        auto cb = findBone(root->child, inx);
        if(cb)
            return cb;
    }
    return nullptr;
}

int main()
{
    vParentIndices.push_back(-1);
    vParentIndices.push_back(0);
    vParentIndices.push_back(1);
    vParentIndices.push_back(2);
    vParentIndices.push_back(3);
    vParentIndices.push_back(4);
    vParentIndices.push_back(5);
    vParentIndices.push_back(2);
    vParentIndices.push_back(7);
    vParentIndices.push_back(8);
    vParentIndices.push_back(9);
    vParentIndices.push_back(2);
    vParentIndices.push_back(11);
    vParentIndices.push_back(12);
    vParentIndices.push_back(13);
    vParentIndices.push_back(14);
    vParentIndices.push_back(15);
    vParentIndices.push_back(15);
    vParentIndices.push_back(15);
    vParentIndices.push_back(15);
    vParentIndices.push_back(14);
    vParentIndices.push_back(20);
    vParentIndices.push_back(21);
    vParentIndices.push_back(22);
    vParentIndices.push_back(23);
    vParentIndices.push_back(24);
    vParentIndices.push_back(25);
    vParentIndices.push_back(23);
    vParentIndices.push_back(27);
    vParentIndices.push_back(28);
    vParentIndices.push_back(23);
    vParentIndices.push_back(30);
    vParentIndices.push_back(31);
    vParentIndices.push_back(14);
    vParentIndices.push_back(33);
    vParentIndices.push_back(34);
    vParentIndices.push_back(35);
    vParentIndices.push_back(36);
    vParentIndices.push_back(37);
    vParentIndices.push_back(38);
    vParentIndices.push_back(36);
    vParentIndices.push_back(40);
    vParentIndices.push_back(41);
    vParentIndices.push_back(36);
    vParentIndices.push_back(43);
    vParentIndices.push_back(44);
    vParentIndices.push_back(2);

    sp_Bone root = std::make_shared<Bone>(0);
    int cnt = CreateHierarchy(0, root);
    std::cout << "bone count: " << cnt << std::endl;

    //printTree( root );

    auto bone = findBone(root, 16);
    if(bone)
    {
        //printChildren(bone);
        printSiblings(bone);
    }

    return 0;
}
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0