# How to detect intersections of two faces of two bufferGeometryBox?

**URL:** <https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761>\
**Category:** Questions\
**Tags:** geometry\
**Created:** [May 27, 2019, 12:11pm UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761 "2019-05-27T12:11:31Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [May 31, 2019, 7:11am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/21 "2019-05-31T07:11:18Z")

</div>

Because there are 12 faces, and i want to detect side

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [May 31, 2019, 7:20am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/22 "2019-05-31T07:20:27Z")

</div>

So, we have 12 faces on 6 sides, each side has 2 faces, thus to find the side by a face index, you need to divide it by 2.  
`Math.floor( intersects[0].faceIndex / 2 );`

---

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [May 31, 2019, 7:23am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/23 "2019-05-31T07:23:30Z")

</div>

Oh, sorry. This number from other code. Yes, ofcourse, but my face has faceIndex in range from 30 to 1800

---

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [May 31, 2019, 7:24am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/24 "2019-05-31T07:24:50Z")

</div>

You can see it in console log  
[http://alovert.ru/](http://alovert.ru/)

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [May 31, 2019, 7:24am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/25 "2019-05-31T07:24:53Z")

</div>

Are you sure that you intersect a box with 12 faces?

---

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [May 31, 2019, 7:26am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/26 "2019-05-31T07:26:30Z")

</div>

Not sure. I set _group_ in raycaster

`intersects = raycaster.intersectObject(group, true);`

---

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [May 31, 2019, 7:28am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/27 "2019-05-31T07:28:45Z")

</div>

If i set single mesh object, intersection not working

---

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [May 31, 2019, 7:30am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/28 "2019-05-31T07:30:43Z")

</div>

Pardon, i forgot delete this code .toNonIndexed()

---

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [May 31, 2019, 7:31am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/29 "2019-05-31T07:31:13Z")

</div>

Hi again! I have some trouble …

How to intersect non indexed buffer geometry? I want to detect intersection between two defferent faces of two different non indexed buffergeometry. I set ray from middle of each faces, but raycaster don`t work with non indexed geometry.

I want to see result like this:[![enter image description here](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/1/18a312a9ecd23ea861ee3b727eb564f775afcffc.png)](https://i.stack.imgur.com/Lzfh3.png)

Thank you very much!

**my code**

```auto
var box = new THREE.Mesh(geometry.toNonIndexed(), material);

var raycaster = new THREE.Raycaster();
var intersects = [];

var pos = box.geometry.attributes.position;
var ori = new THREE.Vector3();
var dir = new THREE.Vector3();

var a = new THREE.Vector3(),
  b = new THREE.Vector3(),
  c = new THREE.Vector3(),
  tri = new THREE.Triangle();

var faces = pos.count / 3;
for (let i = 0; i < faces; i++) {
  a.fromBufferAttribute(pos, i * 3 + 0);
  b.fromBufferAttribute(pos, i * 3 + 1);
  c.fromBufferAttribute(pos, i * 3 + 2);
  tri.set(a, b, c);
  tri.getMidpoint(ori);
  tri.getNormal(dir)
  // scene.add(new THREE.ArrowHelper(raycaster.ray.direction, 
  raycaster.ray.origin, 30, 0xff0000));
  raycaster.set(ori, dir);
  intersects = raycaster.intersectObject(box, true);

  if (intersects.length > 0) {

    console.log(intersects[0]);

  }
}

