user2982195
user2982195

Reputation: 153

How to get an object's world position (Three.js)

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

Upvotes: 3

Views: 7338

Answers (2)

user2982195
user2982195

Reputation: 153

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.

Upvotes: 7

Radio
Radio

Reputation: 2853

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.

Upvotes: 0

Related Questions