- Home /
Quaternion multiplication
hey guys, I'm trying to figure out the syntax (C#) and the proper way to multiply two quaternions into an equation like this:
[v1, s1][v2,s2] = [s1v2+s2v1+v1 (cross) x2, s1s2 - v1 (dot) v2]
where v1 = omega (w), s1 = 0, v2 = xyz of q (which is quaternion), s2 = w of q
here is my syntax in c# to compute this:
Quaternion quatprod = 0+q.w*w + (w*q), 0 - Vector3.dot(w,q);
q = q + ((Time.deltaTime) / 2)* quatprod;
This seems very wrong to me. Also confusing. Any light that anyone can shed on this??
Answer by gfoot · Feb 04, 2014 at 09:50 PM
It is simpler to construct a new quaternion for [v1, s1] and let Unity multiply them:
Quaternion qw0 = new Quaternion(w.x, w.y, w.z, 0);
Quaternion quatprod = qw0 * q;
qw0 is not normalized and I'm not sure how well Unity's Quaternion type caters for that. The reason I doubt this is that it lacks so many operators, such as addition, magnitude, and normalization. It smells like it only supports unit quaternions.
However, I tried it and it seems to work OK - it correctly stores qw0 and quatprod, without auto-normalizing them. So you might be OK.
The next step in your code will cause more problems as you need to multiply a quaternion by a scalar, and to add two quaternions together, neither of which is provided by Unity's Quaternion type. You can define them yourself though and it should work.
If on the other hand you do want to do all the maths yourself without using the Quaternion type, then you should be more explicit about breaking the input quaternion into a vector/scalar pair first:
Vector3 qV = new Vector3(q.x, q.y, q.z);
float qS = q.w;
Then calculate the result, as a vector/scalar pair:
Vector3 quatprodV = qS * w + Vector3.Cross(w, qV);
float quatprodS = -Vector3.Dot(w, qV);
Then integrate your (qV, qS) pair:
qV += (Time.deltaTime / 2) * quatprodV;
qS += (Time.deltaTime / 2) * quatprodS;
Although you didn't mention it, I think you would now normalize the quaternion.
float magnitude = Mathf.Sqrt(qV.sqrMagnitude + qS * qS);
qV /= magnitude;
qS /= magnitude;
And now you can repack q from (qV, qS):
q = new Quaternion(qV.x, qV.y, qV.z, qS);
I edited this to remove some of the doubt about Unity's support for non-unit Quaternions, as it does seem to support them fairly well in practice.
Thanks for your help. It has definitely shed some light on the quaternion object. I am wondering, however, where did you use the quatprod that you computed in the beginning in the end result?? is the full quatprod itself useless unless you break it up?
Thanks!!
Sorry if it was unclear - the first code block was showing how to do the multiplication using Unity's built-in Quaternion.operator*, i.e. by converting (w,0) into a Quaternion first. That block is in isolation, and you'd need to go on to do your "q = q + ..." line afterwards.
The rest of the code blocks are an alternative sequence showing how to do it the opposite way - by first splitting q into (qV, qS) and doing the maths at that level.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132543im_/https://answers.unity.com/themes/thub/images/avi.jpg)