Can anyone give me an idea how to do random object without overlap in JMonkey? I tried to calculate distance between 2 vectors but seem likes it’s not working. Thanks!

# [SOLVED] Create objects at random position without overlap in jmonkey

**normen**#2

Calculating the distance between two vectors works. You can also use the bounding box for more information about what distance to actually calculate.

**TiGon**#4

this is my method to calculate distance between vectors then add vectors to array. However , when I run the game object still overlapping

public ArrayList getVector() {

Vector3f vec = new Vector3f();

Vector3f v1 = randVector();

list.add(v1);

for (int i = 0; i < 3; i++) {

vec = randVector();

if (i == 0) {

while (v1.distance(vec) < 4) {

vec = randVector();

}

list.add(vec);

}

if (i == 1) {

while (list.get(0).distance(vec) < 4 || list.get(1).distance(vec) <4) {

vec = randVector();

}

list.add(vec);

}

if (i == 2) {

while (list.get(0).distance(vec) < 4 || list.get(1).distance(vec) <4 || list.get(2).distance(vec) < 4) {

vec = randVector();

}

list.add(vec);

}

}

return list;

}

**TiGon**#5

Thank for your reply , I added my code , could you please have a look at it ,I don’t know what I did wrong?

**sgold**#7

I can’t guess why you created individual cases for each iteration of the loop. The more conventional approach would be to use two nested loops, an outer one for generating vectors and one for comparing each new vector with those already generated. However, I tested your code and it worked for me: each of the resulting vectors was at least 4 units from the others.

Perhaps your objects are more than 4 units in diameter, or perhaps the bug is in code you didn’t share, such as randVector().

**TiGon**#8

You 're right. my objects were more than 4 units in diameter. I didn’t notice that until I saw your comment . Thank you very much