box3.setFromObject doesn't match object.location

吃可爱长大的小学妹 提交于 2019-12-13 21:06:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!