[AD] matrix_to_quat and get_align_matrix

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


matrix_to_quat often produces wrong results. E.g. this testcase:

#include <allegro.h>
#include <time.h>

void
print_matrix (MATRIX_f *m)
{
    printf ("M| % 2.2f % 2.2f % 2.2f |\n",
        m->v[0][0], m->v[0][1], m->v[0][2]);
    printf (" | % 2.2f % 2.2f % 2.2f |\n",
        m->v[1][0], m->v[1][1], m->v[1][2]);
    printf (" | % 2.2f % 2.2f % 2.2f |\n",
        m->v[2][0], m->v[2][1], m->v[2][2]);
}

void
print_quat (QUAT *q)
{
     printf ("Q| % 2.2f % 2.2f % 2.2f % 2.2f |\n",
        q->x, q->y, q->z, q->w);
}

int
main (void)
{
    MATRIX_f m;
    QUAT q;
    int x, y, z;
    allegro_init ();
    srand (time (NULL));
    x = 128 * (rand() & 1);
    y = 128 * (rand() & 1);
    z = 128 * (rand() & 1);
    printf ("%d %d %d\n", x, y, z);
    get_rotation_matrix_f (&m, x, y, z);
    matrix_to_quat (&m, &q);
    print_matrix (&m);
    print_quat (&q);
}

Will always result in an errorneous quaternion (it says 1.37 instead of
1.0 for one of the components, and all subsequent operations with it
will fail horribly.. or at least be off quite a bit).

I know absolutely zero about quaternion maths, but I found this code in
google (the one that says C++ on the page):

http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/

And replaced the old one. Now it works always correctly. If someone here
has code to do this as well, it would probably better to use that
instead of stealing code found with google as I did :P Or maybe someone
who understands the code could see why the old one was wrong and just
fix the problem.

I also fixed two bugs in get_align_matrix. The first was a mistake with
the normalization (normalizing the non-perpendicular up vector in the
beginning is useless - and the resulting right vector was not
normalized). And, this is really nasty, the two vectors in the
cross_product calls are swapped. Actually, this last bug would have
caused any matrix produced by get_align_matrix to be completely bogus.
So, since it's quite late already.. I may be wrong with this. Did anyone
ever use get_align_matrix? If yes, we probably can't change this. And
then I must be wrong as well.. because the resulting matrix shouldn't
have made much sense (one of the vectors was flipped). But I'm probably
the least knowledgeable on 3D math here.. someone else should verify
this.

-- 
Elias Pschernig





Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/