copengltrigonometrymodel-viewrotational-matrices

Rotation Matrix Shrinks Objects?


Is my math wrong? The user is supposed to be able to input an angle in degrees, and it rotate the matrix respectively. Instead, it shrinks the object and flips it... calling

glmxRotate(&modelview, 0.0f, 0.0f, 1.0f, 90.0f);

(with modelview being an identity matrix) yields:

Regular: https://i.sstatic.net/sSgi8.png

Rotated: https://i.sstatic.net/xTPvq.png

Here's glmxRotate:

glmxvoid glmxRotate(glmxMatrix* matrix, glmxfloat x, glmxfloat y, glmxfloat z,
    glmxfloat angle)
{
    if(matrix -> mx_size != 4){GLMX_ERROR = GLMX_NOT_4X4; return;}

    //convert to rads
    angle *= 180.0f / 3.14159;

    const glmxfloat len = sqrtf((x * x) + (y * y) + (z * z)),
                    c = cosf(angle),
                    c1 = 1.0f - c,
                    s = sinf(angle);

    //normalize vector
    x /= len;
    y /= len;
    z /= len;

    glmxfloat rot_mx[] = {x * x * c1 + c,
                          x * y * c1 + z * s,
                          x * z * c1 - y * s,
                          0.0f,

                          x * y * c1 - z * s,
                          y * y * c1 + c,
                          y * z * c1 + x * s,
                          0.0f,

                          x * z * c1 + y * s,
                          y * z * c1 - x * s,
                          z * z * c1 + c,
                          0.0f,

                          0.0f,
                          0.0f,
                          0.0f,
                          1.0f,};

    _glmxMultiMatrixArray(matrix, rot_mx, 4);
}

Also, if a translation matrix is defined with the translation in the last four column, how would one go about translating an identity matrix, because the outcome would always yield 0s?


Solution

  • Your matrix looks correct to me, though are you aware that your angle to rads multiplication is actually a radians to angle multiplication?

    //convert to rads
    angle *= 180.0f / 3.14159;
    

    Should be Pi/180.f.