Question

I'm having trouble figuring out how to generate matrixes.

Hopefully that picture explains it, but basically I have an initial position, and I'm trying to rotate the main joint, 90 degrees, then following that, rotate the last joint, by 90 degrees. I then apply translation afterwards to get a final matrix (see code). That is applied to a set of points, that are relative to its joint.

The last rotation doesn't seem to work, it is ok if I don't put in the line: matrixPositions[2].appliedRotationMatrix *= (matrixRotX * matrixRotY * matrixRotZ); (the leg is straight down). I must be missing something obvious? Can you not do matrix multiplication this way for rotations?

enter image description here

D3DXMATRIX matrixRotX, matrixRotY, matrixRotZ;
D3DXMatrixRotationX(&matrixRotX, 0);
D3DXMatrixRotationY(&matrixRotY, 0);
D3DXMatrixRotationZ(&matrixRotZ, -PI/2);

matrixPositions[0].appliedRotationMatrix *= (matrixRotX * matrixRotY * matrixRotZ);

D3DXMATRIX matTranslationIn1;
D3DXMatrixTranslation(&matTranslationIn1, (matrixPositions[0].position.x-matrixPositions[1].position.x), (matrixPositions[0].position.y-matrixPositions[1].position.y), (matrixPositions[0].position.z-matrixPositions[1].position.z));

D3DXMATRIX matTranslationOut1;
D3DXMatrixTranslation(&matTranslationOut1, -(matrixPositions[0].position.x-matrixPositions[1].position.x), -(matrixPositions[0].position.y-matrixPositions[1].position.y), -(matrixPositions[0].position.z-matrixPositions[1].position.z));

matrixPositions[1].appliedRotationMatrix *= (matTranslationIn1 * (matrixRotX * matrixRotY * matrixRotZ) * matTranslationOut1);

D3DXMatrixTranslation(&matTranslationIn1, (matrixPositions[0].position.x-matrixPositions[2].position.x), (matrixPositions[0].position.y-matrixPositions[2].position.y), (matrixPositions[0].position.z-matrixPositions[2].position.z));

D3DXMatrixTranslation(&matTranslationOut1, -(matrixPositions[0].position.x-matrixPositions[2].position.x), -(matrixPositions[0].position.y-matrixPositions[2].position.y), -(matrixPositions[0].position.z-matrixPositions[2].position.z));

matrixPositions[2].appliedRotationMatrix *= (matTranslationIn1 * (matrixRotX * matrixRotY * matrixRotZ) * matTranslationOut1);
matrixPositions[2].appliedRotationMatrix *= (matrixRotX * matrixRotY * matrixRotZ);



D3DXMATRIX matrix[3];
for (int x = 0; x < 3; x++)
{
    D3DXMatrixIdentity( &matrix[x]);

    D3DXMATRIX matTranslation;
    D3DXMatrixTranslation(&matTranslation, matrixPositions[x].position.x, matrixPositions[x].position.y, matrixPositions[x].position.z);

    matrix[x] = matrix[x] * matrixPositions[x].appliedRotationMatrix * matTranslation;
}
Was it helpful?

Solution

There are two main steps for your requirements.

  1. Rotate joints 0, 1 and 2 around the origin by 90 degrees.
  2. Rotate joint 2 around joint 1 by 90 degrees.

I write some pseudo code, it almost done, but you still need some updates to use it. see comments in the code for details.

void Rotatation()
{
    // Build up the rotation matrix for step 1
    D3DXVECTOR3 rotAxis(0, 0, 1); 
    float angle = -(D3DX_PI / 2);
    D3DXMATRIX rotMatrix;
    D3DXMatrixRotationAxis(&rotMatrix, &rotAxis, angle);

    // rotate joints 0, 1 and 2 by apply the matrix above
    for (int i = 0; i < 3; i++)
    {
        joints[i].matrix *= rotMatrix;
    }

    // Build up the rotation matrix for joint 2
    // Since joint 2 was not rotate around the origin(I mean the axis should pass the origin), so first you need to translate the rotation center to origin
    // then rotate joint 2, and last move back

    // After the rotation in step 1, joint 1 now locate at (0, 2, 0)
    // to translate it to the origin.
    D3DXMATRIX transMat;
    D3DXMatrixTranslation(&transMat, 0, 2, 0);

    // Now joint 2 can rotate around z-axis, so the rotate matrix is same as step 1

    // after rotation, move back, this matrix is the inverse of transMat
    D3DXMATRIX inverseTransMat;
    D3DXMatrixTranslation(&transMat, 0, -2, 0);

    // Combine the 3 matrix above
    D3DXMATRIX rotMatjoin2 = transMat * rotMatjoin2 * inverseTransMat;

    // rotate jonit 2
    joints[2].matrix *= rotMatjoin2;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top