# How To Mirror Animation Key Frames

**URL:** <https://forum.babylonjs.com/t/how-to-mirror-animation-key-frames/12343>\
**Category:** Questions\
**Created:** [July 9, 2020, 8:29am UTC](https://forum.babylonjs.com/t/how-to-mirror-animation-key-frames/12343 "2020-07-09T08:29:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [July 9, 2020, 8:29am UTC](https://forum.babylonjs.com/t/how-to-mirror-animation-key-frames/12343/1 "2020-07-09T08:29:36Z")

</div>

How do i **MIRROR** a keyframe animation key.

I tried flipping like this

```auto
Position.x *= -1

```

AND

```auto
QuaternionRotation.x *= -1
QuaternionRotation.w *= -1

```

But its not quite working ☹

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [July 9, 2020, 1:53pm UTC](https://forum.babylonjs.com/t/how-to-mirror-animation-key-frames/12343/2 "2020-07-09T13:53:16Z")

</div>

Weird. This looks good to me. Can you share a PG?

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [July 9, 2020, 7:52pm UTC](https://forum.babylonjs.com/t/how-to-mirror-animation-key-frames/12343/3 "2020-07-09T19:52:12Z")

</div>

I cant really put on playground yet. So i made a video trying to show what i am doing:

[Test Mirror Video](http://mackey.cloud/files/videos/testmirror.mp4)

---

<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:** [July 11, 2020, 1:15pm UTC](https://forum.babylonjs.com/t/how-to-mirror-animation-key-frames/12343/4 "2020-07-11T13:15:31Z")

</div>

For the quaternion, try to negate the y and z components instead of the x / w.

[EDIT]  
A quaternion is a rotation around an axis ((ax, ay, az) is the axis, angle is the angle of ration):

```javascript
qx = ax * sin(angle/2)
qy = ay * sin(angle/2)
qz = az * sin(angle/2)
qw = cos(angle/2)

```

Now you want to mirror against the X plane, I think you need to negate the angle and the axis x component:

```javascript
q2x = -ax * sin(-angle/2) = qx
q2y = ay * sin(-angle/2) = -qy
q2z = az * sin(-angle/2) = -qz
q2w = cos(-angle/2) = qw

```

So the y and z components should be negated.  
[/EDIT]

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [July 11, 2020, 4:46pm UTC](https://forum.babylonjs.com/t/how-to-mirror-animation-key-frames/12343/5 "2020-07-11T16:46:58Z")

</div>

I tried the Y and the Z

```auto
public static SampleAnimationQuaternion(animation:BABYLON.Animation, frame: number, mirror:boolean, loopMode:number, result:BABYLON.Quaternion): void {
    if (animation != null && animation.dataType === BABYLON.Animation.ANIMATIONTYPE_QUATERNION) {
        const keys:BABYLON.IAnimationKey[] = animation.getKeys();
        if (frame < keys[0].frame) {
            frame = keys[0].frame;
        } else if (frame > keys[keys.length - 1].frame) {
            frame = keys[keys.length - 1].frame;
        }
        BABYLON.Utilities.FastQuaternionInterpolate(animation, frame, loopMode, result);
        if (mirror === true) {
            //result.x *= -1;
            result.y *= -1;
            result.z *= -1;
            //result.w *= -1;
        }
    }
}

```

Still looks funny with the arms

 ![Screen Shot 2020-07-11 at 6.45.34 AM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/f/f59513c31636de8bbcc997a71367e2f1de47efaf.jpeg)

☹

---

<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:** [July 11, 2020, 5:34pm UTC](https://forum.babylonjs.com/t/how-to-mirror-animation-key-frames/12343/6 "2020-07-11T17:34:43Z")

</div>

I wonder why you have problems with the harms and not the legs / torso / head…

Also, did you change the side orientation of the materials when mirroring? You should set it to clock-wise (I think) as the winding of the vertices does change under mirroring.

---

<div class="post-metadata">

**Author:** ![J.C](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/j.c/32/6574_2.png) [@J.C](https://forum.babylonjs.com/u/J.C)\
**Post date:** [July 13, 2020, 3:08am UTC](https://forum.babylonjs.com/t/how-to-mirror-animation-key-frames/12343/7 "2020-07-13T03:08:17Z")

</div>

not familiar with the source code but I think the scaling should be applied after the other operations e.g. rotating for the bone matrix?  
and, the arm bones are nested, so is there a mechanism in BJS that we need to update the child bones as well when we modify the parent bone?
