In an android game I want to draw a running leg. To output the thigh I do something like:
// legCX,legCY is the location on screen about which the leg rotates. Matrix m = new Matrix(); m.postTranslate(-legCX,-legCY); m.postRotate(legRot); m.postTranslate(legCX,legCY);
So, the above translates the 0,0 point over to the point of rotation about which I want to rotate the leg, then it rotates by the amount I want the leg rotated, then it translates 0,0 back to the original origin.
I then use that matrix to draw the thigh. It ends up rotated as expected. So far so good.
How do I draw the second part of the leg below the knee? It rotates differently than the part of the leg above it and has a center point which moves with the leg above it. I tried the following, but it turns out that the end result is rotation around some single point which doesn't follow the thigh.
Matrix m = new Matrix(); m.postTranslate(-legCX,-legCY); m.postRotate(legRot); m.postTranslate(0,-thighLength); m.postRotate(shinRot); m.postTranslate(0,thighLength); m.postTranslate(legCX,legCY);
I suspect that it's probably necessary to do the two rotations in two different Matrix objects and then combine them somehow, but I can't figure out how exactly to do that.
I've tried reading the various wikipedia articles on matrix operations, but I'm having trouble finding the sort of operation that I'm trying to perform.
EDIT: This type of matrix seems to be called a "transformation matrix". Combining multiple operations is called composition of transformations. However, none of the pages on this topic mention how to do a series of translations and rotations.
Surely, if you can use a matrix to do rotation about one point, it must be possible to do multiple matrix operations somehow to allow rotation about one point and then an additional rotation around a different point.
I've tried looking at pages on skeletal animation, but I can't make head nor tail of what they're talking about. My game won't involve many characters, so I don't need to use a physics engine to do super complex stuff. I just want to do the calculations to draw a character that runs.
EDIT: Hooray! Thanks dkantowitz! Your answer wasn't quite right, but you got me really close. I've edited your answer to be correct now. The second matrix needed to translate from the origin, not relative to the leg's center of rotation. And, the combined matrix needs to preConcat the shin matrix first and then the thigh's matrix. Thanks!