Save current rotation and apply the rotation stored to mesh

I’m implementing a Undo button. I need to save the current rotation of a mesh and then on Undo apply the stored value.

I tried saving this

var objRot= mesh.rotationQuaternion;

Then on Undo I do this:

selectedObj.rotationQuaternion = new BABYLON.Quaternion(objRot.x, objRot.y, objRot.z, objRot.w);

I see some values, but the rotation results always in a 0,0,0,0 rotation.

Any ideas how to save correctly a mesh quaternion and apply it after?

Thanks

Hi. I would prefer to use rotation instead of quaternion.
Get the rotation from quaternion with

quaternion.toEulerAngles()

and on undo simply set mesh rotation, and set rotationQuaternion to null.
Or if you still want to try it with quaternions, then maybe this will help you
https://doc.babylonjs.com/features/position,_rotation,_scaling#rotationquaternion

2 Likes

In this PG https://www.babylonjs-playground.com/#3RTT8P#74

All three boxes are set to the same rotation using quaternions.

The top two are given further random rotations. Click on either of the top two followed by the update button and they are reset to their original rotation matching the lowest box.

2 Likes

rotationQuaternion is a JavaScript object. There are no value type structures in JavaScript and thus they don’t copy by value. I’m not exactly sure why you end up with all zeros (send a PG if you really want to find out), but you probably should create a new Quaternion for your “stored” backup value:

var objRot = new Quaternion();
objRot.copyFrom(mesh.rotationQuaternion);

Then, set it back by:

selectedObject.rotationQuaternion.copyFrom(objRot);

2 Likes

Thank you for your replies, I’ll work on your recommendations and let you know how it goes.

Cheers

I have the same question, can you share any new progress?

Are you having problems with what has been suggested? If so would you please provide a playground showing your problem.

Some time ago we created “Undo position” (origin topic). Might be useful for you https://www.babylonjs-playground.com/#35HAW1#29

Sorry, please forgive me,
I have solved it, please ignore

2 Likes