# Multiple Issues with Pivot-Setting Playground

**URL:** <https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417>\
**Category:** Questions\
**Created:** [December 24, 2018, 12:08pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417 "2018-12-24T12:08:00Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wingnut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wingnut/32/53_2.png) [@Wingnut](https://forum.babylonjs.com/u/Wingnut)\
**Post date:** [December 24, 2018, 12:08pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/1 "2018-12-24T12:08:00Z")

</div>

Hi gang.

I’m having problems with a test playground that periodically sets pivots to random corners of a box. (and then does a random-ish 90-degree spin around that new pivot)

[https://www.babylonjs-playground.com/#195B2X#10](https://www.babylonjs-playground.com/#195B2X#10)

I am still studying [pivot docs](https://doc.babylonjs.com/how_to/pivots), but I can’t find the correct way to prevent box from changing .position.

I think the ‘true’ in the last param of line 48… SHOULD keep the mesh from re-positioning after each pivotMatrix setting, but it is being grumpy for me. It still re-positions the box, sometimes/often.

Help welcome. Thx!

* * *
2nd issue: Also, I am experiencing console.logs that won't shut-off. For example, the above playground has two console.logs at lines 39/40. You can watch console and see them execute.

Now remark them out (disable them), or remove them from the code. Click RUN. Are they still active, outputting text to console? They are doing so, for me, in Firefox & IE. Seems broken, but could be Wingnut screw-up… as always. 🙂 Thx for info/help.

Update: Issue #2 solved… needed to clearInterval() in scene.onDispose. [link](https://www.babylonjs-playground.com/#195B2X#11) “intervals” are SO strange. Ghost-like “forks”… flying around inside the playground walls. “timeouts” are wiser, and can be config’d to act like intervals.

---

<div class="post-metadata">

**Author:** ![Wingnut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wingnut/32/53_2.png) [@Wingnut](https://forum.babylonjs.com/u/Wingnut)\
**Post date:** [December 24, 2018, 2:22pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/3 "2018-12-24T14:22:17Z")

</div>

[https://www.babylonjs-playground.com/#195B2X#17](https://www.babylonjs-playground.com/#195B2X#17)

New playground, trying two newly-web-found functions… that are “repeating setTimeouts”. BOTH seem to need scene.onDispose clear-out… to make line 39/40 logs… stop displaying (after once activated). Weird.

Timeouts and intervals… just strange. 🙂

“Larry had an in-ter-val, interval, interval”… etc.  
“It followed him past RUN one day, RUN one day, RUN one day”… etc.

heh. (parody of “Mary Had a Little Lamb”)

* * *
Meantime, primary issue still exists... the 'true' in line 50 appears to be... umm... not working. hmm.

[Link to api docs for setPivotMatrix](https://doc.babylonjs.com/api/classes/babylon.abstractmesh#setpivotmatrix)

```auto
param: postMultiplyPivotMatrix: boolean

defines if the pivot matrix must be cancelled in the world matrix. When this 
parameter is set to true (default), the inverse of the pivot matrix is also 
applied at the end to cancel the transformation effect.

```

Translation to noobian: Mesh position-change is nulled/cancelled. 🙂

---

<div class="post-metadata">

**Author:** ![logunov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/logunov/32/191_2.png) [@logunov](https://forum.babylonjs.com/u/logunov)\
**Post date:** [December 24, 2018, 2:52pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/4 "2018-12-24T14:52:25Z")

</div>

Hi, @Wingnut, I am not sure aboout my understood of your problem, but if I correct, you are trying to rotate mesh around itself, that means you should use local coordinates.  
As I remember, with pivot matrix, default coordinate system is world. If you want to change this, you should do manually recompute to local coordinate system, and apply handmade-recomputed matrix as argument to the methods.

I will try to find additional info about this, because I have some aftersound in my mind about this mathematical transformations, but for now I can’t formalize this to formula.

---

<div class="post-metadata">

**Author:** ![Wingnut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wingnut/32/53_2.png) [@Wingnut](https://forum.babylonjs.com/u/Wingnut)\
**Post date:** [December 24, 2018, 3:16pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/5 "2018-12-24T15:16:46Z")

</div>

Thanks for reply, Logunov! You might be right… I need to study that.

Yes, I am rotating the mesh, but not really “around itself”, but “around it’s pivot point”. But, mesh “origin” and mesh pivotPoint… are often deemed identical, so you are “technically” correct. The mesh “origin” is indeed moving when the pivotPoint moves. At least I THINK so.

[https://www.babylonjs-playground.com/#195B2X#21](https://www.babylonjs-playground.com/#195B2X#21)

In THAT playground, I disabled the setPivot **Matrix** () call, and instead used setPivot **Point** () in lines 49-53.

SetPivotPoint() allows me to set a “space”… either LOCAL or WORLD… in line 53.

Setting LOCAL or WORLD… doesn’t fix it, but, I’m not sure it is doing the same action… as what YOU describe. I will study… thanks again.

Earlier, I thought that each time I moved the pivot/origin, all future getBoundingInfo().boundingBox.extends\*… were returning bad values. This is why I thought about box1.refreshBoundingInfo(). (line 77)

But, after console.log checks, those numbers looked good… always being 1 or -1 (without using the refresh).

The “bake” in line 76 is also related… but using that… screws-up the scene even MORE. 🙂 I also experimented with similar lines @ 27-29. No joy.

---

<div class="post-metadata">

**Author:** ![logunov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/logunov/32/191_2.png) [@logunov](https://forum.babylonjs.com/u/logunov)\
**Post date:** [December 24, 2018, 3:55pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/6 "2018-12-24T15:55:37Z")

</div>

![3](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/1X/2cbb0d4c0b2d51311835cc0a19f2c8372ce763a7.jpeg)  
@Wingnut, let me share a little of my awesome art skill.

So, all I want to say, try to draw point of rotation to figure out where it is, and try manually calculate, what are you doing with it, before apply this though BABYLON, sometimes it helps me understand where problem in the input data, before trying to figure out where is exactly mathematical error in my computations.

As I can see, here is a big chance of wrong rotation pivot. It can be in local coordinates, but out of object volume, so it will provide movement in world space, on the other hand, as closer pivot to the center of meshes, as lower global movement will be (up to zero).

---

<div class="post-metadata">

**Author:** ![logunov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/logunov/32/191_2.png) [@logunov](https://forum.babylonjs.com/u/logunov)\
**Post date:** [December 24, 2018, 4:07pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/7 "2018-12-24T16:07:47Z")

</div>

To be more clear, I mean: you can rotate around one point and all will be fine, but you using local coordinates, and next point get displacement from previous rotation, and rotate around new point provide new additional displacement in world coordinates.

---

<div class="post-metadata">

**Author:** ![Wingnut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wingnut/32/53_2.png) [@Wingnut](https://forum.babylonjs.com/u/Wingnut)\
**Post date:** [December 24, 2018, 4:16pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/8 "2018-12-24T16:16:27Z")

</div>

> [@logunov](#):
>
> To be more clear, I mean: you can rotate around one point and all will be fine, but you using local coordinates, and next point get displacement from previous rotation, and rotate around new point provide new additional displacement in world coordinates.

Yes, but according to…

> [@Wingnut](#):
>
> param: postMultiplyPivotMatrix: boolean defines if the pivot matrix must be cancelled in the world matrix. When this parameter is set to true (default), the inverse of the pivot matrix is also applied at the end to cancel the transformation effect.

… setting that param TRUE _should_ compensate-for any worldSpace position-changes that the mesh tries to make. It applies the inverse of any world matrix changes that happened to the mesh… during the setPivotMatrix(). I don’t think it is working.

I think you are correct about ALL you have said, but our “compensator” parameter SHOULD counteract that unwanted transform issue. At least I THINK so. 🙂 Thanks for your input.

You MIGHT be implying that it is my spin() function that is messing-with the compensator. It does a worldSpace transform when it spins. hmm. That’s why I was trying a “bake” after the spin. hmm.

---

<div class="post-metadata">

**Author:** ![logunov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/logunov/32/191_2.png) [@logunov](https://forum.babylonjs.com/u/logunov)\
**Post date:** [December 24, 2018, 4:30pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/9 "2018-12-24T16:30:32Z")

</div>

Before posting, I took a look on your code and make assumption.  
I modify a little your playground: [https://www.babylonjs-playground.com/#195B2X#22](https://www.babylonjs-playground.com/#195B2X#22)  
Take a look (wait for 10 or more calls of your functions to see previous pivots, I think it will help you figure out what’s going on better).

For clarifying: is commenting line 81 provide expected result?

---

<div class="post-metadata">

**Author:** ![Wingnut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wingnut/32/53_2.png) [@Wingnut](https://forum.babylonjs.com/u/Wingnut)\
**Post date:** [December 24, 2018, 4:35pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/11 "2018-12-24T16:35:12Z")

</div>

> [@logunov](#):
>
> For clarifying: is commenting line 81 provide expected result?

No. It just disables the “set pivot to a random box corner” operation.

Objective… no mesh position jumps, no matter which corner of the mesh has the pivot point. Only “spin to new position” allowed, no jumps.

---

<div class="post-metadata">

**Author:** ![logunov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/logunov/32/191_2.png) [@logunov](https://forum.babylonjs.com/u/logunov)\
**Post date:** [December 24, 2018, 4:42pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/12 "2018-12-24T16:42:45Z")

</div>

Language barrier is so hard \>\_\<  
I need one more question: you need to make cube rotate around itself, in other words it will provide rotation around local point and accumulate rotations in some direction, or would you like always cube stay in the center and do new rotation?

---

<div class="post-metadata">

**Author:** ![Wingnut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wingnut/32/53_2.png) [@Wingnut](https://forum.babylonjs.com/u/Wingnut)\
**Post date:** [December 24, 2018, 4:46pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/13 "2018-12-24T16:46:01Z")

</div>

I agree (language). Also, I’m not very smart, so, there’s a problem with _that_, too. 🙂

Um, no, the mesh should “crawl around” on the screen… in random directions. The axis that spin() is using… is also random… either x, y, or z. So, mesh should move-around within scene… somewhat. But always SPINNING to the new position, never jumping.

I hope I explained that decently. 🙂 I guess it could be called “Slinky-moving”. Butt-end keeps rotating over head-end… to move the Slinky (down a stairs, for example).

---

<div class="post-metadata">

**Author:** ![logunov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/logunov/32/191_2.png) [@logunov](https://forum.babylonjs.com/u/logunov)\
**Post date:** [December 24, 2018, 5:04pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/14 "2018-12-24T17:04:44Z")

</div>

I figured out what is the problem. It is not in the pivot matrix or rotation errors.  
You are tacking bounding info as new data parameter, but bounding info is **always static** , it **do not change it global position** , so, when you applying your pivot data from the matrix, your **bounding info** do not changes, so, you uses old vertex data from mesh, and it’s rolling back to previous state.  
[https://www.babylonjs-playground.com/#195B2X#24](https://www.babylonjs-playground.com/#195B2X#24)  
Take a look in console logs: if mesh data changes, there should be different numbers, but they are always the same. Bounding info returns always same data, so the agruments of matrix are always same, and it do same rotations.

For now I think so, I will take a deeper look right now, maybe I am wrong with it, but I am pretty sure not.

I will do some tests now, to figure out how to fix it.

---

<div class="post-metadata">

**Author:** ![Wingnut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wingnut/32/53_2.png) [@Wingnut](https://forum.babylonjs.com/u/Wingnut)\
**Post date:** [December 24, 2018, 5:26pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/15 "2018-12-24T17:26:05Z")

</div>

I think boundingInfo NEEDS to stay static. I only use it to get half-sizes OF the box. Since the box doesn’t change size, all extends\* should be 1 or -1, for a size 2 box.

* * *
I found another fun "bake"... 

`box1.bakeTransformIntoVertices(box1.getWorldMatrix());`

Hmm… bake (make default) any matrix-transform… into/onto a mesh. hmm. Just another magic code-line to confuse Wingnut. heh

Maybe, after the spin… bake the PRE-spin matrix transformation… into the newly-spinned mesh.

Nah, that won’t work. (Wingnut feels-around in the dark) 🙂

```auto
        mesh.setPivotMatrix(
            BABYLON.Matrix.Translation(
                rand(xlist), rand(ylist), rand(zlist)
            ),
            true
        );

```

Still… I think that _true_ is broken, and would make all my dreams come _true_, if it worked. Maybe.

---

<div class="post-metadata">

**Author:** ![logunov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/logunov/32/191_2.png) [@logunov](https://forum.babylonjs.com/u/logunov)\
**Post date:** [December 24, 2018, 7:16pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/16 "2018-12-24T19:16:23Z")

</div>

Well, I stuck a little in that, maybe someone else will be better to help you.  
I got same result using leading vectors as method, maybe use of animation with loop mode as constant and loop parameter as false, but I can’t figure out what should rotation axis be.  
[https://www.babylonjs-playground.com/#195B2X#28](https://www.babylonjs-playground.com/#195B2X#28)

---

<div class="post-metadata">

**Author:** ![Rah](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/rah/32/201_2.png) [@Rah](https://forum.babylonjs.com/u/Rah)\
**Post date:** [December 24, 2018, 8:59pm UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/17 "2018-12-24T20:59:26Z")

</div>

i dont know if thats true. Even if you werent smart you try really hard, and that is more important than just being smart.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [December 26, 2018, 10:59am UTC](https://forum.babylonjs.com/t/multiple-issues-with-pivot-setting-playground/417/18 "2018-12-26T10:59:56Z")

</div>

You re dead smart @Wingnut cause you keep learning and sharing 🙂
