# Avoiding Collision between two boxes

**URL:** <https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235>\
**Category:** Questions\
**Tags:** drag-controls, collisions\
**Created:** [December 5, 2019, 10:58am UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235 "2019-12-05T10:58:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThreejsNewbie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/threejsnewbie/32/7975_2.png) [@ThreejsNewbie](https://discourse.threejs.org/u/ThreejsNewbie)\
**Post date:** [December 5, 2019, 10:58am UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/1 "2019-12-05T10:58:49Z")

</div>

![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/9/9125b349652554b627e97fba161f00c2ebbe4db4.jpeg)  
Hi,  
I want to avoid the merging of the two boxes when they collide.I’m able to detect the collision but don’t know how to avoid it from merging.Can anyone please suggest any method that fits here.Thanks  
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

```
var dragControls = new THREE.DragControls( objects, camera, renderer.domElement);

var collision;

dragControls.addEventListener( 'drag', function ( event ) {

intersectObjMas(event.object,objects);

function intersectObjMas(obj, mas) {

     for (var i = 0; i < mas.length; i++) {

	     if(event.object != mas[i]){
		 
                    var firstObject = event.object;

                    var secondObject = mas[i];

                      firstBB = new THREE.Box3().setFromObject(firstObject);

                     secondBB = new THREE.Box3().setFromObject(secondObject);
                     
                     collision = firstBB.isIntersectionBox(secondBB);
                                                   }
                                                              }
                                                                    }

 if ( event.object.position.y < (event.object.geometry.parameters.height /2) ) {

   event.object.position.y = (event.object.geometry.parameters.height /2); // this is to avoid the cube from passing thorugh the ground surface below

  } 
 else {

  	dragControls.addEventListener( 'dragstart', function ( event ) {

      controls.enabled = false;
  		
  	});

    dragControls.addEventListener( 'dragend', function ( event ) {

      controls.enabled = true;

    });

  };

});
```

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [December 5, 2019, 1:44pm UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/2 "2019-12-05T13:44:08Z")

</div>

You normally implement a basic collision detection test like so:

- Save the current transformation of the moving object.
- Apply the transformation of the current frame. In your case, this is done by `DragControls` when dragging an object in 3D space.
- Now perform the collision detection test. In your case it’s an AABB/AABB intersection test.
- If there is an intersection, restore the object’s transformation to the status of the last frame. Or in other words: Prevent the transformation of the current frame.

---

<div class="post-metadata">

**Author:** ![ThreejsNewbie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/threejsnewbie/32/7975_2.png) [@ThreejsNewbie](https://discourse.threejs.org/u/ThreejsNewbie)\
**Post date:** [December 6, 2019, 7:59am UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/3 "2019-12-06T07:59:58Z")

</div>

Could you suggest me any method to store the previous position.  
I tried printing the before collision and after collision position but was getting the same value

```auto
var dragControls = new THREE.DragControls( objects, camera, renderer.domElement);

var collision;

dragControls.addEventListener( 'drag', function ( event ) {
 console.log('old position ' + event.object.position.x);
intersectObjMas(event.object,objects);

function intersectObjMas(obj, mas) {

     for (var i = 0; i < mas.length; i++) {

	     if(event.object != mas[i]){
		 
                    var firstObject = event.object;

                    var secondObject = mas[i];

                      firstBB = new THREE.Box3().setFromObject(firstObject);

                     secondBB = new THREE.Box3().setFromObject(secondObject);
                     
                     collision = firstBB.isIntersectionBox(secondBB);
 console.log('newposition ' + event.object.position.x);
                                                   }
                                                              }
                                                                    }

 if ( event.object.position.y < (event.object.geometry.parameters.height /2) ) {

   event.object.position.y = (event.object.geometry.parameters.height /2); // this is to avoid the cube from passing thorugh the ground surface below

  } 
 else {

  	dragControls.addEventListener( 'dragstart', function ( event ) {

      controls.enabled = false;
  		
  	});

    dragControls.addEventListener( 'dragend', function ( event ) {

      controls.enabled = true;

    });

  };

});

```

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [December 6, 2019, 10:07am UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/4 "2019-12-06T10:07:53Z")

</div>

> [@ThreejsNewbie](#):
>
> Could you suggest me any method to store the previous position.

You are currently logging not the “old position” in your `drag` event handler. The position vector was already changed at this point. So it’s better to create a `Vector3` called `currentPosition` and save the position at the end of the handler e.g.

```js
const object = event.object;
object.userData.currentPosition.copy( object.position );

```

You can create and init `currentPosition` when creating the actual object. In any event, you now have the current and the new position in the drag event handler and can then perform a restore if necessary.

---

<div class="post-metadata">

**Author:** ![ThreejsNewbie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/threejsnewbie/32/7975_2.png) [@ThreejsNewbie](https://discourse.threejs.org/u/ThreejsNewbie)\
**Post date:** [December 6, 2019, 11:16am UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/5 "2019-12-06T11:16:42Z")

</div>

Hi Michael,Its not working in my case  
This is the initial position

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/8/850778cb627aec4b8ce2a49c6ba2362d8617aed3.jpeg)  
I wanted the position when they are this close (image 2)  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/4/43ef88e005b3c3edebbca93464ce9efb3ae4214c.jpeg)  
(image 3) should not happen it must get back to image 2  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/5/52266b31da392584eea4d6b2616188da5d78110e.jpeg)

Is there any other way

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [December 6, 2019, 9:29pm UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/6 "2019-12-06T21:29:42Z")

</div>

It seems you are doing something wrong in your code. It’s best to share your current work of process as a live example so it’s possible to debug the issue.

---

<div class="post-metadata">

**Author:** ![ThreejsNewbie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/threejsnewbie/32/7975_2.png) [@ThreejsNewbie](https://discourse.threejs.org/u/ThreejsNewbie)\
**Post date:** [December 7, 2019, 3:32am UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/7 "2019-12-07T03:32:33Z")

</div>

[Newcontainermodel (1).zip](https://discourse.threejs.org/uploads/short-url/h3zbFqDxQNmdU4rUtJsB6XxmiSO.zip) (1.9 MB)  
Hi michael i have attached the zip file related to the code.  
The script file has all the code related to the cubes

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [December 7, 2019, 9:46am UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/8 "2019-12-07T09:46:49Z")

</div>

Here is your updated `scripts.js`. I’ve only implemented what I’ve been described in my earlier post.

[scripts.js](https://discourse.threejs.org/uploads/short-url/21nFDOIDRpqoZUFtEJ3N1K3oaEu.js) (9.9 KB)

Tip: You have a lot of unused variables in your file. I can only recommend to clean your code up a bit. It’s very confusing to understand right now what parts are actually relevant.

---

<div class="post-metadata">

**Author:** ![ThreejsNewbie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/threejsnewbie/32/7975_2.png) [@ThreejsNewbie](https://discourse.threejs.org/u/ThreejsNewbie)\
**Post date:** [December 7, 2019, 10:26am UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/9 "2019-12-07T10:26:44Z")

</div>

Thanks Michael,  
The boxes were passing through the ground but were not overlapping each other due to the changes made. I made a few changes and now its working properly.  
Thanks for your time and help  
🙂

---

<div class="post-metadata">

**Author:** ![SEBASTHIAN\_COPCA](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/sebasthian_copca/32/10448_2.png) [@SEBASTHIAN\_COPCA](https://discourse.threejs.org/u/SEBASTHIAN_COPCA)\
**Post date:** [April 17, 2020, 6:02am UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/10 "2020-04-17T06:02:33Z")

</div>

I have the same problem; I am starting with ThreeJs. Could you pass me the project.

---

<div class="post-metadata">

**Author:** ![ThreejsNewbie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/threejsnewbie/32/7975_2.png) [@ThreejsNewbie](https://discourse.threejs.org/u/ThreejsNewbie)\
**Post date:** [April 17, 2020, 7:41am UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/11 "2020-04-17T07:41:06Z")

</div>

hi sebasthian the zip contains the collision avoidance code  
[Dragmodel.zip](https://discourse.threejs.org/uploads/short-url/cgAms6SM7Iz3Z4XRrDlh1lTOkpl.zip) (916.5 KB)

---

<div class="post-metadata">

**Author:** ![Pravin\_Poudel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pravin_poudel/32/33369_2.png) [@Pravin\_Poudel](https://discourse.threejs.org/u/Pravin_Poudel)\
**Post date:** [October 27, 2022, 7:56am UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/12 "2022-10-27T07:56:13Z")

</div>

You have put camera inside box but it is not stopping it to get inside another box, why?

---

<div class="post-metadata">

**Author:** ![Atul\_Mourya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/atul_mourya/32/1155_2.png) [@Atul\_Mourya](https://discourse.threejs.org/u/Atul_Mourya)\
**Post date:** [October 29, 2022, 5:18pm UTC](https://discourse.threejs.org/t/avoiding-collision-between-two-boxes/11235/13 "2022-10-29T17:18:25Z")

</div>

Just a word of caution here. The above solution works really well in avoiding overlaps.  
However, this method depends on the drag speed of the cursor.

As an example: if you try to drag and move one box towards the other, the spacing will be depending on on speed of the cursor moved in that direction,  
The greater the speed, the greater the gap.  
Even if anyone try to move the object really slow, it still won’t be 100% snapping.

In this case, am asssuming, there will need additional computation to support snapping to the nearest vertex or the face.
