# When I move the object next to another object with the mouse, I want them to come side by side without any space between them, like a magnet?

**URL:** <https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848>\
**Category:** Questions\
**Created:** [October 12, 2023, 10:04am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848 "2023-10-12T10:04:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Melih](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/melih/32/35454_2.png) [@Melih](https://forum.babylonjs.com/u/Melih)\
**Post date:** [October 12, 2023, 10:04am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/1 "2023-10-12T10:04:29Z")

</div>

When I move the object next to another object with the mouse, I want them to come together like a magnet, without any space between them? How can I do it ?

---

<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:** [October 12, 2023, 3:14pm UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/2 "2023-10-12T15:14:22Z")

</div>

want to show us all what you have done so far? This question can be answered in many different ways, based on the use case.

---

<div class="post-metadata">

**Author:** ![Melih](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/melih/32/35454_2.png) [@Melih](https://forum.babylonjs.com/u/Melih)\
**Post date:** [October 13, 2023, 5:56am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/3 "2023-10-13T05:56:35Z")

</div>

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

Hello

When two cubes approach each other, I want them to stick to each other without overlapping.How can I do it ?

---

<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:** [October 13, 2023, 8:38am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/4 "2023-10-13T08:38:36Z")

</div>

You can use collision detection for that:

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

---

<div class="post-metadata">

**Author:** ![Melih](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/melih/32/35454_2.png) [@Melih](https://forum.babylonjs.com/u/Melih)\
**Post date:** [October 13, 2023, 8:47am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/5 "2023-10-13T08:47:31Z")

</div>

Collusion does not work for me. I want the objects to stick to each other according to their boundaries when held with the mouse cursor and brought closer to the other object.

---

<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:** [October 13, 2023, 8:53am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/6 "2023-10-13T08:53:06Z")

</div>

And this is why we ask for a reproduction of something that doesn’t work. Providing a scene with two boxes (at the same position) and no real explanation as to what isn’t working will not help us help you. How do you pick, where are they positioned. are they always boxes? spheres? How does 2D mouse movement translates to 3D space (again - in your case)? Collisions does solve your initial question, but doesn’t solve the problem in your use-case.

Instead of us doing your homework, show us everything you have done, until it doesn’t do what you want it to do. We will **always** be happy to help in those scenarios!

---

<div class="post-metadata">

**Author:** ![Melih](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/melih/32/35454_2.png) [@Melih](https://forum.babylonjs.com/u/Melih)\
**Post date:** [October 16, 2023, 5:57am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/7 "2023-10-16T05:57:50Z")

</div>

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

I have two objects in the repo and as these two objects approach each other, I want them to stick to each other from the edges and not overlap.

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [October 16, 2023, 10:31am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/8 "2023-10-16T10:31:57Z")

</div>

> [@Melih](#):
>
> I have two objects in the repo and as these two objects approach each other, I want them to stick to each other from the edges and not overlap.

Take a look at this demo 🙂 [Collision and "attachment points" - #4 by carolhmj](https://forum.babylonjs.com/t/collision-and-attachment-points/23678/4)

---

<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:** [October 16, 2023, 10:33am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/9 "2023-10-16T10:33:45Z")

</div>

Also:

- [Magnetic drag and drop feature](https://forum.babylonjs.com/t/magnetic-drag-and-drop-feature/43408)
- [There are any doc or PG to show how does SurfaceMagnetismBehavior work?](https://forum.babylonjs.com/t/there-are-any-doc-or-pg-to-show-how-does-surfacemagnetismbehavior-work/38880)

---

<div class="post-metadata">

**Author:** ![Melih](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/melih/32/35454_2.png) [@Melih](https://forum.babylonjs.com/u/Melih)\
**Post date:** [October 17, 2023, 10:45am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/10 "2023-10-17T10:45:11Z")

</div>

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

Two objects stick together when they approach each other, but I want the side faces of the two objects to stick to each other when they approach each other. They should not be intertwined. How can I do this?

---

<div class="post-metadata">

**Author:** ![Melih](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/melih/32/35454_2.png) [@Melih](https://forum.babylonjs.com/u/Melih)\
**Post date:** [October 18, 2023, 6:52am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/11 "2023-10-18T06:52:13Z")

</div>

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

hello again everyone

I have two boxes and they can move in all directions. I want them to attract each other like magnets. I want the attraction to stop as soon as the two boxes come into contact. How can I do it?

---

<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:** [October 18, 2023, 9:39am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/12 "2023-10-18T09:39:39Z")

</div>

[Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#9F1EA3#4)

You can validate the drag and say if it should drag or not.

---

<div class="post-metadata">

**Author:** ![Melih](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/melih/32/35454_2.png) [@Melih](https://forum.babylonjs.com/u/Melih)\
**Post date:** [October 18, 2023, 9:56am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/13 "2023-10-18T09:56:24Z")

</div>

> [@RaananW](#):
>
> You can validate the drag and say if it should drag or not.

My goal is that it will be draggable. When two objects approach each other, it does not matter from which side they approach, I want it to stick from where it approaches, but they will not intertwine, it will stop as soon as it touches.

---

<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:** [October 18, 2023, 10:04am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/14 "2023-10-18T10:04:40Z")

</div>

have you checked the playground?

---

<div class="post-metadata">

**Author:** ![Melih](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/melih/32/35454_2.png) [@Melih](https://forum.babylonjs.com/u/Melih)\
**Post date:** [October 18, 2023, 10:15am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/15 "2023-10-18T10:15:31Z")

</div>

yes ı checked my playground I don’t have a problem.Can you help me with the problem I described?

---

<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:** [October 18, 2023, 10:28am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/16 "2023-10-18T10:28:16Z")

</div>

Do you want to explain what you tried? what is working and what not?  
I hope someone else from the community can help, because I have no idea what you are trying to achieve.

---

<div class="post-metadata">

**Author:** ![Dad72](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dad72/32/13_2.png) [@Dad72](https://forum.babylonjs.com/u/Dad72)\
**Post date:** [October 18, 2023, 10:28am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/17 "2023-10-18T10:28:21Z")

</div>

RaananW’s PG does exactly what you want here.

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

---

<div class="post-metadata">

**Author:** ![Melih](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/melih/32/35454_2.png) [@Melih](https://forum.babylonjs.com/u/Melih)\
**Post date:** [October 18, 2023, 10:30am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/18 "2023-10-18T10:30:00Z")

</div>

It doesn’t do exactly what I want, but I also want it to stick to each other at its borders, just like a magnet.

---

<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:** [October 18, 2023, 10:31am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/19 "2023-10-18T10:31:17Z")

</div>

it is a starting point. Now it is up to you to take it, and make it your own.  
We don’t provide you with a final project for you to use. We provide you with solutions, in case they might not be fully documented or clear.

It is your task to take it, understand it, and fix it to your needs

---

<div class="post-metadata">

**Author:** ![Dad72](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dad72/32/13_2.png) [@Dad72](https://forum.babylonjs.com/u/Dad72)\
**Post date:** [October 18, 2023, 10:32am UTC](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848/20 "2023-10-18T10:32:02Z")

</div>

Do you want it to stay attached afterwards? you can use parent.

Box2.parent = box1

[Next page](https://forum.babylonjs.com/t/when-i-move-the-object-next-to-another-object-with-the-mouse-i-want-them-to-come-side-by-side-without-any-space-between-them-like-a-magnet/44848.md?page=2)
