2

I am trying to move all the meshes ,of which the loaded obj file is made of, away from each other so that user can see all the meshes.

I am trying to mimic the following example

Demo

Go to the link click on explode icon in the UI tray and move the slider.

What Have I done So Far I have access to all the meshes of the model So I can apply any function on them. So far I have looped through them and gave them random position vector.

It works fine and the whole model explodes so that every mesh is separately visible.

The Problem

By giving random position vector they obviously move to random place. I want to move them exactly like the shared link. Any help would be appreciated. I am new to this awesome threejs and I would really love to learn more. Thanks in Advance

Edit Example Code

    function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}
for(var i = 0; i < mainStand.children.length; i++)
{
    pos1 = getRandomArbitrary(1 , 2);
    pos2 = getRandomArbitrary(1 , 2);
    pos3 = getRandomArbitrary(1 , 2);
    mainStand.children[i].position.set(pos1 , pos2 , pos3);
}

Where mainStand is the loaded obj Model

Uzumaki Naruto
  • 753
  • 1
  • 9
  • 27
  • Can you show us some code ? – Serge K. Aug 21 '17 at 08:56
  • @NathanP. Edited the question. This works fine. the model explodes but not as cool as in the demo. I don't want the animation or anything but every mesh should move the direction it is facing. That's it – Uzumaki Naruto Aug 21 '17 at 09:03
  • I don't understand where the problem is ? Do you need the mathematical expression ? Did you tried increasing `x`, `y`, `z` by `1` each steps to see what happens ? – Serge K. Aug 21 '17 at 09:16
  • if every meshe's xyz are increased by the same amount then no mesh will move away from each other rather it would move the entire model. every mesh should move away from each other not move in the same direction @NathanP. – Uzumaki Naruto Aug 21 '17 at 10:38
  • Yes I know that, but since you seem to be aware of that, I still don't understand what you are _really_ asking. Your code works well, you seem to understand it, so you're seeking the algorithm, am I right ? – Serge K. Aug 21 '17 at 12:03
  • @NathanP. what I want is what is what is working in the demo link bro. – Uzumaki Naruto Aug 22 '17 at 06:03

1 Answers1

1

What you want to do is to move every mesh away from the center position. This is done by calculating the vector between the mesh position and the center, and then move the mesh along this direction:

var center = new THREE.Vector3(0, 0, 0);
var distanceToMove = 0.1;

for (var i = 0; i < mainStand.children.length; i++) {
    var meshPosition = mainStand.children[i].position;

    var direction = meshPosition.clone().sub(center).normalize();

    var moveThisFar = direction.clone().multiplyScalar(distanceToMove);

    mainStand.children[i].position.add(moveThisFar);
}
Holger Ludvigsen
  • 2,320
  • 1
  • 26
  • 31