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 theunallowedIntersects
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 theunallowedIntersects
which has thatItem 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