Intersection between two objects

I am trying to find intersections between two objects to be able to put objects to the correct place.

Check the image below, the yellow squares are the objects that i want to intersect and put them in the brown area only if they intersect with the brown area and does not intersect with the darker area and with the green square

This is the code that i am using right now:

const directions: THREE.Vector3[] = [];
for (let i = 0; i < 360; i += 3) {
    directions.push(new THREE.Vector3(Math.cos(i), 0, Math.sin(i)));
}

const mesh = await createMesh(context, item);

const raycaster = new THREE.Raycaster(undefined, undefined, 0, size);
let ok = false;
for (const direction of directions) {
    raycaster.set(mesh.position, direction);
    const allowedIntersects = raycaster.intersectObjects(allowedIntersectObjects, true);
    const unallowedIntersects = raycaster.intersectObjects(unallowedIntersectObjects, true);

    if (unallowedIntersects.length) {
        ok = false;
        break;
    }

    if (allowedIntersects.length) {
        ok = true;
    }
}

if (ok) {
    unallowedIntersectObjects.push(mesh);
    context.scene.add(mesh);
}

So in the image above,

  • Each item has sizes, Item 1 has size 10, Item 2-4 has size 20, Item 3 has size 5.
  • Item 1: allowedIntersects is correctly filled and i have the brown area object inside that. Which is ok because i want to intersect with that brown area but it does not fill the unallowedIntersects which has that darker area, so it puts the object there which is not correct.
  • Item 2: allowedIntersects is correctly filled and i have the brown area object inside that. Which is ok again but it does not fill the unallowedIntersects which has that Item 4, so it puts the object there which is not correct.
  • Item 3: It’s correctly placed because it intersects with brown area and not intersecting with any unallowed object.
  • If i put item 3 where item 2 is now, unallowedIntersects is correctly filled with Item 4 and it’s not putting my item there. Same for Item 1, if i put item 3 to where item 1 is now, unallowedIntersects is correctly filled with darker area and it’s not putting the item there.

So what is the problem with Item 1 and item 2 not intersecting with unallowedIntersectObjects

I think doing these intersections with a raycaster is probably causing unnecessary pain. Are the objects 2D, as shown in the image, or 3D?

If 3D you could use THREE.Box3.intersectsBox or three-mesh-bvh. If 2D maybe rbush.

1 Like

It’s 2D but i can’t use intersectsBox because the object i want to intersect with is not a box, sphere. It’s a shape that has a lot of different edges. But seems like three-mesh-bvh has a function named intersectsGeometry maybe that can help if it works for my shape.

Edit: it still does not work, i tried to use intersectsGeometry but it returns true no matter what. Also i need collide instead of intersections. Because what i want to find is if the 2 objects touches each other, i don’t want to defined near and far properties because they will mess up my logic anyways since all i want is to check if the object1 touches object2.