libogc : quaternion multiplication problem (and solution)
Posted: Mon Sep 08, 2008 11:21 pm
Hello,
I think that I found a mistake in the libogc implementation of quaternions.
c_guQuatMultiply uses + instead of - several times.
Here is a working version of the function :
As a bonus, here is a fonction that converts a quaternion into a matrix :
Hope it will help someone.
PS : I'm currently working on a rubik's cube homebrew for Wii, which uses quaternions to rotate. The code is quite ugly at the moment, but if you need a ready-to-compile sample, feel free to ask for it.
I think that I found a mistake in the libogc implementation of quaternions.
c_guQuatMultiply uses + instead of - several times.
Here is a working version of the function :
Code: Select all
void my_guQuatMultiply(Quaternion *a,Quaternion *b,Quaternion *ab)
{
Quaternion *r;
Quaternion ab_tmp;
if(a==ab || b==ab) r = &ab_tmp;
else r = ab;
r->w = a->w*b->w - a->x*b->x - a->y*b->y - a->z*b->z;
r->x = a->w*b->x + a->x*b->w + a->y*b->z - a->z*b->y;
r->y = a->w*b->y + a->y*b->w + a->z*b->x - a->x*b->z;
r->z = a->w*b->z + a->z*b->w + a->x*b->y - a->y*b->x;
if(r==&ab_tmp) *ab = ab_tmp;
}
Code: Select all
void my_guQuatToMtx(Quaternion *qua, Mtx m)
{
m[0][0] = 1-2*qua->y*qua->y-2*qua->z*qua->z;
m[0][1] = 2*qua->x*qua->y-2*qua->w*qua->z;
m[0][2] = 2*qua->x*qua->z+2*qua->w*qua->y;
m[0][3] = 0;
m[1][0] = 2*qua->x*qua->y+2*qua->w*qua->z;
m[1][1] = 1-2*qua->x*qua->x-2*qua->z*qua->z;
m[1][2] = 2*qua->y*qua->z-2*qua->w*qua->x;
m[1][3] = 0;
m[2][0] = 2*qua->x*qua->z-2*qua->w*qua->y;
m[2][1] = 2*qua->y*qua->z+2*qua->w*qua->x;
m[2][2] = 1-2*qua->x*qua->x-2*qua->y*qua->y;
m[2][3] = 0;
}
PS : I'm currently working on a rubik's cube homebrew for Wii, which uses quaternions to rotate. The code is quite ugly at the moment, but if you need a ready-to-compile sample, feel free to ask for it.