0
\$\begingroup\$

I'm currently working on a multiplayer skydiving Unity game in which i rotate the players like this:

transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, delta);

Now this rotates the player relative to it's own rotation. I know rotation in world space is done by multiplying the quaternion of the desired positon with the quaternion of the current position like this:

localRotation = transform.rotation * desiredRotation
worldRotation = desiredRotation * transform.rotation

But how do i slerp to that position in world space?

Thank you all in advance and have a great day!

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

First you grab the total rotation quaternion.

fullRelativeRotation = Quaternion.inverse(transform.rotation)*desiredRotation;
rotationTime = 0;
//persist over frames

then every frame until you complete the rotation you slerp the identity quaternion with it.

if(rotationTime < 1){
    rotationThisFrame = Quaternion.Slerp(Quaternion.identity, fullRelativeRotation, min(delta, 1-rotationTime));
    rotationTime+=delta;
    transform.rotation = transform.rotation * rotationThisFrame;
}
\$\endgroup\$
1
  • \$\begingroup\$ Some remarks. 1: You can store the rotationThisFrame outside the if clause. It does not depend on anything that changes with time. 2: If, on the other hand, you'd used the rotationTime and stored the initial and final rotations, then recomputing the quat on the slerp path every frame would have been justified. But, in that case, you would have simply set transform.rotation = Quaternion.Slerp(..). \$\endgroup\$
    – teodron
    Sep 7, 2017 at 9:30
1
\$\begingroup\$

Usually, in order to get from a local coordinate frame to world coordinates, you need to go through the rotation hierarchy, like worldToParent.rotation * parentToLocal.rotation = worldToLocal.rotation.

So, in order to rotate towards a desired orientation in world space, your computation look something like transform.rotation = Quaternion.Slerp(worldToLocal.rotation, desiredRotation, delta). Note that desiredRotation in this case is not transformed, as it is a relative rotation anyways and will thus be applied within the same coordinate frame as the base rotation (in this case worldToLocal, defined as above, which resides in the world frame).

\$\endgroup\$
2
  • \$\begingroup\$ Maybe, to be safer/clearer, mention that the user must not tamper with the worldToLocal and desiredRotation amounts during the rotation sequence of frames. They are set prior to starting the rotation itself. \$\endgroup\$
    – teodron
    Sep 7, 2017 at 9:34
  • \$\begingroup\$ Also many thanks to you! Your solution also worked. \$\endgroup\$
    – xeetsh
    Sep 7, 2017 at 21:25

You must log in to answer this question.

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