Pregunta

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: http://i.imgur.com/eX7Td.png

Rotated: http://i.imgur.com/YnMEn.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?

¿Fue útil?

Solución

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.

Otros consejos

as there is a limited amount of space that the float is stored in the sin and cos are not exactly calculated this means that small errors happen every time the object is rotated this means that over time the object will get smaller. if you want this to not happen use Quaternions for rotations. https://www.npmjs.com/package/quaternion

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top