Version: 7.30.0
Engine: WebGL2
Playground: Babylon.js Playground
Description:
I’m trying to move 2 meshes via PointerDragBehavior, so I attached PointerDragBehavior to one mesh (sphere), and move anther mesh (followingMesh) in the onDragObservable event, and update the position of followingMesh with the position of sphere.
But as I drag, the positions of the 2 meshes got different, see the screenrecording below, how to resolve it?
Actual:
Expected:
The 2 meshes should have the the same position while dragging.
I believe one needs to update the position more often than DragBehavior fires events.
Here is the one of possible solutions - https://playground.babylonjs.com/#PQUTZO#1
The positions printer on the GUI are positions fired with DragBehavior, not DradEnd. If you sync positions at the DragEnd, they are identical. Or if you drag again, they will be synced at start.
The difference (for X coord) is seen here - https://playground.babylonjs.com/#PQUTZO#4
Is this something with dirty mechanism? If line 81 being changed to followingMesh.position = sphere.position.clone();, the rendered and displayed position diffs.
Hi @kzhsw,
The reason you were seeing that behavior is that the PointerDragBehavior doesn’t instantly move the mesh to the new mouse location, it animates it there. So, when you were reading the sphere’s position and setting the followingMesh’s position to that position, it was the current position of the sphere, not where it was animating to.
The event data provided to the callback for the onDragObservable has what you need though.
event.delta gives you the change in position that is being applied to the attached mesh (the sphere), so we can apply it to the follow mesh and it’ll jump exactly to where the sphere is going to animate to.