# Rotation hell - copying rotation from source mesh to parented target mesh

**URL:** <https://forum.babylonjs.com/t/rotation-hell-copying-rotation-from-source-mesh-to-parented-target-mesh/41183>\
**Category:** Questions\
**Created:** [May 27, 2023, 8:25am UTC](https://forum.babylonjs.com/t/rotation-hell-copying-rotation-from-source-mesh-to-parented-target-mesh/41183 "2023-05-27T08:25:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Kerr](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/joe_kerr/32/39017_2.png) [@Joe\_Kerr](https://forum.babylonjs.com/u/Joe_Kerr)\
**Post date:** [May 27, 2023, 8:25am UTC](https://forum.babylonjs.com/t/rotation-hell-copying-rotation-from-source-mesh-to-parented-target-mesh/41183/1 "2023-05-27T08:25:16Z")

</div>

Hi everyone,

#### Context

I have been working on this for two weeks or so now. Initially I just wanted to port a [Ragdoll](https://forum.babylonjs.com/t/small-lib-for-creating-ragdolls/14924/14) script to the new Havok engine. The biggest road block turned out to be the matching of rotations between physics colliders to bone TransformNodes of the skeleton (under a GLTF root node). This can be condensed down a bit further.  
Almost embarrassingly to the very simple problem of copying a rotation of one mesh to another one.

#### Code

Anyway, the first playground1 passes. Line 66 has the main function. Goal is to copy red to green:

[Test1](https://playground.babylonjs.com/#S64F8Y) ✅

Up to the delta part is where I would say I understand sort of why I did that. Then, according to the [docs](https://doc.babylonjs.com/features/featuresDeepDive/mesh/transforms/center_origin/rotation_conventions#yxz-local-axes-yaw-pitch-roll), if you rotate in order of ZXY then this is like rotating local angles by YXZ. Since the delta angles are in World space, I have to use the ZXY order. Anyway, a pass is a pass so next.

[Test2](https://playground.babylonjs.com/#S64F8Y#1) ❌

It is basically the first playground but has the source rotated again. Fails. At first I thought I double rotate something. But I already only rotate by the delta angles! 😵‍💫

[Test3](https://playground.babylonjs.com/#S64F8Y#2) ❌

There is just a single rotation but the parent node has a negative scaling. I know that negative scaling does some weird shit to rotations. Maybe there is some magic “offset” missing which would fix test2 and test3?

Well I am stuck now. I have two other completely different approaches (wildly applying offsets [got me the furthest actually, lol]; normalising with de-/re-parenting). But all fail at some point. I am out of ideas now 🥺

Best wishes  
Joe

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 28, 2023, 9:31pm UTC](https://forum.babylonjs.com/t/rotation-hell-copying-rotation-from-source-mesh-to-parented-target-mesh/41183/2 "2023-05-28T21:31:43Z")

</div>

Basically, if you want to apply the same transformation to a node D than the transformation of a node S, you need to set for D the transformation from S from which you “subtract” the transformation of the parent of D. To “subtract” a transformation means multiplying by the inverse matrix of this transformation, so you must calculate `Sw * Pw-1`, where `Sw` is the world matrix corresponding to S and `Pw-1` is the world matrix inverse of the parent of D:

> **[Babylon.js Playground](https://playground.babylonjs.com/#S64F8Y%233)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

As we only want to have the same orientation for the green mesh than for the red mesh, I update only the `rotationQuaternion` of S (line 66).

---

<div class="post-metadata">

**Author:** ![Joe\_Kerr](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/joe_kerr/32/39017_2.png) [@Joe\_Kerr](https://forum.babylonjs.com/u/Joe_Kerr)\
**Post date:** [June 1, 2023, 7:04am UTC](https://forum.babylonjs.com/t/rotation-hell-copying-rotation-from-source-mesh-to-parented-target-mesh/41183/3 "2023-06-01T07:04:48Z")

</div>

Yay, huge progress. Thanks @Evgeni_Popov😃
