3

I want to place my already placed object at a new location, but it moves from the local position and not global.

this._scene.updateMatrixWorld();
this._scene.add(mesh);
var v1 = new THREE.Vector3();    
v1.setFromMatrixPosition( mesh.matrixWorld );  
mesh.position.set(v1.x +2, v1.y + 2, v1.z + 2);
gman
  • 100,619
  • 31
  • 269
  • 393
user2982195
  • 153
  • 1
  • 9

2 Answers2

7

What I did to solve my problem:

Every mesh got a geometry attribute. That is why you can call mesh.geometry. From this point i created a BoundingSphere around my mesh.

mesh.geometry.computeBoundingSphere();

Now it is possible to get the world position of my boundingSphere, which simultaneously is the world position of my mesh.

var vector = mesh.geometry.boundingSphere.center;

Congratulations! 'vector' got the center world position in (x,y,z) of your mesh.

Just to clarify, 'mesh' is a THREE.Mesh object.

user2982195
  • 153
  • 1
  • 9
0

Wouldn't you just then move the object itself by the increment? I'm not sure why you need the matrix involved?

mesh.position.set(mesh.position.x +2, mesh.position.y + 2, mesh.position.z + 2);

edit... if you need to use matrix, you need to set the matrixWorld. Look at http://threejs.org/docs/#Reference/Core/Object3D.matrixWorld but using the position setter will do the heavy lifting for you.

Radio
  • 2,810
  • 1
  • 21
  • 43
  • Nope, that is wrong, because THREE.js will calculate from local coordinates – user2982195 Mar 04 '16 at 14:28
  • 1
    I think I understand now. You have a child mesh at a location in relation to a parent. You place it or a clone directly onto the scene but now the position is no longer right because its position was in reference to a different parent? Perhaps what you need is to call .localToWorld ( vector ) on the original object. localToWorld is a property of object3d. This would be your new start location. – Radio Mar 04 '16 at 18:12
  • The parent is the scene. The Mesh has to be replaced to new coordinates, but when I do so, the Mesh only moves to local coordinates from its position. – user2982195 Mar 07 '16 at 07:58
  • If the scene is the parent your local and world matrixes are the same. – Radio Mar 07 '16 at 19:25
  • I would not ask the question when local and world matrixes were the same. For example: I move object1 to (1,1,1). Now I move object2 to (1,1,1). They are not at the same location. Both are children of the scene. – user2982195 Mar 08 '16 at 11:48
  • Ok sorry , your issue is becoming clearer as I state or question things. I would suggest in this case, one mesh has its origin offset which needs to be calculated to align it's calculated center point of the mesh with the position you want it to move to. The mesh's actual centroid is not necessarily the object3d's matrix origin. You can calculate the centroid and transform the mesh, or set the inverse of the centroid as the world position. I suggest this answer may help: http://stackoverflow.com/questions/14493019/three-js-child-mesh-position-always-return-0-0-0 – Radio Mar 08 '16 at 16:49