问题
I got an object (loaded with the loader, stl), where I'm trying to create a 'snapping' effect when moving it.
I create a THREE.Box3 from the object to do some detection stuff, but I noticed that my Box3 doesn't match my object's location object.location.
I'm sure I'm messing up worldspace, local space etc etc, so can anyone explain me how I could solve this and what is what?
Code:
var box = new THREE.Box3().setFromObject(activeElement);/
$("#debug").html(
"<p>location: <span>" + activeElement.position.x + " - " + activeElement.position.z + "</span></p>" +
"<p>box - min: <span>" + box.min.x + " - " + box.min.z + "</span></p>" +
"<p>box - max: <span>" + box.max.x + " - " + box.max.z + "</span></p>" +
"<p>sceneScale: <span>" + sceneScale);
So location and box-min don't fit, where they should fit.
回答1:
box3 min and max are point vectors in 3D space, from the lower left behind to the upper right in front of the bounding box.
See this simple example values with a BoxGeometry and the size of (10,10,10):
mesh.position { x: 0, y: 5, z: 0 }
box.min { x: -5, y: 0, z: -5 }
box.max { x: 5, y: 10, z: 5 }
http://jsfiddle.net/L0rdzbej/12/
Update: as figured out in Chat, to place an object next to another ones bounding box use this formula (for snapping on one side of the x-axis):
newPosition.x = otherBox.min.x - ( (activeElement.max.x - activeElement.min.x ) / 2 )
来源:https://stackoverflow.com/questions/31744117/box3-setfromobject-doesnt-match-object-location