1
\$\begingroup\$

I just started looking into Stride as a replacement for Unity, and I'm trying to rewrite some scripts.

My question is: How do you set the world rotation of a transform component?

In other words, what would be Stride's equivalent of using transform.rotation = someQuaternion; in Unity?

I found some helpful info here, but it doesn't seem to mention how to set the world rotation directly. Just local rotation.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

In Unity, the .rotation property works something like this...

public Quaternion rotation {
   get {
       return parent.rotation * localRotation;
   }

   set {
       localRotation = Quaternion.Inverse(parent.rotation) * value;
   }
}

(eliding some complexity around "what if the object doesn't have a parent?" and options to cache data / use dirty flags to avoid redundant computation)

So to implement something similar in Stride, you can take the parent's world matrix and extract the [best-fitting] rotation as a quaternion. Invert that, then multiply by the net rotation you want to set. This will give you a quaternion representing the difference between the rotation you want and the parent's rotation. Multiply it on the correct side to ensure it's expressed relative to the parent coordinate space, not relative to the world. Then that quaternion product will be directly usable as a local rotation for your child object.

I note "best-fitting" because it's possible for a matrix to not be a simple combination of translation, rotation, and axis-aligned scale. Even if each individual object in a transform hierarchy is transformed in only these ways, the net result can be a shear, making the "correct" rotation component ambiguous — more than one rotation can give the same output, by changing the shear component to compensate. If you stick to uniform scales at non-leaf nodes though, you will not have problems with this.

\$\endgroup\$
1
  • \$\begingroup\$ The solution I presented above is high-level, since I don't have the hands-on experience with Stride to show working/tested code for a complete solution. If someone with more Stride experience wants to fill that gap with another answer, I'd happily upvote it! \$\endgroup\$
    – DMGregory
    Sep 16, 2023 at 21:48

You must log in to answer this question.

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