Place the mesh at the top of the pyramid

Hi. I need to build a pyramid of cubes. I’m trying to do this using intersectMesh. I’m going through all the unselected cubes in the loop.

                           randomBox.forEach(function(item, index, array) {    // lift box 
                             if ( currentMesh.intersectsMesh(item) && currentMesh !== item ) {
                                 currentMesh.position.y = item.getBoundingInfo().boundingBox.maximumWorld._y + currentMesh.getBoundingInfo().boundingBox.maximum.y;  
                                }
                           }); 

The code works well if it is done in ascending order of indexes. But if you build randomly, then it does not always work as it should. Maybe someone can help find a mistake, or tell another construction option?
Drag Demo | Babylon.js Playground (babylonjs.com)

good

not_good

Sort it before checking? https://playground.babylonjs.com/#7CBW04#2116 Is that the desired behaviour?

2 Likes

Yes, I think so, it works great! Thank you very much!