8
\$\begingroup\$

I have been trying to build a 2D skeletal animation system for a while, and I believe that I'm fairly close to finishing. Currently, I have the following data structures:

struct Bone {
    Bone *parent;
    int child_count;
    Bone **children;
    double x, y;
};

struct Vertex {
    double x, y;
    int bone_count;
    Bone **bones;
    double *weights;
};

struct Mesh {
    int vertex_count;
    Vertex **vertices;
    Vertex **tex_coords;
}

Bone->x and Bone->y are the coordinates of the end point of the Bone. The starting point is given by (bone->parent->x, bone->parent->y) or (0, 0). Each entity in the game has a Mesh, and Mesh->vertices is used as the bounding area for the entity. Mesh->tex_coords are texture coordinates. In the entity's update function, the position of the Bone is used to change the coordinates of the Vertices that are bound to it. Currently what I have is:

void Mesh_update(Mesh *mesh) {
    int i, j;
    double sx, sy;

    for (i = 0; i < vertex_count; i++) {
        if (mesh->vertices[i]->bone_count == 0) {
            continue;
        }

        sx, sy = 0;
        for (j = 0; j < mesh->vertices[i]->bone_count; j++) {
            sx += (/* ??? */) * mesh->vertices[i]->weights[j];
            sy += (/* ??? */) * mesh->vertices[i]->weights[j];
        }

        mesh->vertices[i]->x = sx;
        mesh->vertices[i]->y = sy;
    }
}

I think I have everything I need, I just don't know how to apply the transformations to the final mesh coordinates. What tranformations do I need here? Or is my approach just completely wrong?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I believe you would be looking to using transformation matrices here, though i think you need to elaborate upon your update function in order to accomodate for this. Ask yourself what transformations you need - translation, rotation?

        sx, sy = 0;
    for (j = 0; j < mesh->vertices[i]->bone_count; j++) {
        sx += (/* ??? */) * mesh->vertices[i]->weights[j];
        sy += (/* ??? */) * mesh->vertices[i]->weights[j];

I find sometimes putting something down in plain english helps. The above for e.g.:

For each bone, perform a (transformation) multiplied by the weights of the bone.

I would put more into

  • Are we rotating or translating this bone?
  • Does the parent constrain this
    movement?

Matrices are handy for this because they can work off of a structure like this in that you can work out the constraint from the parent bone's information and apply it to the child bone.

  • Perform transformation based on previous questions

This may be a completely wrong way about things, but certainly food for thought.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .