InstancedMesh Random Position

I have many cubes in the scene randomly placed in a circle
I want to place an object in a static position and remove the cubes there
How to remove cubes at a certain point?

  for (var i = 1; i < count; i++) {
    let angle = somthing;
    let radius = somthing;

    const x = Math.cos(angle) * radius;
    const z = Math.sin(angle) * radius;

    const cubePosition = new THREE.Vector3(x, 0, z);

As you can see in my scene, there are three cylinders where I want to remove the cubes around the cylinders

Does anyone have any ideas?

Well you can take 2 aproches based what you render first.

I asume in this example you generate cylinders randomly, if that is not the case it will be even easier.

If you generate the cubes first you can store the randomly generated x and z coordinates of the cubes in a variable. The type of data you would store them in is up to you but I would probably go for an array.

After the cube loop is finished and you have your stored coordinates you can start generating the cylinders.
When you generate the cylinders the rules should be quite simple.
If the position of the generated cylinder dose not mach any of the data that is in your position variable ( + an additional offset based on the size of your cubes and cylinders, which you can get thru try and error ) the cylinder should be generated, if it does match any of the positions the loop should run again. Keep in mind that when a new cylinder js generated you will have to add his position to the cube position varibale so it does not interact with the other cylinders.

And if you generate the cylinders first you just do the opposite.

1 Like

thank you for your response.
Your solution seems logical, can you create a simple example?

/cc

Yes, Its me

Use box3 to check collisions Box3 – three.js docs (threejs.org)

My cylinders in the scene are gltf model.
I don’t know how to use box 3 for gltf models, Is it possible?

I found an example for collision detection but as i say my cylinders are gltf model and don’t know how to do it with box 3 and OBB.
example

I think this might happen if I put my cubes loop generated in the if statement in this example. its Right?

I checked your solution
In this solution, you assume that the cubes are created and then the cylinders look for empty spaces to create.
The point is that I don’t want the cubes to be created at the point where the cylinders are created, rather than the cylinders looking for empty space to create.
In two modes with random or predetermined cylinder position.
Another point is that the cylinders are GLTF models
All I have is the position of the cubes in an array and the position of each cylinder in a separate vector3 variable.