Three.js child grid constant position always (0,0,0)

I have loaded some objects via OBJLoader, the loaded object contains one parent and multiple children; then I apply Raycaster and find the clicked child.

Then I want to update the position of the child, but the original position is zero for all children.

var intersect = intersects[0].object;
intersect.position.y = intersect.position.y + 5; // 0 + 5 

      

But everything looks good in my scene. Also, if I remove the clicked object, it is actually removed from the scene. I guess I missed some point where their positions cannot be (0,0,0). How can I reach their relative position?

+3


source to share


3 answers


Try this, then read position (). I had the same problem, here was the answer. (geometry is your mesh geometry.)



objMesh.centroid = new THREE.Vector3();
for (var i = 0, l = geom.vertices.length; i < l; i++) {
    objMesh.centroid.add(geom.vertices[i].clone());
}
objMesh.centroid.divideScalar(geom.vertices.length);
var offset = objMesh.centroid.clone();

objMesh.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(-offset.x, -offset.y, -offset.z));

objMesh.position.copy(objMesh.centroid);

      

+2


source


Position relative to parent



Multiply the position by the parent's transform to get the world space coordinates, if that's what you are looking for

+2


source


This is because all objects in the obj file have their axis of rotation at World 0,0,0 regardless of where their local bar was prior to export.

0


source







All Articles