9

I have some custom geometries obtained from a STEP file conversion and I use the mouse to rotate them. They rotate around the origin of the scene, but since they are far from it, they seem rotating on a virtual sphere. How can I move them to the origin so that they don't seem "floating" around (I mean that I'd like to reduce to zero the radius of the virtual sphere). This is the example I'd like to move. I've tried setting their position to (0, 0, 0) doing:

object.position.x = 0;
object.position.y = 0;
object.position.z = 0;

but it didin't work.

Emonale
  • 513
  • 2
  • 7
  • 22

1 Answers1

32

The typical solution to this problem is to translate the geometry right after it is created. You do that by applying a translation matrix to the geometry like so:

geometry.applyMatrix( new THREE.Matrix4().makeTranslation( distX, distY, distZ ) );

EDIT: You can simply do this, instead:

geometry.translate( distX, distY, distZ ); // three.js r.72

The function geometry.computeBoundingBox() may be of help to you in determining an amount to translate.

However, I see in your case, you have multiple geometries, so it it a bit more complicated, but doable. You will need to translate each geometry by the same amount.

EDIT

Tip: Instead of adding each object to the scene, create a parent object, add it to the scene, and then add the objects to the parent.

var parent;

parent = new THREE.Object3D();
scene.add( parent );

parent.add( object1 );
parent.add( object2 );
// and so on...

Then in your render function, just rotate the parent, not the individual objects.

WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • Thanks for the answer. Using the parent object doesn't seem to change the situation, so I'll try the first suggestion – Emonale Oct 11 '12 at 09:20
  • The parent-object suggestion was not part of the solution, just a friendly tip. :-) If you chose to implement it, see the "trick" in the answer in http://stackoverflow.com/questions/12795775/rotate-a-collada-model-around-its-own-y-axis as a alternate way to solve your problem. – WestLangley Oct 11 '12 at 09:26
  • Oh now I understood thanks ;-) I created an object, I added the parent to it and then all the geometries to the parent. But I still don't understand how I can move the parent to the origin. If I use the computeBoundingBox() function for the parent and then I get the center of the bounding box, should I move the parent there? – Emonale Oct 11 '12 at 10:01
  • Since you have decided to use the parent trick, and not translate the geometry itself, try `parent.position.set( -800, 300, -800 )` and see if it gets better. This would be more like the negative of the original bounding box, which you could compute exactly. – WestLangley Oct 11 '12 at 10:13
  • Maybe I did something wrong, but it's not getting better. I've updated the page http://sygest.agintersys.com/testing.html I used: object = new THREE.Object3D(); scene.add( object ); parent = new THREE.Object3D(); parent.position.set( -800, 300, -800 ) object.add( parent ); parent.add( object0 ); parent.add( object1 ); ... – Emonale Oct 11 '12 at 10:20
  • The 0.27 scale factor is playing into this... Leave the scale factor and set `parent.position.set( -230, 100, -230 );` – WestLangley Oct 11 '12 at 10:38