SixDofDragBehavior is not updating the rotation value on the dragged object

I’m not sure if this is a bug or just a misunderstanding on my part, so I’ll post this as a question first.

I’m using the SixDofDragBehavior to move objects around a scene. After letting go of an object, I’d like to save the position, rotation, and scale of the object. Position and Scale are working as expected, but the rotation values are always set to 0, even when the object has visibly been rotated as part of the drag.

Please see the playground below. With the console open, click on and drag the green box. When you let go, it will console.log the current rotation, which for me is always 0.


The SixDofDragBehavior class is using the rotationQuaternion property to control the rotation of the node, rather than rotation. From the doc page:

Gets or sets the rotation Quaternion property : this a Quaternion object defining the node rotation by using a unit quaternion (undefined by default, but can be null).
If set, only the rotationQuaternion is then used to compute the node rotation (ie. node.rotation will be ignored)

So, the rotation property of the node is being ignored and is not used. If you output subject1’s rotationQuaternion, you will see the values changing :slight_smile: You may find the toEulerAngles and fromEulerAngles methods on Quaternion useful as well.

For the data storage part, I noticed that subject1data is not defined. Maybe that part is missing from the playground you posted?

Yeah, I thought I got rid of all of that, but I missed a bit.

Thanks for your reply :smiley:

So, I can use rotationQuaternion.toEulerAngles () to get a Vector that I can save. I just did a quick test and using this value on rotation when starting a scene seems to work ok. Is that safe to do, seeing how the SixDofDragBehavior will only use rotationQuaternion?

Do you think I should save and restore the values for rotationQuaternion instead?

The SixDofDragBehavior class has these lines inside _targetDragStart:

        if (!this._ownerNode.rotationQuaternion) {
            this._ownerNode.rotationQuaternion = Quaternion.RotationYawPitchRoll(this._ownerNode.rotation.y, this._ownerNode.rotation.x, this._ownerNode.rotation.z);

So, if you start dragging a mesh that does not have a rotationQuaternion set, it will automatically set it for you to be the equivalent of the mesh’s rotation values. This is why your solution works okay.

In my personal opinion, though, your code will be easier to maintain if you use rotationQuaternion for everything. I think it will be easier to reason about what you’re doing.

For example, if you try to adjust the rotation of the node via code after it’s been dragged, it won’t work, but it won’t be immediately obvious why.

Thanks! I’ll standardize on saving quaternions then.