Reputation: 9049
I currently can rotate around a pivot point by first translating to the pivot point then performing the rotation and finally translating back to the origin. I do that easily enough for the shoulder in my example. However I cannot figure out how to also add in a rotation around the elbow for the forearm.
I've tried the following for the forearm rotation around the elbow:
Neither work for me. Any suggestions? I'm really stuck on this one.
Upvotes: 2
Views: 628
Reputation: 4677
I ran into a similar problem when I was doing some skeletal animation. It's very helpful to use recursion for this. Also, structure your bones hierarchically (e.g. shoulder is a parent of forearm which is a parent of hand, etc.). By doing that you can write your code as follows:
void drawBone(Bone *root) {
if (!root) return;
glPushMatrix();
glTranslatef(root->x,root->y,0);
glRotatef(root->a,0,0,1);
// insert code to actually draw bone here
int i;
glTranslatef(root->l,0,0);
for (i=0; i<root->childCount; i++)
drawBone(root->child[i]);
glPopMatrix();
}
My bone struct looked like this:
typedef struct _Bone {
float x,y,a,l; // position, angle, length of bone
int childCound; // number of children for this bone
struct _Bone *child[MAX_CHILD_COUNT], *parent;
} Bone;
This website is a great resource for skeletal animation.
Upvotes: 3
Reputation: 13541
The second approach should work. I don't really know your model for the human body. Some similar task was a first assignment in a computer vision course I took.
A helpful thing is to use a scene-graph to build parts that share a common local coordinate system. Then you can traverse it and rotate correctly. It is helpful not to translate but to use glPushMatrix() and glPopMatrix()
In this way you can think in a local coordinate system and affect all other elements.
Upvotes: 0