Random Mesh Spawn with No Overlap?

Hello everyone,

I’m sure this issue can be easily solved by experienced programmers, but I’m having trouble figuring it out :thinking: Is there a simple method for keeping these meshes from overlapping when they spawn at random locations on the ground?


Thanks in advance for any help or suggestions!

1 Like

You probably just want to add each new mesh to an array and then brute force check the proposed position of each new mesh against each previously added mesh’s position to make sure they don’t overlap with some poor-man’s collision.

I’ve been using

function distanceVector( v1, v2 ) {
var dx = v1.x - v2.x;
var dy = v1.y - v2.y;
var dz = v1.z - v2.z;
return Math.sqrt( dx * dx + dy * dy + dz * dz );

but BABYLON.Vector3 has a nice Distance() method built in (Vector3 - Babylon.js Documentation)

If (BABYLON.Vector3.Distance(mesh[i].position, new Vec3(proposedX,y,proposedZ)) < shoulderWidth) try again.

Something like that.

Alternatively maybe you could just increment initially from 0 with a random x/z value.

for (var ix=0; ix<maxX; ix += Math.rand(spacing)+shoulderWidth){
for (var iz=0; iz<maxZ; iz += Math.rand(spacing)+breastDepth){
new Mesh @ (ix, y, iz) }}

Something like that.

Good luck, have fun!


An alternative, you set up a grid with a positioning rectangle within a spacing rectangle order the grid coordinates randomly in a array and choose grid references fr Let om the array in order fro 0 upwards. This way you can never place overlapping objects.

Let positioning rectangle have width W and depth D, let the width and depth of the object to place be w and d respectively. Let X be the width and Z the depth of the spacing rectangle. Min for X is W + w and min for Z is D + d. The smaller the positioning rectangle the more in lines will be the objects and the smaller the spacing rectangle the closer the objects will be.



Hello @Wingnut, @withADoveInOneHand, and @JohnK

Thanks to each of you for all the very interesting information and possible solutions! :smiley: :+1:

I’ll experiment with both approaches (array and grid) and see which one works best with my project.

Also, thanks for putting together a functional PG example JohnK, that will be very helpful as always.

Hi guys! I deleted my initial reply… too long and boring. :slight_smile: Good to see @JohnK got a workable solution. He’s a genius, and so is “dove”.

A very interesting question and even MORE interesting responses. Love it. Party on, guys.

Hello @JohnK !
What if we don’t know the exact dimensions of the mesh added ; how can we make the ground as different zones(like square zones) and check if there is a mesh already at this zone(considering that a mesh can’t be added to a zone if this one contain another mesh(in its middle ; or at its borders)

Sorry for the delay

This PG places objects of random size within cells of a regular grid

For something more random you need @withADoveInOneHand 's solution.


Thank You , helpful example
let assume that I have one ground ; and a grid Places array(position with width and height of each cell place)

=> loop into all grid Places and check if there’s a mesh on each one => we found the free spaces to have the opportunity to add new objects

How to trigger a mesh in which cell is ; or if a cell is containing already an object?

Thank You