Absolute value of `snapDistance` in `onSnapObservable` of `PositionGizmo`

Hello Babylon.js community!

Currently, I am playing around with the PositionGizmo with “snapping” enabled.

I was wondering

  1. how I would know in which direction the gizmo was dragged (positive vs. negative) and
  2. therefore, would like to ask if it is expected that the snapDistance passed by the onSnapObservable seems to be the absolute value (unsigned).
  3. is it true, that the position of the mesh attached to the gizmo is not yet updated in the callback of onSnapObservable?

I think I would have expected that it would return a negative value if the gizmo gets dragged into the “opposite” direction so that I could calculate the new position already in the callback :innocent:.

Please, see this playground and specifically the following part.

gizmo.snapDistance = 0.1;
gizmo.xGizmo.onSnapObservable.add(snap => {
        snap.snapDistance, // Only absolute value of dragDistance
        gizmo.attachedMesh?.position // After first drag, position is still 0,1,0

Thanks in advance for your clarification :pray:!

cc @Cedric the Gizmo daddy

Hi @Philippe and welcome to the forum!

  1. you can compute the delta between current and previous position. For now, there is no way to query this value.
  2. yes, it’s a distance value. if a snap is asked to happen every meter, it doesn’t matter which direction
  3. I don’t know actually. callbacks and observable are numerous and happen in different places, so I’m not sure for that one.

Why not using onDragObservable and query the object position ?

Thank you for your reply @Cedric!

I played around with onDragObservable. Interestingly, the dragDistance seems to return a negative number when dragging in the “opposite” direction.

This feels like a tiny inconsistency or do I maybe mix something up :sweat_smile:?

See updated playground.

gizmo.xGizmo.dragBehavior.onDragObservable.add(drag => {
        drag.dragDistance // This distance can be negative

Hey @Cedric,

sorry for bothering again :innocent:.

But I would be curious to hear if you think it would make sense to align onSnapObservable & onDragObservable so that both of their distance could be negative?

Thanks a lot :pray:!

I’ll take a look this week and see what I can do.

Hi @Philippe
Sorry for the huge delay. Yes, I think it makes sense to have negative distance in onSnapObservable. I’m doing the change now.

snap distance with axisDragGizmo is now signed. It was already signed for planeRotation and axisScale

1 Like

Thanks a lot :tada:!