# Dynamically changing pointerDragBehavior

**URL:** <https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204>\
**Category:** Questions\
**Created:** [November 25, 2019, 5:54pm UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204 "2019-11-25T17:54:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![flex](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@flex](https://forum.babylonjs.com/u/flex)\
**Post date:** [November 25, 2019, 5:54pm UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/1 "2019-11-25T17:54:52Z")

</div>

Hi All-

I am having great fun with babylon js! What an awesome piece of software. Many thanks. Ahem.

I have been able to make various pointerDragBehaviors work but I am unable to change the behavior dynamically. I want to test where on a mesh user is clicking and depending on test results change from moving in a plane to moving in a vector.

I tried various approaches. attach() and detach() made the most sense:

```
pointerDragBehavior.onDragStartObservable.add((event)=>{
    if(1 === 1){
        this.pointerDragBehavior.detach(event.source);
        this.pointerDragBehavior2.attach(event.source);

```

This throws ‘cannot read property ‘getScene’ of undefined’.  
[https://www.babylonjs-playground.com/#9UFEBE#80](https://www.babylonjs-playground.com/#9UFEBE#80)

Any advice?

TIA! Michael

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [November 25, 2019, 5:57pm UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/2 "2019-11-25T17:57:30Z")

</div>

> [@flex](#):
>
> I am having great fun with babylon js! What an awesome piece of software. Many thanks

Thanks a lot 😉 And welcome!

Let me invoke our gizmo manager 😉 @Cedric

---

<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:** [November 25, 2019, 6:23pm UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/3 "2019-11-25T18:23:54Z")

</div>

Hi Michael!

Let me take a look at your PR. I’ll let you know when I’ve found something 🙂

---

<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:** [November 25, 2019, 9:24pm UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/4 "2019-11-25T21:24:02Z")

</div>

Try this:

[https://www.babylonjs-playground.com/#9UFEBE#81](https://www.babylonjs-playground.com/#9UFEBE#81)

`PointerDragBehavior._options` is private, but I think it should do no harm to have it available through getter/setter: @Deltakosh?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [November 25, 2019, 9:34pm UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/5 "2019-11-25T21:34:55Z")

</div>

No problem at all

---

<div class="post-metadata">

**Author:** ![flex](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@flex](https://forum.babylonjs.com/u/flex)\
**Post date:** [November 25, 2019, 10:47pm UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/6 "2019-11-25T22:47:40Z")

</div>

Thank you, Evgeni, Cedric and Deltakosh.

Now my code is behaving better but not perfectly.

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#9UFEBE%2383)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

The expected behavior is that when you grab the top half of the box it move on the Y axis, which it does, and when you grab the bottom half it moves in the XZ plane, which it does sometimes.

If I make it so it move on the X axis instead, it works fine. Something is up with applying the dragPlaneNormal options. Could it be related to this issue, that is, some parameters not getting cleared?

> [@PointerDragBehavior dragPlaneNormal problem](https://forum.babylonjs.com/t/pointerdragbehavior-dragplanenormal-problem/1529):
>
> Hi, everyone! Please check this PG: [https://www.babylonjs-playground.com/index.html#UTINNB#16](https://www.babylonjs-playground.com/index.html#UTINNB#16) Lines 48-50: PointerDragBehavior is created to work in the XZ plane. One lines 138 and 138 the behavior is attached and detached to the clicked/released mesh respectively. What happens is, that the SECOND (and only the second) time you drag a mesh, the XY plane is used. After the second time it works as expected. Plus, on the SECOND time, when the object is selected it kind of jumps close to the po…

---

<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:** [November 26, 2019, 10:05am UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/7 "2019-11-26T10:05:09Z")

</div>

Things are more clear now. dragPlaneNormal is used for picking, that means that it needs to be reset back when onDragEndObservable event is fired. That’s the first issue. Then, dragPlanePoint is the world coordinate on that plane. This means that if the dragPlaneNormal is (0,1,0), y will always be 1. give or take some epsilon corresponding to float precision.  
So, you can’t do the test in onDragStartObservable with a y comparison. You can do some raypicking or do your test on some other checks.  
In this PR:  
[https://www.babylonjs-playground.com/#9UFEBE#84](https://www.babylonjs-playground.com/#9UFEBE#84)  
I do the test on z. Imagine a plane that passes by the center of the cube that cuts it in half.

![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/8/8d40124ae1e97e24ac6296111033affc410688f7.png)

If the user clicks below that line (z on the plane is \< box.\_position.z) then keep the drag plane as is. Otherwise, set the dragAxis to (0,1,0).

The z comparison works in this case because the camera is properly aligned along the z axis. For any other camera orientation, you would have to compute the length of the dragPlanePoint to the camera eye position and compare it with the length of the box center to the camera eye position.

---

<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:** [November 26, 2019, 10:45am UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/8 "2019-11-26T10:45:10Z")

</div>

PR created:

> <https://github.com/BabylonJS/Babylon.js/pull/7211>

---

<div class="post-metadata">

**Author:** ![flex](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@flex](https://forum.babylonjs.com/u/flex)\
**Post date:** [November 26, 2019, 8:17pm UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/9 "2019-11-26T20:17:37Z")

</div>

Thanks, @Cedric for pointing out that the dragPlaneNormal causes y = 1 regardless of where click it.

I understand your approach in your example but I don’t understand why it’s necessary.

Here’s an example where I restore the behavior to moving object in y vector after drag ends.

[https://www.babylonjs-playground.com/#9UFEBE#85](https://www.babylonjs-playground.com/#9UFEBE#85)

When I click in the bottom half of the cube the cube moves toward the camera when I drag, not in the xz plane.

If I remove the onDragEndObservable callback something really puzzling happens.

[https://www.babylonjs-playground.com/#9UFEBE#86](https://www.babylonjs-playground.com/#9UFEBE#86)

If I click on the lower half of the cube and drag, the cube move toward the camera.  
If I click and release, THEN click and drag it moves in the xz plane, as expected.

---

<div class="post-metadata">

**Author:** ![flex](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@flex](https://forum.babylonjs.com/u/flex)\
**Post date:** [November 26, 2019, 11:04pm UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/10 "2019-11-26T23:04:23Z")

</div>

Hi All-

Here’s a not so elegant solution.

[https://www.babylonjs-playground.com/#9UFEBE#92](https://www.babylonjs-playground.com/#9UFEBE#92)

I have 2 pointerDragBehaviors, one on the y axis and one on the xz plane, and attach and detach them depending on where the click is. I transfer the dragID as well.

Thanks for all your help! I’m learning a lot and look forward to contributing. Eventually. 😵

---

<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:** [November 26, 2019, 11:21pm UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/11 "2019-11-26T23:21:17Z")

</div>

I think it’s quite a good solution, as it may not be possible to do it by using a single `PointerDragBehavior` instance because some calculations depending on the current `options` are made before `onDragStartObservable` is called, which may explain why it does not work in the other PGs.

---

<div class="post-metadata">

**Author:** ![flex](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@flex](https://forum.babylonjs.com/u/flex)\
**Post date:** [December 4, 2019, 2:18pm UTC](https://forum.babylonjs.com/t/dynamically-changing-pointerdragbehavior/7204/12 "2019-12-04T14:18:25Z")

</div>

I cleaned up my solution a bit.

I removed xzDrag.startDrag(dragId) because the existing drag can be transferred from one behavior uninterrupted. Doing a startDrag results in the clicks in the lower half of the box being “sticky”, i.e. one release (onDragEndObservable) event is missed.

[https://www.babylonjs-playground.com/#9UFEBE#95](https://www.babylonjs-playground.com/#9UFEBE#95)
