- Home /
Multiply a Quaternion?
I want to do the equivalent of:
v = Vector3( a, b, c ) * d;
... but for Quaternions. If d<1, I know I can do it with Quaternion.Slerp:
q = Quaternion.Euler( a, b, c );
q2 = Quaternion.Slerp( Quaternion.identity, q, d );
... but if d>1, I can't use that. The only way I can think of at the moment is to multiply the quaternion by itself many times (and then add a slerp for the remaining fraction):
q = Quaternion.Euler( a, b, c );
q2 = Quaternion.identity;
dInt = Mathf.Floor(d);
dRemainder = d - dInt;
for (var a=0;a<dInt;a++) {
q2 *= q;
}
q2 *= Quaternion.Slerp( Quaternion.identity, q, dRemainder );
But if d==508.4 or something, will that hog a lot of time doing 508 quaternion multiplications and a slerp? I will be doing this every frame (or in FixedUpdate). Is there a simpler way?
What is it you are trying to do? $$anonymous$$ultiplying a vector simply increases the magnitude of the vector without changing its direction/rotation. Quaternions don't have magnitudes (per se), and therefore multiplying them by a number won't accomplish anything. What are you trying to use this for?
I meant a vector as in euler angles... but multiplying euler angles presents me with gimbal lock problems.
Answer by Mike 3 · Jan 14, 2011 at 03:43 PM
You could probably use ToAngleAxis, multiply the angle, then shove both back in using AngleAxis. Why not just do Quaternion.Euler( new Vector3(a, b, c) * d); instead though?
I would do it with AxisAngle as well (and that's what I did for the Locomotion System). The second suggestion of multiplying the Euler angles by d won't work, as Euler angles work in a different space.
As I understand Angle-Axis (which might well be not very well!) wouldn't that just result in a body that spins round and round one particular axis, if the axis doesn't change?
For instance, using this method and multiplying by 2 - would that result in exactly the same as multiplying any Quaternion by itself? Or would the result be along the same axis, but rotated around it by twice the angle?
It would do what conceptually quaternion*angle would do. e.g. a 90 degree rotation around y would result in a 180 one if multiplied by two, etc
Answer by Paulius-Liekis · Jan 14, 2011 at 03:46 PM
As I understand you want to rotate by quaternion d times, right? Then use:
Quaternion.ToAngleAxis (out angle : float, out axis : Vector3) : void
It will give you angle which you can multiply by d and then construct quaternion from angle and axis again.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095439im_/https://answers.unity.com/themes/thub/images/avi.jpg)