# Simple collision detection on drag

**URL:** <https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130>\
**Category:** Questions\
**Created:** [February 3, 2021, 7:02pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130 "2021-02-03T19:02:24Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![JPeer264](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jpeer264/32/14133_2.png) [@JPeer264](https://forum.babylonjs.com/u/JPeer264)\
**Post date:** [February 3, 2021, 7:02pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/1 "2021-02-03T19:02:24Z")

</div>

Hey people,

I am kinda new to the BabylonJS scene. I wanted to achieve a very simple collision detection when dragging a mesh. Basically I have a ground and one mesh on top. Goal is that the mesh is not draggable outside the ground. I thought it would be the easiest to have a invisible wall around my ground and have a collision detection on the mesh.

In [my example](https://codesandbox.io/s/zealous-clarke-zx707?file=/src/index.ts) I did a very simple setup (at least I guess it is simple and basic) where I have one sphere, which is draggable, and one wall on the side of the ground. This wall is not yet invisible and not everywhere around the ground.

The collision detection works fundamentally, but when I drag the sphere very fast or when I drag far outside the collision detection doesn’t work anymore. Does anybody have a glue why the collision detection is not that exact?

> **[zealous-clarke-zx707 - CodeSandbox](https://codesandbox.io/s/zealous-clarke-zx707?file=%2Fsrc%2Findex.ts)**
>
> zealous-clarke-zx707 by JPeer264 using @babylonjs/core, @babylonjs/gui, @types/cannon, cannon

---

<div class="post-metadata">

**Author:** ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)\
**Post date:** [February 3, 2021, 7:28pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/2 "2021-02-03T19:28:05Z")

</div>

Welcome to the forum!

I have noticed you integrated a physics engine (cannon in that case) to use as collision detector. A physics engine has a lot of advantages, but is sometimes not the right choice for a specific usecase.

If you don’t need the physics calculations (like forces, impulses, gravity, etc’), I would use babylon’s internal collision system instead of a complex physics engine. You can use “mesh.moveWithCollisions” to move and calculate collisions against any collider that you define, and prevent the mesh from going through it completely (the same way our camera collision system works, It is decribed here:

[Camera Collisions | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/cameras/camera_collisions)

---

<div class="post-metadata">

**Author:** ![JPeer264](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jpeer264/32/14133_2.png) [@JPeer264](https://forum.babylonjs.com/u/JPeer264)\
**Post date:** [February 3, 2021, 8:33pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/3 "2021-02-03T20:33:16Z")

</div>

Thanks for the kind words. Also thanks for the quick reply

Alright I checked out the `mesh.moveWithCollisions` method, it looks promising. Still I am not sure how that would work in combination with the `PointerDragBehavior`

I saw some examples using a global variable `startingPoint` like in following example: [Babylon.js Playground](https://www.babylonjs-playground.com/#1TZI08)

I don’t know if I like having that as it uses the global variable `startingPoint` except being encapsulated within one object. Furthermore meshes cannot be clicked when there is no ground behind one mesh (this is why I used `PointerDragBehavior` in the first place)

---

<div class="post-metadata">

**Author:** ![RFhd](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@RFhd](https://forum.babylonjs.com/u/RFhd)\
**Post date:** [May 1, 2022, 5:33pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/4 "2022-05-01T17:33:10Z")

</div>

helloo @JPeer264 , did you figure out how to use moveWithCollisions with the pointerDragBehavior ? I would appreciate any example , as I am trying but it’s not seem to work as expected

Thank You

---

<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:** [May 2, 2022, 3:08pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/5 "2022-05-02T15:08:54Z")

</div>

Do you have a repro of your issue in the playground ?

---

<div class="post-metadata">

**Author:** ![RFhd](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@RFhd](https://forum.babylonjs.com/u/RFhd)\
**Post date:** [May 2, 2022, 3:52pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/6 "2022-05-02T15:52:16Z")

</div>

Helloo @sebavan🙂

> **[Babylon.js Playground](https://playground.babylonjs.com/#1M79PT%235)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

this is repro for my issue  
Any Suggestion will be appreciated

Thank You

---

<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:** [May 2, 2022, 3:53pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/7 "2022-05-02T15:53:46Z")

</div>

Thanks a lot, @Cedric might have some ideas ?

---

<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:** [May 2, 2022, 4:05pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/8 "2022-05-02T16:05:32Z")

</div>

Here i a PG that uses validateDrag to check collisions:

> **[Babylon.js Playground](https://playground.babylonjs.com/#BEYVHR%234)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

The idea here is to replace `Vector3.Distance` with the collision detection of your choice.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [May 2, 2022, 5:12pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/9 "2022-05-02T17:12:01Z")

</div>

> **[Babylon.js Playground](https://playground.babylonjs.com/#1M79PT%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Click on scene to activate key use

A turn anti-clockwise  
D turn clockwise  
W move forward  
S move backward.

Note as in the above PG moveWithCollisions takes a direction vector as a parameter not a position. Try the delta from

> **[PointerDragBehavior | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/babylon.pointerdragbehavior#ondragobservable)**
>
> A behavior that when attached to a mesh will allow the mesh to be dragged around the screen based on pointer events \[API\]

rather than base.position

On mobile currently so cannot provide more help.

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [May 2, 2022, 7:05pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/10 "2022-05-02T19:05:21Z")

</div>

If you want to use moveWithCollisions to check for collisions while dragging, you could do it like this for example. 🙂

```auto
pointerDragBehavior.moveAttached = false
pointerDragBehavior.onDragObservable.add((eventData) => {
    base.moveWithCollisions(eventData.delta)
})

```

> **[Babylon.js Playground](https://playground.babylonjs.com/#1M79PT%237)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![RFhd](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@RFhd](https://forum.babylonjs.com/u/RFhd)\
**Post date:** [May 2, 2022, 7:11pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/11 "2022-05-02T19:11:48Z")

</div>

Thank You All !! 😊

---

<div class="post-metadata">

**Author:** ![RFhd](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@RFhd](https://forum.babylonjs.com/u/RFhd)\
**Post date:** [May 3, 2022, 10:54am UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/12 "2022-05-03T10:54:10Z")

</div>

Hello @blake 🙂  
I have an additional issue that I am trying to solve  
I am using a cuboid instead of a simple ground because I want some thickness  
But the drag function is not working anymore as expected  
maybe you know what can be the issue?

> **[Babylon.js Playground](https://playground.babylonjs.com/#1M79PT%2317)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Thank you in advance

---

<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:** [May 3, 2022, 12:03pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/13 "2022-05-03T12:03:24Z")

</div>

[Mesh Collision | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#1M79PT#18)

with `pointerDragBehavior.updateDragPlane = false;` the dragplane stays the same.

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [May 3, 2022, 4:06pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/14 "2022-05-03T16:06:45Z")

</div>

Also worth noting that the base mesh is now colliding with the ground, which can be fixed by moving the base mesh up by half the ground’s height (by 0.1 in this case). Then things are working as expected again. 🙂

> **[Babylon.js Playground](https://playground.babylonjs.com/#1M79PT%2319)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![JPeer264](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jpeer264/32/14133_2.png) [@JPeer264](https://forum.babylonjs.com/u/JPeer264)\
**Post date:** [May 3, 2022, 4:47pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/15 "2022-05-03T16:47:21Z")

</div>

Hey sorry for getting back so late. Unfortunately I did not came up with a solution in combination with PointerDragBehavior. For now I left it having a global variable and use it as I posted it in the playground. However I might give it another shot as there were some pretty nice examples already here.

---

<div class="post-metadata">

**Author:** ![RFhd](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@RFhd](https://forum.babylonjs.com/u/RFhd)\
**Post date:** [May 4, 2022, 8:56am UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/16 "2022-05-04T08:56:06Z")

</div>

Hello @blake Thank you so much for your response!  
For this case , all goes as expected now!  
May I ask if we are using a transform Node instead of the base mesh  
How this behavior can be done ? knowing that the transform Node doesn’t accept moveWithCollisions  
Because I have another case where I have to use the transform node instead

> **[Babylon.js Playground](https://playground.babylonjs.com/#1M79PT%2323)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Thank you

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [May 4, 2022, 5:09pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/17 "2022-05-04T17:09:37Z")

</div>

Heya, one option, using a TransformNode parent, is to in the validateDrag method check each child for intersections against each box. I tend to dislike using validateDrag to check intersections thou, because a fast drag can’t get as close to the obstacle and also it’s harder to drag along the edge of an obstacle.

> **[Babylon.js Playground](https://playground.babylonjs.com/#1M79PT%2326)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

But another option could be to use an AbstractMesh instead of a TransformNode for the parent, then you can still use moveWithCollisions like before. IMO this option provides the better dragging experience. 🙂

> **[Babylon.js Playground](https://playground.babylonjs.com/#1M79PT%2325)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![RFhd](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@RFhd](https://forum.babylonjs.com/u/RFhd)\
**Post date:** [May 4, 2022, 5:34pm UTC](https://forum.babylonjs.com/t/simple-collision-detection-on-drag/18130/18 "2022-05-04T17:34:17Z")

</div>

Amazing !! Thank you so much for your time and solutions !! 😊
