Answer the question
In order to leave comments, you need to log in
How to display objects randomly, but at the same time so that they do not intersect?
I load an object and assign it a random position and scale. I need my objects to be displayed randomly every time, but not intersect at the same time. Now they are displayed, but intersect. The search condition for intersections looks like this:
var collision = firstBB.isIntersectionBox(current);
for (let i = 0; i < 15; i++) {
let object = loadObj('./models/model.fbx');
scene.add(object);
}
export const loadObj = (path, options = {}) => {
this.THREE = THREE;
const container = new THREE.Object3D();
container.loaded = false;
const loader = new FBXLoader();
loader.load(
path,
object => {
container.loaded = true;
object.position.set(randPos(), randPos(), randPos());
object.scale.set(randScale(), randScale(), randScale());
object.rotation.set(randRotation(), randRotation(), randRotation());
container.add(object);
let bbox = new THREE.Box3().setFromObject(container);
const isCollision = checkCollisions(bbox);
objects.push(container);
},
xhr => {
console.log('ERROR')
},
xhr => {
}
);
return container;
};
const randPos = () => {
return Math.random() * 500 - 250;
};
const randScale = () => {
return Math.random() * 2 + 40;
};
const randRotation = () => {
return Math.random() * 2 * Math.PI;
};
const checkCollisions = (current) => {
for (let i = 0; i < objects.length; i++) {
let firstBB = new THREE.Box3().setFromObject(objects[i]);
var collision = firstBB.isIntersectionBox(current);
if (collision) {
remove(objects, i);
return true;
}
}
};
Answer the question
In order to leave comments, you need to log in
The idea was to write objects into an empty array and then compare the current object with all, but I don’t understand how to fully implement this logic
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question