```

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [June 9, 2019, 10:57am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/30 "2019-06-09T10:57:08Z")

</div>

/cc

> <https://stackoverflow.com/questions/56513248/how-to-get-intersection-between-two-different-faces-of-two-different-non-indexed>

---

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [June 9, 2019, 10:58am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/31 "2019-06-09T10:58:11Z")

</div>

What does it mean?

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [June 9, 2019, 11:03am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/32 "2019-06-09T11:03:50Z")

</div>

Nothing bad. Just the other users can track the issue on other resources.

---

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [June 9, 2019, 2:10pm UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/33 "2019-06-09T14:10:33Z")

</div>

Oh, ok! I just intersting what mean /cc 😄

---

<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:** [June 9, 2019, 2:36pm UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/34 "2019-06-09T14:36:43Z")

</div>

CC comes from the email domain: [https://www.techwalla.com/articles/what-does-cc-mean-in-a-text](https://www.techwalla.com/articles/what-does-cc-mean-in-a-text)

---

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [June 9, 2019, 2:39pm UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/35 "2019-06-09T14:39:53Z")

</div>

Wow, this is a discovery for me. I’m Russian 😀

---

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [June 12, 2019, 3:51am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/37 "2019-06-12T03:51:23Z")

</div>

Hi! I sucessfully set ray from each faces and when intersection detect face , its return flag true. But i have a problem. I cant to return flag false when i delete object, that intersects. My code:

```
				box.userData.sides = {
					0: false,
					1: false,
					2: false,
					3: false,
					4: false,
					5: false
				}
				

				collision.push(box);
				scene.add(box);

				checkRaycast();
			
		
	}

	load(0, 0, 0, 'cube', 0, 0, 0);

	function checkRaycast() {
		var raycaster = new THREE.Raycaster();
		var intersects = [];
		for (let j = 0; j < collision.length; j++) {

			var pos = collision[j].geometry.attributes.position; 
			var ori = new THREE.Vector3();
			var dir = new THREE.Vector3();
			var a = new THREE.Vector3(),
				b = new THREE.Vector3(),
				c = new THREE.Vector3(),
				tri = new THREE.Triangle();
		
			var index = collision[j].geometry.index;	
			var faces = index.count / 3;
			scene.updateMatrixWorld()
			for (let i = 0; i < faces; i++) {
				a.fromBufferAttribute(pos, index.array[i * 3 + 0]);
				b.fromBufferAttribute(pos, index.array[i * 3 + 1]);
				c.fromBufferAttribute(pos, index.array[i * 3 + 2]);
				a.set(a.x + collision[j].position.x, a.y + collision[j].position.y, a.z + collision[j].position.z);
				b.set(b.x + collision[j].position.x, b.y + collision[j].position.y, b.z + collision[j].position.z);
				c.set(c.x + collision[j].position.x, c.y + collision[j].position.y, c.z + collision[j].position.z);
				tri.set(a, b, c);
				tri.getMidpoint(ori);
				tri.getNormal(dir);
				
				raycaster.set(ori, dir);	
				
				intersects = raycaster.intersectObjects(collision, true);
				
				//scene.add(new THREE.ArrowHelper(dir, ori, 500, 0xff0000));
				if (intersects.length > 0) {
					var intFace = Math.floor(intersects[0].faceIndex / 2);
					if (intersects[0].distance > 0 && intersects[0].distance < 0.2) {
						intersects[0].object.userData.sides[intFace] = true;
					}
				} else {
			
					collision[j].userData.sides[0] = false;
					collision[j].userData.sides[1] = false;
					collision[j].userData.sides[2] = false;
					collision[j].userData.sides[3] = false;
					collision[j].userData.sides[4] = false;
					collision[j].userData.sides[5] = false;
					
				}

			}
		}
	}

```

My project: [https://alovert.ru](https://alovert.ru)

To add object, please click on object LBM, to delete RBM

---

<div class="post-metadata">

**Author:** ![Arkadiy\_Vinkovskiy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arkadiy_vinkovskiy/32/58384_2.png) [@Arkadiy\_Vinkovskiy](https://discourse.threejs.org/u/Arkadiy_Vinkovskiy)\
**Post date:** [June 12, 2019, 3:59am UTC](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761/38 "2019-06-12T03:59:36Z")

</div>

I set the flag to false for each side, if the sides do not intersect. But this flag is set for all sides, regardless of whether the sides intersect or not

[Previous page](https://discourse.threejs.org/t/how-to-detect-intersections-of-two-faces-of-two-buffergeometrybox/7761.md?page=1)
