更改枢纽点后如何正确移动SCNNode?

问题描述

我当前正在场景中放置一个多维数据集。该多维数据集当前位于根节点内。当我在SceneKit编辑器中检查多维数据集的枢轴点时,它位于边界体积的中心,但位于底部表面。我的目标是使枢轴点成为边界体积的中心。我正在使用此代码来实现此目标:

func showNode(node: SCNNode,horizontalPosition: CLLocationCoordinate2D,verticalPosition: Float){
    //All models downloaded will have locations based on centre,therefore set pivot to centre
    //Change pivot point to the centre of the bounding Box to match structure of uploaded DT models
    //1. Get The Bounding Box Of The Node
    let min = simd_float3(node.boundingBox.min)
    let max = simd_float3(node.boundingBox.max)
    //2. Set The Pivot
    //Changing pivot will affect location of childnodes
    node.pivot = SCNMatrix4MakeTranslation(min.x + (max.x-min.x)/2,min.y + (max.y-min.y)/2,min.z + (max.z-min.z)/2)
    
    //Affine transformations (Scale,Rotation,Translation) -> in this order
    node.scale = SCNVector3(Float(scale_W),Float(scale_W),Float(scale_W))
    
    //Coordinates of object in (X,Y) plane
    let latLong = getLocalCoordinates(latLong: horizontalPosition)
    print(SCNVector3(latLong.x,verticalPosition,latLong.z))
    //Place object at world position (referencing world origin)
    node.worldPosition = SCNVector3(latLong.x,latLong.z)
    
    //Store which node is added to AR scene
    outdoorNode = node
}

我可以成功地将立方体放置在定义的世界坐标上。当我想将多维数据集从其当前位置沿X方向(无论哪个方向)平移1m时,问题就开始了。我有一个将节点平移1m的按钮,代码如下:

@objc func leftTap() {
    print("left tap")
    if (isOutdoorNodeInScene) {
        self.outdoorNode.worldPosition.x -= 1
        print(outdoorNode.worldPosition)
        addOrUpdateOutdoorAnchor(anchor: outdoorNodeAnchor!,node: outdoorNode)
    }
}

我希望模型仅在此X轴上发生变化,但是在所有3个轴上都发生变化。我注意到,当我不更改枢轴点时,就会观察到所需的功能。但是,如果我不更改枢轴点,则它不再位于立方体的中心。有人可以解释为什么我观察这种行为吗?我附上了一些图片作为证据。

更改枢轴点以反映中心时:

enter image description here

不更改枢轴点时:

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)