L
L
Levin19942018-03-26 00:58:38
JavaScript
Levin1994, 2018-03-26 00:58:38

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);

Here are the loops from 1 to 15, in the loop I call the object load method
for (let i = 0; i < 15; i++) {
     let object = loadObj('./models/model.fbx');

      scene.add(object);

 }

The function of loading an object and assigning its position, scale
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;
};

Random features
const randPos = () => {
    return Math.random() * 500 - 250;
  };

  const randScale = () => {
     return Math.random() * 2 + 40;
  };
  const randRotation = () => {
    return Math.random() * 2 * Math.PI;
   };

Intersection check method (does not fully work. needs to be finalized)
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;
        }
      }
     };

The idea was to write objects to an empty array and then compare the current object with all, but I don’t understand how to fully implement this logic and how to determine the new random position of the object so that it does not intersect the rest. And then add it to the scene.
How to check for intersections. And if they are, then place the object on a new position?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bhudh, 2018-03-26
@Bhudh

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

What is there to understand? A loop, or forEach, or some, or every… There are a lot of options.
Is it worth it? Extra gestures, it is easier to wait until randomly gives a non-overlapping position.
The definition is required only if the objects are large and the placement area is limited. Then determine the minimum value of the possible distance between these objects and shoot at this distance + epsilon.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question