7

I have two quaternions, as example:

    w     x     y     z
1:  0.98  0.08  0.17  -0.01
2:  0.70  0.70  0.0   0.0

I need to multiply them, to get third one, with all rotations in it, but have no ideas. It would be perfect, if there is a function in PHP / C++ / PAWN to do such a thing.

I searched about it a lot, but found almost nothing for me to understand.

Thanks for answers.

therainycat
  • 324
  • 1
  • 2
  • 14

3 Answers3

7

You should choose a language. In C++, the Boost.Math library includes quaternions; I don't know about the other languages you mention. Or for simple multiplication, you could just use the multiplication table (which I copied from Wikipedia):

*| 1  i  j  k
-------------
1| 1  i  j  k
i| i -1  k -j
j| j -k -1  i
k| k  j -i -1

For example, i*j gives the value in row i and column j, which is k.

So, assuming your quaternions represent w*1 + x*i + y*j + z*k, multiplication would be something like

quaternion operator*(quaternion a, quaternion b) {
    return {
        a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,  // 1
        a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,  // i
        a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,  // j
        a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w   // k
    };
}

(NOTE: that's untested, and probably riddled with typos).

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

Ok, so after wiki.

I would make something like:

class Quaternion{
double w,x,y,z;

public:
 Quaternion(double w, double x, double y, double z) : w(w), x(x), y(y), z(z) {};

 operator*(const Quaternion& rhs){
   double _w, _x, _y, _z;
   //compute new values
   _w = w*rhs.w - x*rhs.x - y*rhs.y - z*rhs.z;
   _y = /* after wiki */;
   _x = /* after wiki */;
   _z = /* after wiki */;

   //update values
   w = _w; x = _x; y = _y; z = _z; 
 }
}

I.e. make an object with 4 real numbers, write an operator to calculate new coefficients.

luk32
  • 15,812
  • 38
  • 62
0

Expressing the imaginary xi+yj+zk as a vector, the multiplication of Qc{Wc,Vc} = Qa{Wa,Va} * Qb{Wb,Vb} is as follows: Qc{Wc,Vc} = {WaWb + WaVb + WbVa + VaVb}.

From VaVb = -(Va dot Vb) + (Va cross Vb) [the former is a scaler and the latter is a vector], then grouping scalers and vectors: Qc{Wc,Vc} = {WaWb + -(Va dot Vb), WaVb + WbVa + (Va cross Vb)}.