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

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!

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

1 Like

If you shared some of your code, someone might be able to tell you why it’s not working.

1 Like

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();
for (int i = 0; i < 3; i++) {
vec = randVector();
if (i == 0) {
while (v1.distance(vec) < 4) {
vec = randVector();
}
}
if (i == 1) {
while (list.get(0).distance(vec) < 4 || list.get(1).distance(vec) <4) {
vec = randVector();
}
}
if (i == 2) {
while (list.get(0).distance(vec) < 4 || list.get(1).distance(vec) <4 || list.get(2).distance(vec) < 4) {
vec = randVector();
}
}
}
return list;
}

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

2 Likes

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().

1 Like

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

1 Like