How to properly remove observer

Hello everyone.
I’d like to remove gizmo observers when specific mesh is clicked. For example, I want to put gizmo on polygon and move it with every vertices (spheres), but now onDrag always rerender my polygon so when none of the vertice is moving it’s gonna rerender polygon in the same place. I though that onDragObservable.remove(createPolygonDragbehaviour) should solve my problem but it doesn’t… Any suggestion will be helpful, thanks.

lines 151-163 represents my probem
https://playground.babylonjs.com/#QNJLCY#30

video:

Pinging @Cedric

Not sure to understand. I guess what you want if to be able to move the polygon and all control spheres when the gizmo is attached to the polygon?

Yes, that’s why I need to remove createPolygonDragbehaviour when gizmo is attached to Polygon I guess.

I see. I add a new transform that is the parent of spheres and polygon. And when the polygon is clicked, set the parent in the gizmo manager.

Lines 152-157, lines 240-241
https://playground.babylonjs.com/#QNJLCY#31 you meant something like this? I still can’t figure this out. :confused:
The problem is that some of the drag behaviours of gizmo should work on specific meshes and some not I think.

ok, let me find some time to take a closer look.

1 Like

I finaly found time to make a PG :slight_smile:

https://playground.babylonjs.com/#V0G671#3

It’s mostly copy/paste of what you did without the GUI and infos on the polygon.
Polygon and spheres are parented to the root.
The root is also in the gizmo pickable list.
That list is updated when the polygon is updated.
When the polygon is picked, the root is set as the editable one instead.

Let me know if you need more explanation :slight_smile:

1 Like

Your solution is very clear to me. I was thinking about making two gizmos - separate for polygon and spheres. It was kinda working, but your solution is much much better :slight_smile: Thanks for the explanation!

1 Like

@Cedric, actually I need some more explanation :smiley: I’m wondering why roots yPlaneGizmo renders in the world center and not like in the center of the root. In the picture, you can see the bounding box of the root and it’s yPlane.

the yplane gizmo renders at the position of root.
if you move root and move polygon/spheres, at some point the yplane gizmo can be very far from the center of polygon.
To resolve that, when a sphere onDragEnd occurs, unparent spheres and polygon from root, move the root to the center of polygon. then reparent sphere/polygon to root.

Sorry for having such a hard time understanding it but I think I made everything from your instructions and it still doesn’t work.
I mean even if you render the polygon like near to the end of the ground the yPlane of root gonna show in the center of the world - without spheres/polygon dragging. In the picture above I showed just created polygon, without any draggig.

yes, the root is positioned at (0,0,0). you can create spheres far beyond that. and the root will not move in my PG.
As long as you don’t change root position programatically (when a new sphere is added or moved), the root will only move with the gizmo is moved.

https://playground.babylonjs.com/#V0G671#5 Cedric, something like this?

edit*: https://playground.babylonjs.com/#V0G671#6
even if I’m doing something like this:

            let pos = {...spheresPositionsArr[0]}
            pos.x += 2
            root.position = pos

            polygon.parent = root;
            spheresArray.forEach(element => {
                element.parent = root;
            })
  1. doing some operations on the root first(move it in x-axis by 2)
  2. ascribe spheres/polygon to it as childrens
  3. children’s now also moves by 2 in x-axis but I guess they’re shouldn’t?

edit**
I saw that setting parent by setParent and by qual sign works different, so I’ll try to use it

https://playground.babylonjs.com/#V0G671#7
Ok, I think I did it like you wrote here and It still doesn’t work…

ok, I see what’s going wrong. let me se if I can find a fix :slight_smile:

1 Like

And here it is :slight_smile:

https://playground.babylonjs.com/#V0G671#8

I changed my mind and did all the position computation in relative space. see function recomputeRootSpace()
Also, when adding a new sphere, its position is now relative to root position.

2 Likes