0
\$\begingroup\$

I would like to solve this with quaternions:

A - B = C

for(int i = 0; i < 10 ; i ++){

  B += C/10

};

I want to get the difference (C) between two quaternions (A & B), and then add the difference to the subtrahend (B) multiple times.

Is there a way to do this? Or do I need to convert to eulerAngles?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Quaternion difference:

 C = Quaternion.Inverse(B) * A;

Quaternion division by a scalar:

 C_fraction = Quaternion.Slerp(Quaternion.identity, C, 1f/steps);

Quaternion increment:

 for (int i = 0; i < steps; i++) {
     B = B * C_fraction;
 }

Though a more idiomatic way to write this would be...

 for (int i = 0; i < steps; i++) {
     float progress = (i + 1) / (float)steps;
     interpolated = Quaternion.Slerp(start, end, progress);
 }

If you don't need every step to be exactly the same, Lerp also works in place of Slerp, but is a little cheaper. It takes smaller steps at the beginning and end of the interpolation and bigger steps in the middle.

Note that order matters, and expresses sequence of rotations, or which rotations should be performed relative to the parent/world axes (terms on the left) or using the object's local axes (terms on the right right).

Writing C as I did above, it's a local space rotation that needs to be multiplied on the right to have its intended effect: B * C == B * Inverse(B) * A == A.

Flipping the order of multiplication to C * B will not, in general, give me A. To get a difference for left-multiplication like that (applying C in parent/world space), I'd need to define C with A on the left too: C = A * Inverse(B)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .